@[TOC]

Servlet writing

There are three ways to implement Servlet functionality:

1. The first: Servlet interface, interface methods must be all implemented.

In this way, all methods in the interface need to be overridden on the requirements side. This approach allows for maximum customization (true personalization, I understand).

2. The second: GenericServlet,

Inheriting GenericServlet, the service method must be overridden so that others can optionally override it as required.

Using this approach, we can only override requirements for receiving and responding to client requests, while other methods can be selectively rewritten based on actual requirements, making our development servlets easy. However, this approach is independent of the HTTP protocol.

For example, the following overrides the service method

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    HttpServletRequest request;
    HttpServletResponse response;
    try {
        request = (HttpServletRequest)req;
        response = (HttpServletResponse)res;
    } catch (ClassCastException var6) {
        throw new ServletException("non-HTTP request or response");
    }

    this.service(request, response);
}

Copy the code

The service source

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String method = req.getMethod();
    long lastModified;
    if (method.equals("GET")) {
        lastModified = this.getLastModified(req);
        if (lastModified == -1L) {
            this.doGet(req, resp);
        } else {
            long ifModifiedSince;
            try {
                ifModifiedSince = req.getDateHeader("If-Modified-Since");
            } catch (IllegalArgumentException var9) {
                ifModifiedSince = -1L;
            }

            if (ifModifiedSince < lastModified / 1000L * 1000L) {
                this.maybeSetLastModified(resp, lastModified);
                this.doGet(req, resp);
            } else {
                resp.setStatus(304); }}}else if (method.equals("HEAD")) {
        lastModified = this.getLastModified(req);
        this.maybeSetLastModified(resp, lastModified);
        this.doHead(req, resp);
    } else if (method.equals("POST")) {
        this.doPost(req, resp);
    } else if (method.equals("PUT")) {
        this.doPut(req, resp);
    } else if (method.equals("DELETE")) {
        this.doDelete(req, resp);
    } else if (method.equals("OPTIONS")) {
        this.doOptions(req, resp);
    } else if (method.equals("TRACE")) {
        this.doTrace(req, resp);
    } else {
        String errMsg = lStrings.getString("http.method_not_implemented");
        Object[] errArgs = new Object[]{method};
        errMsg = MessageFormat.format(errMsg, errArgs);
        resp.sendError(501, errMsg); }}Copy the code

3. Third: Inherit HttpServle

Inheriting HttpServlet, which is an abstract class under the javax.servlet. HTTP package and is a subclass of GenericServlet. If we choose to inherit HttpServlet, we only need to override the doGet and doPost methods, not the Service methods.

Using the third approach, we indicate that our request and response needs are HTTP protocol dependent. In other words, we are accessing through HTTP protocol. Each request and response complies with the HTTP protocol specification. The request method is the one supported by HTTP. Currently, we only know GET and POST, but actual HTTP supports seven request methods: GET, POST, PUT, DELETE TRACE OPTIONS HEAD.

Such as:

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//@WebServlet(name = "thirdServlet", urlPatterns = "/servlet")
// We can simplify it even further, as we find that the name attribute does not seem to be useful
//@WebServlet(urlPatterns = "/servlet")
// If there is only one value in parentheses, the default is urlPattern
@WebServlet("/servlet")
public class ThirdServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}}Copy the code