Easy Tutorial
❮ Servlet Auto Refresh Servlet Internationalization ❯

Servlet Package

The structure of a web application involving the WEB-INF subdirectory is standard for all Java web applications and is specified by the Servlet API. Given a top-level directory named myapp, the directory structure is as follows:

/myapp
    /images
    /WEB-INF
        /classes
        /lib

The WEB-INF subdirectory contains the deployment descriptor of the application, named web.xml. All HTML files are located under the top-level directory myapp. For admin users, you will find that the ROOT directory is the parent directory of myApp.

Creating a Servlet in a Package

The WEB-INF/classes directory contains all Servlet classes and other class files, with the directory structure matching their package names. For example, if you have a fully qualified class name com.myorg.MyServlet, then this Servlet class must be located in the following directory:

/myapp/WEB-INF/classes/com/myorg/MyServlet.class

The following example creates a MyServlet class with the package name com.myorg.

// Naming the package
package com.myorg;  

// Importing necessary Java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {

  private String message;

  public void init() throws ServletException
  {
      // Perform necessary initialization
      message = "Hello World";
  }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html;charset=UTF-8");

      // Actual logic goes here
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }

  public void destroy()
  {
      // Do nothing
  }
}

Compiling a Servlet in a Package

Compiling a class in a package is not much different from compiling other classes. The simplest method is to keep your Java file in the fully qualified path, as mentioned above, it will be kept in com.myorg. You also need to add this directory to your CLASSPATH.

Assuming your environment is set up correctly, navigate to the **<Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes** directory and compile MyServlet.java as follows:

$ javac MyServlet.java

If the Servlet depends on other libraries, you must also reference those JAR files in your CLASSPATH. Here, I only referenced the servlet-api.jar JAR file because I did not use any other libraries in the Hello World program.

This command line uses the built-in javac compiler, which comes with the Sun Microsystems Java Software Development Kit (JDK). For this command to work, the location of the Java SDK you are using must be included in your PATH environment variable.

If everything goes smoothly, the above compilation will generate the MyServlet.class file in the same directory. The next section will explain how to deploy a compiled Servlet into production.

Servlet Packaging and Deployment

By default, the Servlet application is located under the path <Tomcat-installation-directory>/webapps/ROOT, and the class files are placed in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes. If you have a fully qualified class name com.myorg.MyServlet, then this Servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class. You need to create the following entries in the web.xml file located at <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/:

<servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.myorg.MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>

These entries should be created within the <web-app>...</web-app> tags in the web.xml file. There may already be various entries available in the file, but do not be concerned about them.

At this point, you have essentially completed the setup. Now, let's start the Tomcat server using <Tomcat-installation-directory>\bin\startup.bat (on Windows) or <Tomcat-installation-directory>/bin/startup.sh (on Linux/Solaris, etc.), and finally, enter http://localhost:8080/MyServlet in the address bar of your browser. If everything goes smoothly, you will see the following result:

| Hello World |

❮ Servlet Auto Refresh Servlet Internationalization ❯