Tutorial
Menus

Servlet - Servlet Basics

Deploying Servlet Using Tomcat

Rating: 4.7/5 (6 votes cast)

Level   : Beginners
Author : Arunkumar S
Download Source : Not Avaliable

Deploying Servlet Web Applications to Tomcat

Assume that we have:

  1. An application in a package named pack1
  2. The application consists of one Java class HelloWorld.java
  3. HelloWorld  is a Servlet, i.e. it extends HttpServlet class.
  4. Compile HelloWorld.java to get  HelloWorld.class

 
//HelloWorld.java 
public class HelloWorld extends HttpServlet { 

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HEAD><TITLE> www.JavaOrigin.com</TITLE></HEAD><BODY>");
out.println("<h1> HelloWorld - www.JavaOrigin.com</h1>");
out.println("<P>This is output is from HelloWorld.");
out.println("</BODY>");
out.close();
}
}

Deploying In Default Directory

  1. In TOMCAT_HOME\webapps\ create a directory named javaorigin (ex c:\Tomcat6.0\webapps\)
  2. In javaorigin create a directory named WEB-INF
  3. In WEB-INF create a directory named classes
  4. By now we have the following path: TOMCAT_HOME\webapps\javaorigin\WEB-INF\classes\
  5. Copy HelloWorld.class into classes directory.
  6.  Create  web.xml file and place  into TOMCAT_HOME\webapps\javaorigin\WEB-INF\ .
  7. Open web.xml file and type following servlet mapping tegs

   Copy the following text into the file:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

    <web-app>
    <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
    </web-app>

 Running

To test the Servlet, go to this URL in the browser http://localhost:8080/javaorigin/HelloWorld

This will run your Servlet. Notice that the use of the word servlet in the URL's path does not correspond to an actual directory.

Note

If the Servlet class did not have a package we would have copied it in TOMCAT_HOME\webapps\javaorigin\WEB-INF\classes\ , and would run it with the URL http://localhost:8080/javaorigin/HelloWorld

Deploying In Any Directory

 Preparing Directories

  1. Assume that we want to install the application in a directory named C:\jap1\
  2. In C:\javaorigin\ create a directory named WEB-INF
  3. In WEB-INF create a directory named classes
  4. By now we have the following path: C:\javaorigin\WEB-INF\classes\pack1\
  5. Copy HelloWorld.class into classes directory.

Modifying server.xml

  1. Using a text editor, open the file TOMCAT_HOME\conf\server.xml
  2. Find the </ContextManager> tag. (Note: this is an end tag)
  3. Directly in the line before it, insert the following:
  4. <Context path="/javaorigin"
    docBase="c:/javaorigin"
    crossContext="false"
    debug="0"
    reloadable="true" >
    </Context>
  5. Save and close the file.

This tells the server to map requests that start with /javaorigin (after the server name and port number) to the directory c:/javaorigin

   http://localhost:8080/javaorigin/HelloWorld
< Previous 1 | 2 | 3 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
Javaorigin.com contact@javaorigin.com