current position:Home>JavaWeb Tomcat (III) httpservlet

JavaWeb Tomcat (III) httpservlet

2022-04-29 13:05:48YUELEI118

HttpServlet Inheritance structure

  • Really develop webapp when , Created Servlet Inheritance is HttpServlet class
  • Have a look first HttpServlet Class inheritance structure

HttpServlet Class inherited GenericServlet abstract class

public abstract class HttpServlet extends GenericServlet

and GenericServlet Class implements the Servlet, ServletConfig, java.io.Serializable Interface

public abstract class GenericServlet implements Servlet, ServletConfig,
        java.io.Serializable 

 Insert picture description here

HttpServlet Execution steps

  • from Tomcat Create a Servlet Start looking HttpServlet The calling steps of the methods in the class , For the convenience of narration , Inherit HttpServlet Class HelloHttpServlet Class is what you want to create Servlet.
public class HelloHttpServlet extends HttpServlet

1. Users access... Through a browser webapp

2. Tomcat Will analyze the resource address in the browser . adopt Web.xml The configuration file finds the... That needs to be created Servlet class .

3. Tomcat establish HelloHttpServlet Class object , And will Web.xml The configuration file is passed to ServletConfig

4. perform init Method .

effect : Pass in the configuration file to the global variable config

	//HttpServlet Code in a class 
    @Override
    public void init(ServletConfig config) throws ServletException {
    
        this.config = config;
        this.init();
    }

5. Above code execution this.init()

If HelloHttpServlet Class inherits the method , Will perform HelloHttpServlet Class init()

	//HttpServlet Code in a class 
    public void init() throws ServletException {
    
        // NOOP by default
    }

6. Tomcat Continue to call service(ServletRequest req, ServletResponse res)

effect : Cast the passed in parameter to , Continue to call service Method and pass the converted variable into

	//HttpServlet Code in a class 
    @Override
    public void service(ServletRequest req, ServletResponse res)
        throws ServletException, IOException {
    

        HttpServletRequest  request;
        HttpServletResponse response;

        try {
    
            request = (HttpServletRequest) req;
            response = (HttpServletResponse) res;
        } catch (ClassCastException e) {
    
            throw new ServletException(lStrings.getString("http.non_http"));
        }
        // Called service(HttpServletRequest req, HttpServletResponse resp)
        service(request, response);
    }

7. Called at the end of the above code service(request, response)

effect : Get the request type in the user request , Execute the corresponding method according to the type .

	//HttpServlet Code in a class 
    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    

        String method = req.getMethod();

        if (method.equals(METHOD_GET)) {
    
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
    
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
    
                long ifModifiedSince;
                try {
    
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } catch (IllegalArgumentException iae) {
    
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
    
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
    
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }

        } else if (method.equals(METHOD_HEAD)) {
    
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);

        } else if (method.equals(METHOD_POST)) {
    
            doPost(req, resp);

        } else if (method.equals(METHOD_PUT)) {
    
            doPut(req, resp);

        } else if (method.equals(METHOD_DELETE)) {
    
            doDelete(req, resp);

        } else if (method.equals(METHOD_OPTIONS)) {
    
            doOptions(req,resp);

        } else if (method.equals(METHOD_TRACE)) {
    
            doTrace(req,resp);

        } else {
    
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //

            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);

            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }

8. stay HelloHttpServlet Class to override the corresponding method ,

When overriding a method , Make it clear that the user is sending GET、POST Or requested by . You can't write all response types .
If the user sends the wrong type of request ,HttpServlet Returns the 405 error ( Wrong request type ).

     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
          //Todo:
     }

     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
          //Todo:
     }

copyright notice
author[YUELEI118],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291138177663.html

Random recommended