Quickly create a servlet and configure and use it in web.xml

    This time, I'm going to teach you how to quickly create a servlet and configure and use it in web.xml  

    Let's first look at the basic structure of the project: 

forward from:


  • The first step is to create a servlet. Because a servlet is an interface, implements are required. 
    An example of a servlet created is as follows, since a servlet is an interface, some of its methods must be implemented.
destroy();  
 getServletConfig();
 getServletInfo();
 init();
 service();

    An example of the servlet created is as follows:

package com.hello.com;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FristServlet implements Servlet {

    public DeadlineServlet() {
        super();
        System.out.println("constructor");
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
    }

    @Override
    public ServletConfig getServletConfig() {
        System.out.println("getServletConfig");
        return null;
    }

    @Override
    public String getServletInfo() {
        System.out.println("getServletInfo");
        return null;
    }

    @Override
    public void init(ServletConfig arg0) throws ServletException {
        System.out.println("init");
    }

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        System.out.println("service");
    }

}
    After the creation, configure and map the servlet in the web.xml file. Only in this way can the browser access the servlet and interact with it. 
The configured web.xml file is as follows:     It should be noted that:      (1) The name of the servlet you want to configure is in the tag, and the package of the servlet you want to configure is located.   


  (2) The label filled in is the route accessed by your browser. For example, how to access this servlet here, you only need to enter http://localhost:8989/FristDemo/test in your browser.  The last test is your configuration The route, as for the previous http://localhost:8989/FristDemo  this is the default address of your project. [The port number 8989 here may be different from yours, because I changed the default startup port number of tomcat, which can be changed in tomcat's server.xml, and there are many online tutorials.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0" metadata-complete="true">
<!--Configure and map Servlet-->
<!--Configure Servlet-->
<servlet>
       <servlet-name>FristServlet</servlet-name>
       <servlet-class>com.hello.com.FristServlet</servlet-class>
</servlet>
<!--Map Servlet -- >
<servlet-mapping>  
       <servlet-name>FristServlet</servlet-name>  
       <url-pattern>/test</url-pattern>  
   </servlet-mapping>  
</web-app>

result of running

    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326976603&siteId=291194637