78-2
78-2
Objective: Install TOMCAT web server and APACHE. Access the above developed
static web pages for books web site, using these servers by putting the web pages
developed .
Theory:
You must set the JAVA_HOME environment variable to tell Tomcat where to find Java. Failingto
properly set this variable prevents Tomcat from handling JSP pages. This variable should listthe
base JDK installation directory, not the bin subdirectory.
On Windows XP, you could also go to the Start menu, select Control Panel, chooseSystem, click on
the Advanced tab, press the Environment Variables button at the bottom, and enter the
JAVA_HOME variable and value directly as:
Name: JAVA_HOME
Value: C:\jdk
Since servlets and JSP are not part of the Java 2 platform, standard edition, youhave to identify
the servlet classes to the compiler. The server already knows about theservlet 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 unknownclasses.
Name: JAVA_HOME
Value: install_dir/common/lib/servlet-api.jar
The next step is to tell Tomcat to check the modification dates of the class files of requested
servlets and reload ones that have changed since they were loaded into theserver's memory. This
slightly degrades performance in deployment situations, so isturned off by default. However, if
you fail to turn it on for your development server,you'll have to restart the server every time you
recompile a servlet that has already beenloaded into the server's memory.
Be sure to make a backup copy of server.xm before making the above change
Enable the Invoker Servlet:
The invoker servlet lets you run servlets without first making changes to yourWeb application's
deployment descriptor. Instead, you just drop your servlet into WEB-INF/classes and use the
URL http://host/servlet/ServletName . The invoker servlet isextremely convenient when you are
learning and even when you are doing your initialdevelopment.
To enable the invoker servlet, uncomment the following servlet and servlet-mapping elements in
install_dir/conf/web.xml. Finally, remember to make a backup copyof the original version of this
file before you make the changes.
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
…
</servlet>
…
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
PROGRAM 8
Objective: Assume four users user1, user2, user3 and user4 having the passwords
pwd1, pwd2, pwd3 and pwd4 respectively. Write a servlet for doing the following.
1. Create a Cookie and add these four user id’s and passwords to this Cookie.
2. Read the user id and passwords entered in the Login form (week1) and authenticate
with the values (user id and passwords) available in the cookies
Theory:
Servlet Life cycle:
1. Servlet class loading
2. Servlet Instantiation
3. call the init method
4. call the service method
5. call destroy method
If you consider a servlet to be just like any other Java program, except that it runs within a
servlet container, there has to be a process of loading the class and making it ready for requests.
Servlets do not have the exact equivalent of a main method that causes them to start execution.
When a web container starts it searches for the deployment descriptor (WEB.XML) for each of
its web applications. When it finds a servlet in the descriptor it will create an instance of the
servlet class. At this point the class is considered to be loaded (but not initialized).
The HttpServlet class inherits the init method from GenericServlet. The init method performs a
role slightly similar to a constructor in an “ordinary” Java program in that it allows initialization
of an instance at start up. It is called automatically by the servlet container and as it causes the
application context (WEB.XML) to be parsed and any initialization will be performed. It comes
in two versions, one with a zero parameter constructor and one that takes a ServletConfig
parameter.
The servlet engine creates a request object and a response object. The servlet engine invokes the
servlet service() method, passing the request and response objects. Once the init method returns
the servlet is said to be placed into service. The process of using init to initialize servlets means
that it is possible to change configuration details by modifying the deployment descriptor
without having them hard coded in with your Java source and needing a re-compilation.
void init(ServletConfig sc)
The service() method gets information about the request from the request object, processes the
request, and uses methods of the response object to create the client response. The service
method can invoke other methods to process the request, such as doGet(), doPost(), or methods
you write. The service method is called for each request processed and is not normally
overridden by the programmer.
The code that makes a servlet “go” is the. servlet
Servlet void service(ServletRequest req,ServletResponse res)
The destroy Method:
Two typical reasons for the destroy method being called are if the container is shutting down or
if the container is low on resources. This can happen when the container keeps a pool of
instances of servlets to ensure adequate performance. If no requests have come in for a particular
servlet for a while it may destroy it to ensure resources are available for the servlets that are
being requested. The destroy method is called only once, before a servlet is unloaded and thus
you cannot be certain when and if it is called.
void destroy()
ServletConfig Class:
ServletConfig object is used by the Servlet Container to pass information to the Servlet
during it's initialization. Servlet can obtain information regarding initialization parameters and
their values using different methods of ServletConfig class initialization parameters are
name/value pairs used to provide basic information to the Servlet during it's initialization like
JDBC driver name, path to database, username, password etc.