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 named myapp, the directory structure looks like this:

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

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

Create the Servlet in the 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 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 package named The MyServlet class of com.myorg.

// 为包命名
package com.myorg;  

// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
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");
 
      // 实际的逻辑是在这里
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }
  
  public void destroy()
  {
      // 什么也不做
  }
}

Compiling the Servlet in the package

There is no big difference between compiling the classes in the package and compiling other classes. The easiest way is to keep your java files with fully qualified paths, like the classes mentioned above, will be kept in com.myorg. You also need to add the directory to CLASSPATH.

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

$ javac MyServlet.java

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

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

If everything goes well, the above compilation will generate the MyServlet.class file in the same directory. The next section explains 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 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>

The above entries are to be created in the < ;web-app>...</web-app> tags. There may already be various entries available in this file, but don't care.

At this point, you're basically done, now let's use <Tomcat-installation-directory>\bin\startup.bat (on Windows) or <Tomcat-installation-directory>/bin /startup.sh (on Linux/Solaris, etc.) to start the tomcat server, and finally enter http://localhost:8080/MyServlet in the browser's address bar. If all goes well, you will see the following results:

Hello World