Content:

1. Servlet
2.The HTTP protocol3. Request
Copy the code

The Servlet:

1.concept2.steps3.All works4.The life cycle5. Servlet3. 0Note configuration * View above [HTTPS://blog.csdn.net/qq_43531919/article/details/107955080]
7.The architecture of the Servlet Servlet interface - | GenericServlet - abstract class | GenericServlet HttpServlet - abstract class * : In the future, when defining a Servlet class, GenericServlet can be inherited and implement the service() methodCopy the code
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/demo2")
public class ServletDemo2 extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("demo2....."); }}Copy the code
* HttpServlet: an encapsulation of the HTTP protocol to simplify operations1.Define classes that inherit HttpServlet2.Duplicate the doGet/doPost methodCopy the code
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("/demo3")
public class ServletDemo3 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet....");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost..."); }}Copy the code
8.Servlet Configuration1.Urlpartten :Servlet access path1.A Servlet can define multiple access paths: @webservlet ({"/d4"."/dd4"."/ddd4"})
		2.Path definition rules:1./ XXX: the path matches2./ XXX/XXX: multilayer path, directory structure3.*.do: the extension matches4. / *Copy the code
/** * Servlet path configuration */
//@WebServlet({"/d4","/dd4","/ddd4"})
//@WebServlet("/user/demo4")
//@WebServlet("/user/*")
//@WebServlet("/*")
@WebServlet("*.do")
public class ServletDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("demo4..."); System.out.println(req); }}Copy the code

HTTP:

Hyper Text Transfer Protocol Hyper Text Transfer Protocol Hyper Text Transfer Protocol Hyper Text Transfer Protocol Hyper Text Transfer Protocol Hyper Text Transfer Protocol Hyper Text Transfer Protocol1.TCP/ IP-based advanced protocol2.Default port number:80
		3.Request/response model-based: One request for one response4.Stateless: Each request is independent of each other and cannot exchange data * historical version: *1.0Each request response establishes a new connection1.1: Reuse connection * request message data format1.Request Line Request Mode Request URL Request Protocol/Version GET /login. HTML HTTP/1.1* Request mode: * HTTP7Request mode, commonly used are2* GET:1.The request parameters are in the request line, after the URL.2.The requested URL is limited in length3.Not very safe * POST:1.The request parameters are in the request body2.The requested URL length is unlimited3.A relatively safe2.Request header: The client browser tells the server some information request header name: Request header value * Common request headers:1.User-agent: the browser tells the server that I can access the version information of the browser you use * and obtain this header information on the server side to solve the compatibility problem of the browser2.Referer: HTTP://localhost/login.html* Tell the server, where did I (current request) come from? * action:1.Hotlinking prevention:2.Statistical work:3.Request blank line Blank line is used to separate the header of a POST request from the body of the request.4.Request body (body) : * Request parameters that encapsulate the POST request message * String format: POST /login.html HTTP/1.1
		Host: localhost
		User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
		Accept: text/html,application/xhtml+xml,application/xml; q=0.9, */ *; Q = 0.8 Accept - Language: useful - CN, useful; Q = 0.8, useful - TW; Q = 0.7, useful - HK; Q = 0.5, en - US; Q = 0.3, en. Q = 0.2 Accept - Encoding: gzip, deflate Referer: http://localhost/login.html Connection: Keep-alive upgrade-insecure -Requests: 1 username=zhangsan * Response message data formatCopy the code

Request:

1.Principle of Request object and Response object1.The Request and Response objects are created by the server. Let's use them2.The Request object is used to get the request message, and the Response object is used to set the response message2.Request object inheritance architecture: ServletRequest - interface | | inherit it - interface implementation org. Apache. Catalina. The RequestFacade class (tomcat implementation)3.Request:1.Gets the request message data1.GET /day14/demo1? name=zhangsan HTTP/1.1* method:1.To obtain a request: GET *String getMethod()  
				2.Get the virtual directory: /day14 *String getContextPath()
				3.Get the Servlet path: /demo1 *String getServletPath()
				4.Obtain get request parameters: name=zhangsan *String getQueryString()
				5.Get the request URI: /day14/demo1 *String getRequestURI():		/day14/demo1
					* StringBuffer getRequestURL()  :http://localhost/day14/demo1* URL: Uniform Resource Locator: HTTP://localhost/day14/demo1 PRC* URI: Uniform Resource Identifier (URI) :/day14/Not the republic of6.Obtain the protocol and version HTTP/1.1
					* String getProtocol()

				7.Get the IP address of the client: *String getRemoteAddr()
Copy the code
@WebServlet("/RequestDemo01")
public class RequestDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. Obtain the request from GET
        String method = request.getMethod();
        System.out.println(method);
        //2.(*) Obtain the virtual directory /day14
        String contextPath = request.getContextPath();
        System.out.println(contextPath);
        Get the Servlet path: /RequestDemo01
        String servletPath = request.getServletPath();
        System.out.println(servletPath);
        //4. Obtain get request parameters: name=zhangsan
        String queryString = request.getQueryString();
        System.out.println(queryString);
        (*) Obtain the request URI: /day14/RequestDemo01
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURI);
        System.out.println(requestURL);//http://localhost/day14/RequestDemo01
        //6. Obtain the protocol and version HTTP/1.1
        String protocol = request.getProtocol();
        System.out.println(protocol);
        //7. Obtain the client IP address: 127.0.0.1
        StringremoteAddr = request.getRemoteAddr(); System.out.println(remoteAddr); }}Copy the code