You are on page 1of 9

What is covered in this Servlet example

1. How to write the servlet class. 2. How to compile the Servlet class. 3. How to extract the HTML form parameters from HttpServletRequest. 4. web.xml deployment descriptor file. 5. How to create the war (web application archive) file. 6. How to deploy and run the sample web application in tomcat web container. You need the Java SDK and tomcat web server installed, to run the example Servlet application developed in this tutorial. Setting up the Servlet development environment tutorial explains how to setup the development environment. If you don’t have the basic understanding of the J2EE web containers and web application structure, read the previous Servlet tutorials which explain these basic things. This is a very simple web application containing a HTML file and a servlet. The HTML document has a form which allows the user to enter the name, when the form is submitted application displays the welcome message to the user. First of all we need to create the directory structure for our sample web application as explained in the tutorial Understanding the directory structure of web applications. Create the directory structure as shown in below figure.

Create the HTML file.
Copy the following code into form.html file and save it under servlet-example/pages directory.

<html> <head> <title>The servlet example </title> </head> <body> <h1>A simple web application</h1>

servlet.init(config). */ response.HttpServlet.io. import java. /* * Write the HTML to the response */ out. String welcomeMessage = "Welcome "+name. javax.servletexample.getWriter().java file and save it under servlet-example/WEBINF/src/jsptube/tutorials/servletexample directory.servlet.ServletConfig.HttpServletRequest.PrintWriter.println("<head>").IOException. javax. package jsptube.servlet. out.getParameter("name").setContentType("text/html"). IOException { /* * Get the value of form parameter */ String name = request. .servlet.ServletException.servlet. javax. /* * Set the content type(MIME Type) of the response.http. } protected void doPost(HttpServletRequest request.http. HttpServletResponse response) throws ServletException. import import import import import javax.io.HttpServletResponse. out.http.println("<html>"). PrintWriter out = response. public class WelcomeServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { super.println("<title> A very simple servlet example</title>"). import java. javax.<form method="POST" action="WelcomeServlet"> <label for="name">Enter your name </label> <input type="text" id="name" name="name"/><br><br> <input type="submit" value="Submit Form"/> <input type="reset" value="Reset Form"/> </form> </body> </html> The welcome Servlet class Copy the following code into WelcomeServlet.tutorials.

<web-app version="2.com/xml/ns/j2ee http://java.sun.println("<h1>"+welcomeMessage+"</h1>").xsd"> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servletclass>jsptube.xml file and save it directly under servlet-example/WEB-INF directory.println("<a href="/servletexample/pages/form.println("<body>").WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> /pages/form.servletexample.w3. out. You can use jar command. ant or an IDE like Eclipse. out.4" xmlns="http://java.html">"+"Click here to go back to input page "+"</a>").java It will create the file WelcomeServlet. . out.class in the same directory. Open the command prompt and change the directory to the servlet-example/WEBINF/src/jsptub/tutorials/servletexample directory. The deployment descriptor (web.com/xml/ns/j2ee" xmlns:xsi="http://www.println("</html>"). } public void destroy() { } } Now compile the servlet class as explained below.out. Web applications are packaged into a WAR (web application archive).java using the following command.println("</body>").println("</head>").com/xml/ns/j2ee/web-app_2_4. There are many different ways to create a WAR file.close(). out.sun. All the Servlets and other classes used in a web application must be kept under WEB-INF/classes directory.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java. out. Copy the following code into web.xml) file. javac WelcomeServlet. This tutorial explains how to create the WAR file using jar tool.tutorials.html </welcome-file> </welcome-file-list> </web-app> Create the WAR (Web application archive) file.sun. out. Compile the WelcomeServlet. Copy the class file to classes directory.

you should see the screen similar to below when you open the application in browser. into an archive file called servletexample. open the command prompt and change the directory to <TOMCAT_HOME/bin> directory and run the startup.Open the command prompt and change the directory to the servlet-example directory. This command lists the content of the WAR file. If tomcat server is running on port other than 8080 than you need to change the URL accordingly. If you are using any other J2EE server.war * This command packs all the contents under servlet-example directory. To start the server.war.war to <TOMCAT_HOME>/webapp directory. That’s it! You have successfully deployed the application to tomcat web server. This tutorial explains how to deploy the sample web application to tomcat web container. .war. Deploying the application to tomcat web container. If the application has been deployed properly.war file. tomcat will extract the war file into the directory with the same name as the war file. You can use following command to view the content of servletexample. Our sample application can be accessed at http://localhost:8080/servletexample/. and execute the following command.bat file. A web application can be deployed in tomcat server simply by copying the war file to <TOMCAT_HOME>/webapp directory. including subdirectories. We used following command-line options:    -c option to create new WAR file. Jar –tvf servletexample. -v option to generate verbose output. -f option to specify target WAR file name. jar cvf servletexample. consult the documentation of the server. Once you start the server. Copy servletexample. Deployment steps are different for different J2EE servers.

Enter your name in text box and click on submit button. How the application works. . The next step is to understand how the application works. It will display welcome message with your name as shown in below figure.

The init() method: Next lines of code declares the init() method. package jsptube.html file is specified as welcome file in web.IOException. java.servlet.servlet. the web container delegates the request to WelcomeServlet class. javax.Servlet interface.servlet. The next step is to understand the code. First line of code declares the package.servlet.http. .servlet. Understanding the code.servlet.Servlet interface.servletexample.ServletException. These are the most common classes and interfaces that you will need to import in every servlet. browser sends the HTTP POST request with name parameter.servlet package contains classes and interfaces that are independent of HTTP protocol. Init() method is a Servlet life cycle method and defined in javax.servlet.xml.html file.    Extract the name parameter from HttpServletRequest object. When you fill the form field and click on submit form button. javax.Servlet interface indirectly by extending javax.http package contains classes and interfaces that are specific to HTTP protocol.http. When the request is received by WelcomeServlet it performs following tasks.PrintWriter.xml file so web server serves this file by default.servlet. Based on the servlet mapping in web.ServletConfig. The next lines of code import the required classes and interfaces. import import import import import import import java.servlet. Generate the HTML document and write the response to HttpServletResponse object.io.HttpServletRequest. Generate the welcome message. the javax. Browser receives the HTML document as response and displays in browser window.HttpServletResponse.tutorials. javax. javax.servlet.tutorials.io. Generally Servlets in web application implements javax. Import servlet packages: The Java servlet API consists of two packages: javax.http. \pages\form.http.When you access the application by navigating to URL http://localhost:8080/servletexample/ the web server serves the form. javax. HttpServlet class provides HTTP specific implementation of Servlet interface. public class WelcomeServlet extends HttpServlet { Every servlet class needs to implement javax.servlet.servlet and javax. javax. Servlet class declaration: Next line of code declares the WelcomeServlet class.http. Package declaration WelcomeServlet class is a part of jsptube.servletexample package.HttpServlet. Of these two packages.HttpServlet class.

HttpServletResponse response) throws ServletException. String name = request.println("</head>"). out. HttpServlet class provides the implementation of service() method which calls either doGet() or doPost() method based on the HTTP request method.getParameter("name"). in out example. getParameter() method takes the name of the form field as String argument and returns whatever user has entered into the form as String value. out.println("<h1>"+welcomeMessage+"</h1>"). response.println("<title> A very simple servlet example</title>"). it just calls the super class version of the method.io. init() method doesn’t do anything. The answer is: because it is implemented by HttpServlet class.println("<html>"). HttpServletResponse object encapsulates the HTTP response. Generate Response: The next line sets the content type of the response. this is text/html. HttpServletRequest object encapsulates the information contained in the request. Following line of code extracts the value of name parameter. out.println("<a href="/servletexample/pages/form.getWriter(). doPost() method is used to handle the HTTP POST request. getParameter() method of HttpServletRequest interface is used to extract the value of form parameters. protected void doPost(HttpServletRequest request. Extract request parameters from HttpServletRequest object. PrintWriter out = response. doPost() method is defined in HttpServlet class.println("<body>"). Response can be returned to the client by writing to the java.html">"+"Click here to go back to input page "+"</a>"). Service the HTTP POST request: Next lines of code declares the doPost() method.public void init(ServletConfig config) throws ServletException { super. out.init(config). } Init() method is used to initialize the servlet. . This method takes HttpSevletRequest and HttpServletResponse objects as arguments. Since we have used POST method in our HTML form.println("</body>"). The next line is very simple it constructs the welcome message.<br /> Following lines of code writes the HTML to the response.PrintWriter object associated with ServletResponse. doPost() is called. out.println("<head>"). IOException {<br /> You may wonder why there is no service() method defined in the WelcomeServlet class. out. out. out.setContentType("text/html"). String welcomeMessage = "Welcome "+name. In our example servlet. Following line obtains the PrintWriter object from HttpServletResponse object.

Following lines declares the XML name space. Every Servlets in a web application must be defined in the web. <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>jsptube. such as an HTML or JSP document. <web-app xmlns="http://java.com/xml/ns/javaee" xmlns:xsi="http://www. Next is servlet mapping. out. can be specified as a welcome file using the <welcome-file-list> element in the deployment descriptor.xml file is called the deployment descriptor. Next is welcome file list. browser sends the POST request to path /WelcomeServlet which is than handled by WelcomServlet class. Destroy() method is a servlet life cycle method and defined in javax.com/xml/ns/javaee/web-app_2_5.close().xml Deployment descriptor file: Web.servletexample. public void destroy() { } Destroy method is called by the web container when removing servlet instance out of service or when the server is shutting down. The first line in the deployment descriptor specifies the version of the XML and encoding used.tutorials. The following servlet mapping definition specifies that the request for the URL /WelcomeServlet should be handled by WelComeServlet class. Servlet-class specifies the fully qualified class name of the servlet.w3.sun.sun. the web container displays the welcome file automatically.println("</html>").Servlet interface.5”> Next is the servlet definition.out.xsd" version="2. Any public resource. it includes the configuration of our web application. . this name is used later in servlet-mapping.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java. The web.sun. When you type the URL path pointing to a web application (for example http://localhost:8080/servletexample). <form method="POST" action="/WelcomeServlet"> So when the form is submitted.xml file using <servlet> tag. Following lines defines the WelcomeServlet.com/xml/ns/javaee http://java.servlet. Destroy() method: Next lines of code declares the destroy() method.WelcomeServlet</servletclass> </servlet> Servlet-name tag specifies the name of the servlet. Servlet mapping is used to map the URLs to a servlet. <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping> We have used /WelcomeServlet as the value of action attribute in the HTML form. Close the PrintWriter object.

web container server pages/form.html as the welcome file.Following lines specifies /pages/form.html file. So when you access the URL http://localhost:8080/servletexample. </web-app> . <welcome-file-list> <welcome-file> /pages/form.html </welcome-file> </welcome-file-list> Next line ends the <web-app> tag.

   .

1472  .

-4/ .

09 ...09.88 45901443.4/03940.10.420$07..09 0.92  %00.250..3/8.09:3/07807.420$07.

 .

87.

859:-0.

9:947.8.

.947     5.09#06:089706:089 995$07.09#06:089 25479. 807...077/0 5:-.09431 25479.431 9748$07..09 $07.4//4!489 995$07.09#0854380  5:-.090903/8995$07. 807..09.250/70.05943 8:507 39 .09.09 $07..09...4/39 $07.90/.05943 25479.05943 25479.09431..8 807. 807.05943 . 4  .05943  ....250  25479.807.880.09   .420$07.09 995 995$07. 807. 807.09 25479.09#0854380 70854380 9748$07.090..09 995 995$07.0859:-0 9:947.. 4 !7397907  25479...431  <   57490.090...09 995 995$07..

 0990.20907 ..7.:04114725.

20907 3. $9733.20 .00.420 3.420088.20706:089 09!.7.20  $9730.

439039950 %50 419070854380  .  $0990.

 70854380 809439039%50 909.

92   !73979074:970854380 097907  .

 79090%949070854380 .

090./  4:9 57393 990. 4:9 57393 92  4:9 57393 0.250.078250807.

990  .

4:9 57393 .

0 ./  4:9 57393 -4/  4:9 57393  0.0.420088.

  4:9 57393 .701.

090.250.807.

08.5.

0 ..9435:95.1472 92 .070944 -.

  4:9 57393 ..

-4/  4:9 57393 .

30/-04  50390.425090807.09.3/.9479490807.09 0.480   <   5:-..88..4//08974   < < 4.3090/70.92  4:9 .805.250..422.3/574259.

 .

87.

859:-.

8.9:947.

 0-..09 .09 ...807..20/70.420$07.420$07...09 .881094.422.947 90$07.883 908.947 4250900.420$07..0.947 4590.098.8808:80/3.090.9..9432:89-0059:3/07 ..9090100..250/70.:8390 1443.55.3/ .8808/70..3/4907..70.

.3/8.8808/70.4/03940- 210.9:3/07807.250.947   %0/0542039/08...09 0.75947 0- 2 10  45901443.09/70.

07843  238995.  /70.55.947  0- .

.

.42. 8:3 ...

2.

38.

00 2388995.

.

  47.

 .

$.02. 389.943995.0 88.4..02.3.

.

.. 8:3 .42..

2.

38.

00 995.

.

..42.. 8:3 .

2.

38.

00.

55** 8/   807.09 3.0- .09.420$07.09   807.200.

09..09 .807.250 0.090.420$07.09 3.8 807.20   807.88859:-0 9:947.

09 ..88  .807.

420$07.09 2.09 3.09.09  807.807.200.553   807.

807.99073.09 3.20   :7 5.

0.09.420$07.

99073  .:7 5.

09 2.420 10 89   0.420 10.807.553  0.

08.5.

1472 92.

0.420 10  .

420 10 89 .0.

.3/1107039.55.702.9438.70.580 %89:947.384 94.# 0-...30.#10 4:.3/ ..943.55.7.7.55 70..70.943.05.3:80.7944  .7.0- .0 10  0-..9090#10:83.3947.705..422.90.0 %070.0/394.8 94.55.9090# 0-..

90.250/70..438:990 /4.90-.4390398:3/07807.20  4:.55.1807.7 9.90-.3/89890...010.307 14:.090.090.38494/05490 8.9030#10  .94394942...07-4804:95:9  14594394850.70:83.250 .250/70.4390394190#10  054390.45390.422..439.7.710  ..70.3/94.:2039.50390.3 .05.3/5.3:801443.3-0/0540/3942.943.34907807.07  0-...090.250 .07 ..70/1107039147/1107039807.307  054203989058.709#103.43903941807.3/.07825-.7  %8.947 .94394942.9807.947 3.3/574259.250 .422.09 0.7  0:80/1443.:/38:-/70.8.9434190807.422.3/00.55.250 .090.:90 901443.2500-.90.3090/70..078 %89:947.3/ 30459438 O O O .55..9479490807.7.4594394.0/807.19.3/  .7  %8.422.45943940307.71094 % %* .439.422.94708 394.1807.090.422.09 0..

794% %* .090.55/70.947  45807.250 .0-.

7990807.3090/70.0-.422.3/.07  3.990.90-807.9097.947 %...04:89.710  %489.7990807.07 450390.07 942.203.0881: /0540/90...55.94394942..9 894:..55/70.7 1039490/70.3/574259.94794% %* .08:..20.9479908.890.

9995..947.250.0880/.-3 /70..943..79:5 -.3/7:39089.3-0..910  :78.55.

.

4.489  ..

090.807.250.

.9433-74807  .078 7:33343547949079..47/3  190.700382.794-04034: 450390..943..3090&#. 1942.8-003/0540/574507 4:84:/800908..9807.55.34:300/94.55.3  9..

88433-041:70   %03098905894:3/0789.55.094:7 3. 39074:73.203909-4.3/490.943478  490....0.20.943478  .4202088.438:-29-:9943 9/85.3/.55.

08890.9394&#995.034:.943-3....55...

.

489  ..4.

250.807.090.

09 995 995$07.. 807..05943 25479..20907 . 807.420$07.3/39071.0939071...09.4201030- 21084 0-807.. 807. 807. 807.0 995$07.0910.8808.089.09.0  0307.9903.83-748073/4  %03098905894:3/0789.420$07.$07......09!.870854380.4/0/0.09 .:9  034:190147210/...807.38./08 %%!850....09 $07.09.7. 807.4/02547990706:70/..88  WZ`W`[V 0930841.8 807.09 25479.9.09 $07.900- 807..94%%!57494.0995071472814439.09.94341$07.9090%/4.:2039.3//85.420$07.08.90890 706:089940..0890%/4.09#06:089 25479..08901472 9210 5..708900.2502039..090..420$07.09 995  1908094 5.2505.80...3/.09#06:0894-0.8808..70850.880.38.42243.88  0390706:089870. 4  ..88  O O O 97.0 0307.439. 807.07807.55330- 2 900-.090903/8995$07.88574.0.$07..8 807.9  0307.09#08543804-0.439..3/39071.090.1...70902489.94:300/942547930.10/. 807...5...88  5:-.7.09...3/39071.3/390...09.420$07.09 9955.09431 25479.. 807..439.09 995 995$07. 807.250 \[^`_W^bW`\SUSYW_ %0.07807.4202088.43889841945.4/0/0.8808. 807...0939071.0939071.438:-291472-:9943 -74807803/890%%!! $%706:089 93.09.09 $07..0859:-0 9:947.9-0903/3.7941859:-0 9:947...0.0. 807.089.09 $07..90900.092.09.3/90..88300/8942502039..095..3//0130/ 3..205.9  7480770..9.3/790907085438094995$07..1.08 90...09 995 995$07....02094/..07807.0  5.4/0  SUSYWVWUS^S`[Z 0.05943 25479..:2039.205. 807..03/70. 4 !7397907 25479..55.708 905.09 995 995$07.0  .703/0503/03941%%! 57494..09 $07.07807...8808...888.089810-/01.4 ..4  %030930841.0939071.4/0/0.7089039 2094/ 39 2094/8..3/39071.089.08  25479.09  W^bW`US__VWUS^S`[Z 093041.307/00.209071742995$07..0 7893041.4/0  &3/0789..09830-.081472 92108850.0.80/4390807.94325020398 ..0/-0.3/.09#0854380 %080..

.09 34:70..09 39 2094//4083 9/4 .431 < 39 2094/8:80/9439.393 9:89..431 9748$07.09.4/39 $07.05943 8:507 39 .5:-.88.8908:507.0784341902094/  W^bUW`W   .090807.09431.250807.

439.3/ 995$07.09#08543804-0.05943  .58:.70890/4!489 2094/ /4!489 2094/8/0130/3995$07.09.98.09#06:089706:089 995$07.58:.903..90890%%!70854380  57490.^W]aW_` 0930841..90/.09#0854380 70854380 9748$07.09..09#06:0894-0.8.88  /4!489 2094/8:80/94.943 .7:20398 995$07.30/390706:089 995$07.08995$0.4//4!489 995$07.4/0/0.9089031472..09#06:089.3/090%%!! $%706:089 %82094/9.903.09#08543804-0.05943-7.

.08903.88 995$07.80907/409 47/4!489 2094/-.8$973..209078 09!...20706:089 09!.20907 2094/9.09..7.20 0307.09.990.:0413.07:807.20907  $9733...43/079070834807.420088..9890.:809825020390/-995$07.7.8$973.250 988 909.439039950419070854380 34:90./08902502039.3/709:738 ..0 2094/.420 3.4/0097.0  $9730.09.88 %0.98900.8039070/394901472.7:2039.3807 8-0.0:80/! $%2094/34:7%1472 /4!489 8.88574.:0  %0309308.00.94341 807.0/   `^SU`^W]aW_`\S^SW`W^_X^[ ``\W^bW`W]aW_`[TWU` 4433041.7..43897:.4202088...90.20907 2094/41995$07.205.. 4:2..420$07.90#0854380%030930809890.0782509.20907 3.80/4390%%!706:0892094/  $3.:0411472 5.0 2094//0130/3900..204190147210/.7.7.20  09!.00.08:80/94097.09#06:08939071..

92  70854380 809439039%50 909.

884.039-7939490.09#08543804-0.3-0709:730/9490.92  #0854380.9  !73979074:970854380 097907 -7.3890!73979074-0..91742995$07..90/9 $07.9.. 4 !73979074-0.09#0854380  443304-9..

/  4:9 57393 990.078250807.090.4/0790890%949070854380  4:9 57393 92  4:9 57393 0. 44330841.250.

990  4:9 57393 .

/  4:9 57393 -4/  4:9 57393  0.0.0 .420088.

  4:9 57393 .701.

090.250.807.

08.5.

0 .070944 -..1472 92 .9435:95.

  4:9 57393 ..

-4/  .

4:9 57393 .

0/90/0542039/08.75947850...:/0890..09 $07.439.0910 ..3//0130/3..0788:993/43  WcWT W\[ WZ`VW_U^\`[^XW 0- 2108.0  0- .943414:70-.9  4:9 .04:941807.2085.02094/..4/3:80/  443308/0.70890/08974 2094/ 08974 2094/8. 807.75947 93..55238995.09389.3.807..4/0/0..708903.92  48090!73979074-0..55.04703 90807.480  08974 2094/0930841.078434190.10890.0/-900-..3/03.307037024.0939071.0  5:-.943  %0178930390/0542039/08.4//08974 < 089742094/8.3807.431:7..

.

.42... 8:3 .

2.

38.

.00 2388995...

.

  47.

 .

02.$.0 88.943995.3..4. 389.02.

.

8:3 .42....

2.

38.

00 995....

.

8:3 ..42...

2.

38.

...00.

200.09.09  807.07843   09890807.0983.09 3.420$07.55.0- .09 807.099.09/013943 ..420$07.9432:89-0/0130/3900- 210 :83807. 443308/01308900.55** 8/ .0-.07$07.

20 807.88859:-0 9:947.09 .8 807..090.250 0.09.420$07.807.09 3.

88 ..807.09 .

09 3.590&#894.09 2.108901:6:.807.88850.553/013943850.09 983..9073807.850.553  $07.990706:08914790&#.092.10/.1089.807.209.108903.204190807.09 $07.204190807.09  098807.883.09 2.208:80/.09 .5538:80/942.09 %01443807..553 $07.092.

09..3/0/- 0420$07.09 3.0984:/-0.200.88  807.09 2.420$07.0.420$07.553 807.09.

20 :7 5.99073.807.09 3.

0.420$07.09.

:7 5.99073 .

807.553 0.0:80/.09 2..

943.890.420$07.0.943.997-:90390%1472  14722094/! $%.09...:041..

09 $40390147288:-2990/ -74807803/890! $%706:089945.9.0.420$07.

8 9.0 8:. 0.3-0850.8.42010:83900.:2039 ..88  0980.09.0-.8.420$07.420 10 89002039390/0542039/08.95439394.4201089 35:-.09..943 1470.0.7084:7.3/0/-0..10/.3%47$!/4..75947 034:95090 &#5.42$07.250995.3.55.

.

.4.489  .

42010.307/85.250 900- .090.  .9.:942.807..8900.439.

108.443308850.

5.08.

.42010 $4034:.08890&# 995.8900.1472 92..

.

.4.489  .

075.307807.08.250 0-.807.439.090.

1472 9210  0.420 10 .420 10 89 0.

5.08.

1472 92 .

420 10 .0.

  .559.0.420 10 89 093003/8900- .

55   .0- .