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

Servlet instance


May 14, 2021 Servlet


Table of contents


Servlet instance

Servlet is a Java class that services HTTP requests and implements the javax.servlet.servlet interface. Web application developers typically write servlets to extend javax.servlet.http.httpservlets and implement abstract classes of the servlet interface specifically for handling HTTP requests.

Hello World sample code

Here's an example source code for servlet output Hello World:

// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// 扩展 HttpServlet 类
public class HelloWorld 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

Let's write the code above in the HelloWorld.java file, put it in C: sservletDevel (on Windows) or /usr/servletDevel (on UNIX), and you'll need to add these directories to CLASSPATH.

Suppose your environment is set up correctly, go to the ServletDevel directory, and compile helloworld .java, as follows:

$ javac HelloWorld.java

If the servlet relies on any other libraries, you must include those JAR files in CLASSPATH. Here, I only include the servlet-api .jar JAR file because I don't use any other libraries in the Hello World program.

The command line uses the javac compiler built into the Sun Microsystems Java Software Development Kit (JDK). For this command to work properly, you must locate the Java SDK used in the PATH environment variable.

If all goes well, the compilation above generates HelloWorld files .class directory. The next section explains how compiled servlets are deployed in production.

Servlet 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 the WEB-INF/classes/com/myorg/MyServlet .class.

Now, let's copy the HelloWorld.class to the webapps/ROOT/ROOT/WEB-INF/classes and create the following entries in the web.xml file located in the .lt;Tomcat-design-directory-gt;/webapps/ROOT/WEB-INF/.xml file:

    <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>

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 using the .lt; Tomcat-design-directory.bat (on Windows) or the .lt;Tomcat-design-directory.sh (on Linux/Solaris, etc.) and finally enter http://localhost:8080/HelloWorld in the browser's address bar. If all goes well, you'll see the following results:

Servlet instance