1. ignousite.blogspot.com (ignou study helper)
Mcsl 054 (51) lab manual
E1.
Verify Installation and setting of Web Container / Web Server / Tomcat and prepare an Installation report, which
contains setting of class path. Server port, starting and shutting down of server etc.
-----------------------------------------------------------------------------------------------------------
Installing Tomcat
This is a brief "how-to" for installing Tomcat on a Windows PC.
Installing Java
Tomcat requires java in order to run. If your computer already has java installed, you can probably skip this step.
However, make sure you have a recent version of java. Here I provide instructions for installing version 1.4.2 of the
Java 2 Platform, Standard Edition (J2SE).
Steps for installing java
1. Go to the download page of J2SE Version 1.4.2.
2. Select the version for Windows and click through the license acceptance. After two pages, you will be able to
download the EXE file for installing java on windows. Look for the SDK version.
3. Download and run the EXE installation program.
4. You will need to accept the license agreement again.
5. Use the suggested directory for installing java (C:j2sdk1.4.2_01).
6. You may use the remaining default settings for the installation.
Setting the Java Environment Variable
Tomcat will need to know where you have installed java. To do this, you will need to set the environment variable
JAVA_HOME to C:j2sdk1.4.2_01 (where you installed java).
Here are the steps for setting the environment variable on my computer (Windows XP Professional). The steps will
probably be similar for other Windows computers.
1. Open the control panel under the start menu.
2. Double-click on System.
3. Click on the Advanced tab.
4. Click on the Environment Variables button.
5. Under System Variables, click on the New button.
6. For variable name, type: JAVA_HOME
7. For variable value, type: C:j2sdk1.4.2_01
8. Continue to click OK to exit the dialog windows.
Installing Tomcat
After setting the JAVA_HOME environment variable, you can install tomcat.
1. Go to the Tomcat Web page.
2. Click on Binaries under the Download label on the left side of the page.
3. Scroll down until you see Tomcat 4.1.x. (x will be some number greater than 10).
4. Click on the link ending with exe (e.g. 4.1.27 exe).
5. Download and run the exe file.
2. ignousite.blogspot.com (ignou study helper)
6. I suggest you install Tomcat at c:tomcat4
7. Use the default settings and provide a password that you will remember.
8. now assume that your tomcat are installed at c:tomcat4
Running Tomcat
Here are the steps to see if Tomcat has been successfully installed
1. Start Tomcat by finding its start program in the Programs Menu (located in the Start menu). Look under Apache
Tomcat 4.1 and select "Start Tomcat".
2. Open a Web browser and type in the following URL:
o http://localhost:8080/
At this point, you should see the Tomcat home page, which is provided by the Tomcat Web server running on your
computer. Note: if your computer has an internet name or an IP number, you may access your Tomcat server
anywhere on the internet by substituting localhost with the full name or IP number.
To shut down your server and remove the Console window, select "Stop Tomcat" in the same menu of where you
selected "Stop Tomcat".
. Set Your CLASSPATH
Since servlets and JSP are not part of the Java 2 platform, standard edition, you have to identify the servlet classes
to the compiler. The server already knows about the servlet classes, but the compiler (i.e., javac) you use for
development probably doesn't. So, if you don't set your CLASSPATH, attempts to compile servlets, tag libraries, or
other classes that use the servlet and JSP APIs will fail with error messages about unknown classes. Here are the
standard Tomcat locations:
Tomcat 4: c:tomcat4commonlibservlet.jar
in addition to the servlet JAR file, you also need to put your development directory in the CLASSPATH. Although this
is not necessary for simple packageless servlets, once you gain experience you will almost certainly use packages.
Compiling a file that is in a package and that uses another class in the same package requires the CLASSPATH to
include the directory that is at the top of the package hierarchy. In this case, that's the development directory I just
discussed. Forgetting this setting is perhaps the most common mistake made by beginning servlet programmers!
Finally, you should include "." (the current directory) in the CLASSPATH. Otherwise, you will only be able to compile
packageless classes that are in the top-level development directory.
Here are two representative methods of setting the CLASSPATH. They assume that your development directory is
C:Servlets+JSP. Replace install_dir with the actual base installation location of the server. Also, be sure to use the
appropriate case for the filenames, and enclose your pathnames in double quotes if they contain spaces.
• Windows 98/Me. Use the autoexec.bat file.
o Tomcat 4
Sample code: (Note that this all goes on♣ one line with no spaces--it is broken here only for readability.)
set CLASSPATH=.; C: Tomcat4commonlibservlet.jar
Sample file to download♣ and modify: autoexec.bat
Note that these examples represent only one approach for setting the CLASSPATH. Many Java integrated
development environments have a global or project-specific setting that accomplishes the same result. But these
settings are totally IDE-specific and won't be discussed here. Another alternative is to make a script whereby -
3. ignousite.blogspot.com (ignou study helper)
classpath ... is automatically appended onto calls to javac.
• Windows NT/2000/XP. You could use the autoexec.bat file as above, but a more common approach is to use
system settings. On WinXP, go to the Start menu and select Control Panel, then System, then the Advanced tab,
then the Environment Variables button. On Win2K/WinNT, go to the Start menu and select Settings, then Control
Panel, then System, then Environment. Either way, enter the CLASSPATH value from the previous bullet.
Enable the Invoker Servlet
The invoker servlet lets you run servlets without first making changes to your Web application's deployment
descriptor (i.e., the WEB-INF/web.xml file). Instead, you just drop your servlet into WEB-INF/classes and use the
URL http://host/servlet/ServletName (or http://host/webAppName/servlet/ServletName once you start using your
own Web applications). The invoker servlet is extremely convenient when you are learning and even when you are
doing your initial development. To enable the invoker servlet, uncomment the following servlet-mapping element in
c:tomcat4confweb.xml. Also, do not confuse this Apache Tomcat-specific web.xml file with the standard one that
goes in the WEB-INF directory of each Web application. Finally, remember to make a backup copy of the original
version of this file before you make the changes.
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
Create a simple web application
Here are the steps for running the class examples discussed on the first day.
1. Goto the following location on your computer: C:Tomcat 4webapps
o Create a directory “webdir” under C: Tomca4webapps, which will be your home directory for your assignment.
o Crate a directory “WEB-INF” under C: Tomcat4webapps webdir
o Crate a directory “classes” under C: Tomcat4webappswebdirWEB-INF, which will hold your servlet class files.
2. Goto http://127.0.0.1:8080/examples/servlets/helloworld.html, copy the java code and paste it into a blank file in
your editor (such as notepate), and save it as
c: Tomcat4webappswebdirWEB-INFclassesHelloWorld.java
3. Open a “Command Prompt” windows, and goto c: Tomcat4webappswebdirWEB-INFclasses
4. try “javac HelloWorld.java”
5. you will get a HelloWorld.class
6. Stop and restart Tomcat.
7. You can access the first example with the following URL:
o http://localhost:8080/webdir/servlet/HelloWorld
8. You can work at your own project based on webdir directory, and use some IDEs for programming, such as
Jcreator or EditPlus.
2.
E2.html
<html>
<title>Welcome Page</title>
5. ignousite.blogspot.com (ignou study helper)
</html>
E3.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class e3 extends HttpServlet
{
String name=null;
String address=null;
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
name=req.getParameter("name");
address=req.getParameter("address");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<h1 align=center> Java Programing</h1>");
out.println("<h3>Your Name :"+name+"</h3>");
out.println("<h3>Your Address :"+address+"</h3>");
out.close();
}
}
4. e4.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class e4 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
// get the current session object, create one if necessary
HttpSession session=req.getSession(true);
// Increment the hit count for this page, the value is saved
// in this clients session under the name"tracker.count"
Integer count=(Integer)session.getValue("tracker.count");
if(count==null)
count=new Integer(1);
else
count=new Integer(count.intValue()+1);
session.putValue("tracker.count",count);
out.println("<html><title>Session Counts</title><body>");
out.println("<h2>Current Date And Time:"+new Date();+"</h2>");
6. ignousite.blogspot.com (ignou study helper)
out.println("<br><h3>The servlet has been"+count+((count.intVAlue()==1)?"time":"Times")+"accessed
since loaded<h3>");
out.println("</body></html>
out.close();
}
}
5.e5.html
<html>
<title>Welcome Page</title>
<body>
<form method="post" action="/inter.do">
<h1 align=center>Inter Servlet Communication</h1><br>
<h3>Enter Your Name:<input type="text" name="name"></h3>
<input type="submit" value="Click Me">
</form>
</body>
</html>
E5.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class e5_1 extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
String msg=null;
String name=req.getParameter("name");
msg="Hi "+name+", Welcome to Servlet Programming";
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<br><h3>"+msg+" From First Servlet</h3><br>");
RequestDispatcher rd=req.getRequestDispatcher("e5_2.class");
rd.forward(req,res);
out.close();
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class e5_2 extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
String msg=null;
String name=req.getParameter("name");
msg="Hi "+name+", Welcome to Servlet Programming";
7. ignousite.blogspot.com (ignou study helper)
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<br><h3>"+msg+" From Second Servlet</h3><br>");
out.close();
}
}
6. e6.html
<html>
<title>Welcome Page</title>
<body>
<form action="/e6.do">
<h1 align=center>J2ee Programing</h1><br>
<h3>Enter Your Name:<input type="text" name="name"></h3>
<h3>Enter Your Address:<input type="text" name="address"></h3>
<input type="submit" value="Click Me">
</form>
</body>
</html>
E6.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class e6 extends HttpServlet
{
String sname=null;
int sport;
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
sname=req.getServerName();
sport=req.getServerPort();
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<h1 align=center> J2ee Programing</h1>");
out.println("<h2>Server Information</h2><br>");
out.println("<h3>Server Name :"+sname+"</h3>");
out.println("<h3>Server Port :"+sport+"</h3>");
out.close();
}
}
7. e7.html
<html>
<title>Welcome Page</title>
<body>
<form action="/e7.do">
<h1 align=center>J2ee Programing</h1><br>
8. ignousite.blogspot.com (ignou study helper)
<h3>Enter Your Roll Number:<input type="text" name="sno"></h3>
<input type="submit" value="Click Me">
</form>
</body>
</html>
E7.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class e7 extends HttpServlet
{
int sno;
Connection con=null;
Statement st=null;
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
sno=req.getParameter("sno");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
st=con.createStatement();
String sq="select * from student where sno="+sno;
ResultSet rs=st.executeQuery(sq);
if(rs.next())
{
out.println("<h1 align=center> J2EE Programing</h1>");
out.println("<br><h3>Student Information</h3>");
out.println("<br>Student Number"+sno);
out.println("<br>Student Name :"+rs.getString(2);
out.println("<br>Student Address :"+rs.getString(3);
out.println("<br>Student Email Id :"+rs.getString(4);
out.println("<br>Program of study :"+rs.getString(5);
out.println("<br>Year of Admission :"+rs.getDate(6);
}
else
out.println("<br>Invalid Student Number Or Student doesn't Exist");
}
catch(Exception e)
{
out.println(e);
}
finally()
{
st.close();
27. ignousite.blogspot.com (ignou study helper)
try
{
if(ps!=null)
ps.close();
if(con!=null)
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
%>
</table>
</body></html>
20. e20.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Ramu</to>
<from>Deenu</from>
<heading>Festival Whishes</heading>
<body>May God give all the happiness to you and your family and
i am wishing u the best wishes</body>
<closing>Best Wishes</closing>
<signature>Ramu</signature>
</note>
21. e21.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
<book>
<title>My First Servlet</title>
<unit1>
<name>Introduction to servlet</name>
<chapters>
<ch1>* What is Servlet</ch1>
<ch2>* what are the advantage of servlet</ch2>
</chapters>
</unit1>
<unit2>
<name>Servlet Life Cycle</name>
<chapters>
<ch1>* Servlet Initialization</ch1>
<ch2>* Servlet Reloading</ch2>