Tuesday 2 April 2013

Example program for Life cycle of servlet

Example program for Life cycle of servlet



Steps:
1. create the folder structure
2. set the classpath
3. compile the servlet programs
4. deploy the application in server
5. run the web application in the browser


1. Create the folder structure 

(i) create the folder with any name (Ex: LifeCycleServlet)
(ii) create the html file
(iii) servlet program
(iii) create the WEB-INF folder


(ii) create the html file :  Home.html

<html>
 <head>
  <title>LifeCycleServlet</title>
 </head>
 <body>
  <form action="myFirstServlet" method="get">
   <br/><br/><br/>
   <center>
    <input type="submit" value="invoke LifeCycle"/>
   </center>
  </form>
 </body>
</html>

//output


(ii) write the servlet program
//LCServlet.java
package com.rajendra.servlets;
import javax.servlet.*;


import java.io.IOException;
import java.io.PrintWriter;
public class LCServlet implements Servlet
{
 public void init(ServletConfig sc)
 {
  config=sc;
  System.out.println("in init");
 }
 public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException
 {
  PrintWriter out=res.getWriter();
  out.println("Hello from LifeCycleServlet");
 }
 public void destroy()
 {
  System.out.println("in destroy");
 }
 public String getServletInfo()
 {
  return "LifeCycleServlet";
 }
 public ServletConfig getServletConfig()
 {
  return config;
 }
 private ServletConfig config;
}

/*
set the classpath to compile the servlet program
//D:\\LifeCycleServlet>set CLASSPATH=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.;
//D:\\LifeCycleServlet>javac -d . LCServlet.java
*/
                                                       (iii) create the WEB-INF folder

1.  create WEB-INF folder in LifeCycleServlet folder,go to WEB-INF folder
2.  web.xml file
3. create classes folder
4. lib folder


->now copy the class file of LifeCycleServlet folder of LCServlet.java
-> paste in to classes folder of WEB-INF folder

                                                                        4. web.xml file

1. create a note pad in WEB-INF folder
2. type the following in that note pad
3. save it as web.xml file

<web-app>
<servlet>
<servlet-name>ls</servlet-name>
  <servlet-class>com.rajendra.servlets.LCServlet</servlet-class>
  </servlet>

<servlet-mapping>
  <servlet-name>ls</servlet-name>
  <url-pattern>/myFirstServlet</url-pattern>
  </servlet-mapping>
</web-app>

4. save the above file and save it as web.xml in WEB-INF folder
5. go to command prompt and prepare the war file (this is optional)

D:\\LifeCycleServlet>jar cvf LCS.war * .class

4. deploy the application in server

Steps:
1. copy the folder(LifeCycleServlet) or Copy the WAR file
2. deploy into the server
(i) go to tomcat server installed directory(assume that we have installed c : drive)
3. double click on C:
4.  click on Program Files
5. Go to TomCat Server 6.0 (latest version)
6.  open 'webapps' folder
7. paste your folder(application) ie. LifeCycleServlet


5.   Start the Tomcat Server

1.  go to C:\Program Files\Apache Software Foundation\Tomcat 6.0
2. open the bin folder
3.  double click on tomcat6.exe file
4. now we will see the following (see the figure)

6.   Executing (running) the servlet program

1. go to any web browser
2. type http://localhost:8888/ (here 8888 is the port number)

3. double click on Tomcat Manager
4. select your application from the list
5. http://localhost:8888/LifeCycleServlet/
6. and type the home.html at the end of the URL
eX:http://localhost:8888/LifeCycleServlet/Home.html

7. double click on the button i.e invoke LifeCycle


You may like the following posts:


   

No comments:

Post a Comment