1 of 12

Deploying the Servlet

2 of 12

Steps to execute the servlet

  • Create a directory structure
  • Create a Servlet
  • Compile the Servlet
  • Create a deployment descriptor
  • Start the server and deploy the project
  • Access the servlet

3 of 12

1)Create a directory structure

  • The directory structure defines that where to put the different types of files so that web container may get the information and respond to the client.
  • The Sun Microsystems defines a unique standard to be followed by all the server vendors.

4 of 12

2)Create a Servlet

There are three ways to create the servlet.

  • By implementing the Servlet interface
  • By inheriting the GenericServlet class
  • By inheriting the HttpServlet class

Note: The HttpServlet class is widely used to create the servlet because it provides methods to handle http requests such as doGet(), doPost, doHead() etc.

In this example we are going to create a servlet that extends the HttpServlet class. In this example, we are inheriting the HttpServlet class and providing the implementation of the doGet() method. Notice that get request is the default request.

5 of 12

Demo.java

import javax.servlet.http.*;  

import javax.servlet.*;  

import java.io.*;  

public class DemoServlet extends HttpServlet{  

public void doGet(HttpServletRequest req,HttpServletResponse res)  

throws ServletException,IOException  

{  

res.setContentType("text/html");//setting the content type  

PrintWriter pw=res.getWriter();//get the stream to write the data  

  

//writing html in the stream  

pw.println("<html><body>");  

pw.println("Welcome to servlet");  

pw.println("</body></html>");  

  

pw.close();//closing the stream  

}}  

In this example we are going to create a servlet that extends the HttpServlet class. In this example, we are inheriting the HttpServlet class and providing the implementation of the doGet() method. Notice that get request is the default request.

6 of 12

3)Compile the servlet

  • For compiling the Servlet, jar file is required to be loaded. Different Servers provide different jar files:

1) servlet-api.jar🡪Apache Tomcat

2) weblogic.jar🡪Weblogic

3) javaee.jar🡪Glassfish

4) javaee.jar🡪Jboss

  • Two ways to load the jar file

1. set classpath

2. paste the jar file in JRE/lib/ext folder

Put the java file in any folder. After compiling the java file, paste the class file of servlet in WEB-INF/classes directory.

7 of 12

4)Create the deployment descriptor (web.xml file)

  • The deployment descriptor is an xml file, from which Web Container gets the information about the serlvet to be invoked.
  • The web container uses the Parser to get the information from the web.xml file. There are many xml parsers such as SAX, DOM and Pull.
  • There are many elements in the web.xml file. Here is given some necessary elements to run the simple servlet program

8 of 12

Web.xml

<web-app>  

  

<servlet>  

<servlet-name>sonoojaiswal</servlet-name>  

<servlet-class>DemoServlet</servlet-class>  

</servlet>  

  

<servlet-mapping>  

<servlet-name>sonoojaiswal</servlet-name>  

<url-pattern>/welcome</url-pattern>  

</servlet-mapping>  

  

</web-app>  

9 of 12

  • There are many elements in the web.xml file. Here is the illustration of some elements that is used in the above web.xml file. The elements are as follows:

<web-app> represents the whole application.

<servlet> is sub element of <web-app> and represents the servlet.

<servlet-name> is sub element of <servlet> represents the name of the servlet.

<servlet-class> is sub element of <servlet> represents the class of the servlet.

<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.

<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to invoke the servlet.

10 of 12

5)Start the Server and deploy the project

  • To start Apache Tomcat server, double click on the startup.bat file under apache-tomcat/bin directory.
  • One Time Configuration for Apache Tomcat Server
  • You need to perform 2 tasks:
  • set JAVA_HOME or JRE_HOME in environment variable (It is required to start server).
  • Change the port number of tomcat (optional). It is required if another server is running on same port (8080).

11 of 12

1) How to set JAVA_HOME in environment variable

  • To start Apache Tomcat server JAVA_HOME and JRE_HOME must be set in Environment variables.
  • Go to My Computer properties -> Click on advanced tab then environment variables -> Click on the new tab of user variable -> Write JAVA_HOME in variable name and paste the path of jdk folder in variable value -> ok -> ok -> ok.
  • After setting the JAVA_HOME double click on the startup.bat file in apache tomcat/bin.Note: There are two types of tomcat available:Apache tomcat that needs to extract only (no need to install)
  • Apache tomcat that needs to install

12 of 12

5) How to deploy the servlet project

  • Copy the project and paste it in the webapps folder under apache tomcat.�

6) How to access the servlet

Open broser and write http://hostname:portno/contextroot/urlpatternofservlet. For example:

http://localhost:9999/demo/welcome