Servlet & HTTP & request notes

clionlin 2022-05-22 11:57:45 阅读数:720

servlethttprequestnotes

Servlet:

1. Concept
2. step
3. Execution principle
4. Life cycle
5. Servlet3.0 Annotation configuration
6. Servlet Architecture of
Servlet -- Interface
|
GenericServlet -- abstract class
|
HttpServlet -- abstract class
* GenericServlet: take Servlet Other methods in the interface are implemented by default , Only will service() Methods as abstractions
* The future defines Servlet Class time , Inherit GenericServlet, Realization service() The method can
* HttpServlet: Yes http A kind of encapsulation of protocol , Simplified operation
1. Define class inheritance HttpServlet
2. make carbon copies doGet/doPost Method
7. Servlet Related configuration
1. urlpartten:Servlet Access path
1. One Servlet Multiple access paths can be defined : @WebServlet({"/d4","/dd4","/ddd4"})
2. Path definition rules :
1. /xxx: Path matching
2. /xxx/xxx: Multi layer path , Directory structure
3. *.do: Extension matching

HTTP:

* Concept :Hyper Text Transfer Protocol Hypertext transfer protocol
* Transfer protocol : Defined , When communicating between client and server , Format of data sent
* characteristic :
1. be based on TCP/IP High level agreement for
2. Default port number :80
3. Based on the request / In response to the model : One request corresponds to one response
4. Stateless : Each request is independent of each other , Can't interact with data
* Version history :
* 1.0: Each request response creates a new connection
* 1.1: Multiplexing connection
* Request message data format
1. Request line
Request mode request url Request protocol / edition
GET /login.html HTTP/1.1
* Request mode :
* HTTP The agreement has 7 In the request mode , Commonly used 2 Kind of
* GET:
1. The request parameter is on the request line , stay url after .
2. Requested url Of limited length
3. Not very safe.
* POST:
1. The request parameter is in the request body
2. Requested url Of unlimited length
3. A relatively safe
2. 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 , I access the browser version information you use
* The information of the header can be obtained on the server side , Solve browser compatibility problems
2. Referer:http://localhost/login.html
* Tell the server , I ( The current request ) Where are from ?
* effect :
1. Anti theft chain :
2. Statistical work :
3. Request a blank line
Blank line , Is used to split POST The request header of the request , And the request body .
4. Request body ( Text ):
* encapsulation POST Of the request parameter of the 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: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-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 format

Request:

1. request Objects and response The principle of objects
1. request and response Objects are created by the server . Let's use them
2. request Object to get the request message ,response Object to set the response message
2. request Object inheritance Architecture :
ServletRequest -- Interface
| Inherit
HttpServletRequest -- Interface
| Realization
org.apache.catalina.connector.RequestFacade class (tomcat)
3. request function :
1. Get request message data
1. Get request line data
* GET /day14/demo1?name=zhangsan HTTP/1.1
* Method :
1. Get request method :GET
* String getMethod()
2. (*) Get virtual directory :/day14
* String getContextPath()
3. obtain Servlet route : /demo1
* String getServletPath()
4. obtain get Mode request parameters :name=zhangsan
* String getQueryString()
5. (*) Get request URI:/day14/demo1
* String getRequestURI(): /day14/demo1
* StringBuffer getRequestURL() :http://localhost/day14/demo1
* URL: Uniform resource locator : http://localhost/day14/demo1 The People's Republic of China
* URI: Uniform resource identifiers : /day14/demo1 republic
6. Get the protocol and version :HTTP/1.1
* String getProtocol()
7. Get the... Of the client IP Address :
* String getRemoteAddr()
2. Get request header data
* Method :
* (*)String getHeader(String name): Get the value of the request header by the name of the request header
* Enumeration<String> getHeaderNames(): Get all request header names
3. Get request body data :
* Request body : Only POST Request mode , There's a requester , Encapsulate... In the request body POST Requested request parameters
* step :
1. Get stream object
* BufferedReader getReader(): Get character input stream , Only character data can be operated
* ServletInputStream getInputStream(): Get byte input stream , All types of data can be manipulated
* Explain after uploading the knowledge points of the file
2. Then take the data from the flow object
2. Other features :
1. Regardless of get still post The following methods can be used to obtain request parameters in both request methods
1. String getParameter(String name): Get the parameter value according to the parameter name username=zs&password=123
2. String[] getParameterValues(String name): Get the array of parameter values according to the parameter name hobby=xx&hobby=game
3. Enumeration<String> getParameterNames(): Get all requested parameter names
4. Map<String,String[]> getParameterMap(): Get... For all parameters map aggregate
* Chinese code scrambling :
* get The way :tomcat 8 Have already put get The problem of disorderly pattern has been solved
* post The way : It's messy
* solve : Before getting the parameters , Set up request The coding request.setCharacterEncoding("utf-8");
2. Request forwarding : A way to jump resources inside the server
1. step :
1. adopt request Object get request forwarder object :RequestDispatcher getRequestDispatcher(String path)
2. Use RequestDispatcher Object to forward :forward(ServletRequest request, ServletResponse response)
2. characteristic :
1. The browser address bar path does not change
2. It can only be forwarded to the internal resources of the current server .
3. Forwarding is a request
3. Shared data :
* Domain object : An object of scope , Data can be shared in scope
* request Domain : Represents the scope of a request , It is generally used to share data among multiple resources for request forwarding
* Method :
1. void setAttribute(String name,Object obj): Store the data
2. Object getAttitude(String name): Get value by key
3. void removeAttribute(String name): Remove key value pairs by key
* Be careful : Put this in forward On top of the method , Otherwise, too fast response will result in failure to obtain information !
4. obtain ServletContext:
* ServletContext getServletContext()
copyright:author[clionlin],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202203140552568349.html