Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Servlet package


May 15, 2021 Servlet


Table of contents


Servlet package

The web application structure that involves the WEB-INF subdirect directory 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 looks like this:

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

The WEB-INF subdirecter contains a deployment descriptor for the application, called the web .xml. A ll HTML files are located under the top-level directory myapp. For admin users, you'll find that the ROOT directory is the parent of myApp.

Create a servlet in the package

The WEB-INF/classes directory contains all the servlet classes and other class files, which are located in a directory structure that matches their package names. For example, if you have a fully qualified class name, com.myorg.MyServlet, the servlet class must be 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.

// 为包命名
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()
  {
      // 什么也不做
  }
}

Compile the servlet in the package

The classes in the compilation package are not much different from the other classes that are compiled. T he easiest way is to keep your java file with a fully qualified path, such as the class mentioned above, which will remain in com.myorg. Y ou also need to add the directory to CLASSPATH.

Assuming that your environment is set up correctly, go to the directory of the .lt; Tomcat-design-directory/webapps/ROOT/WEB-INF/classes and compile the MyServlet .java, as follows:

$ javac MyServlet.java

If the servlet depends on other libraries, you must also reference those JAR files in CLASSPATH. I only refer to the servlet-api .jar JAR file because I don't use any other libraries in the Hello World program.

The command line uses the built-in javac compiler, which comes with the Sun Microsystems Java Software Development Kit (JDK, full name Java Software Development Kit). M icrosystems' Java Software Development Kit (JDK). For this command to work properly, you must include the location of the Java SDK that you use in the PATH environment variable.

If all goes well, the compilation above generates a MyServlet file .class directory. The next section explains how to deploy a compiled servlet into production.

Servlet packaging deployment

By default, the Servlet application is located under the path-tomcat-design-directory/webapps/ROOT, and the class files are placed in the .lt;Tomcat-design-directory-gt;/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, and you will need to create the following entries in the web.xml file located in the web.xml

    <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 is to be created in .xml web-mail file. i nside the label. There may already be various entries available in this file, but don't care.

Now that you're basically done, let's start the tomcat server (on Windows.bat) or the tom http://localhost:8080/MyServlet cat server (on Linux/Solaris, etc.) using the .lt; Tomcat-design-directory.sh If all goes well, you'll see the following results:

Hello World