Servlet Package

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

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

The WEB-INF subdirectory contains the application's deployment descriptor, named web.xml. All HTML files are located in the top-level directorymyappbelow. For the admin user, 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. The directory structure where the class files are located matches their package names. For example, if you have a fully qualified class namecom.myorg.MyServlet, then the Servlet class must be located in the following directory:

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

The following example creates a package namedcom.myorgfor the MyServlet class.

// 为包命名
package com.myorg;  

// 导入必需的 java 库
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
  {
      // 执行必需的的初始化
      message = "Hello World";
  }
 
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // 设置响应内容类型
      response.setContentType("text/html;charset=UTF-8");
 
      // 实际的逻辑是在这里
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }
  
  public void destroy()
  {
      // 什么也不做
  }
}

Compiling a Servlet in a Package

Compiling classes in a package is not much different from compiling other classes. The simplest way is to keep your java files with fully qualified paths; for example, the class mentioned above will be kept in com.myorg. You also need to add that directory to the CLASSPATH.

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

$ javac MyServlet.java

If the Servlet depends on other libraries, you must also reference those JAR files in the CLASSPATH. Here I only referenced the servlet-api.jar JAR file because I didn't 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). In order for this command to work properly, you must include the location of the Java SDK you are using in the PATH environment variable.

If everything goes well, the above compilation will generate theMyServlet.classfile in the same directory. The next section will explain how to deploy a compiled Servlet to production.

Servlet Packaging and Deployment

By default, Servlet applications are located under <Tomcat-installation-directory>/webapps/ROOT, and class files are placed in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.

If you have a fully qualified class namecom.myorg.MyServlet, then the Servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class. You need to create the following entry 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>

The above entry 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 don't worry about them.

At this point, you are basically done. 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 the following in the browser's address bar:http://localhost:8080/MyServletIf everything goes well, you will see the following result:

Hello World

Other Extensions