1. Basic concepts of request and response 1.1 in JSP
Request: when a user enters a URL in a browser and submits or submits a form, a request is created and sent to the server.
Response: when the server receives a user's request, it performs corresponding processing based on the user's specific request and returns the operation result to the user.
1.2 common methods for request objects
- Object getattribute (string name) returns the attribute value of the specified attribute.
- Enumeration getattributenames () returns the enumeration of all available attribute names
- String getcharacterencoding () returns the character encoding method
- Int getcontentlength () returns the length of the Request body (in bytes)
- String getcontenttype () Get the MIME type of the Request body
- Servletinputstream getinputstream () obtains the binary stream of a row in the Request body.
- String getparameter (string name) returns the parameter value of the specified name parameter.
- Enumeration getparameternames () returns the enumeration of available parameter names
- String [] getparametervalues (string name) returns an array containing all values of the parameter name
- String getprotocol () returns the protocol type and version number of the request.
- String getscheme () returns the scheduler name used for the request, such as HTTP. HTTPS and FTP.
- String getservername () returns the server host name that receives the request
- Int getserverport () returns the port number used by the server to accept this request.
- Bufferedreader getreader () returns the decoded Request body.
- String getremoteaddr () returns the IP address of the client sending this request.
- String getremotehost () returns the client host name that sends this request
- Void setattribute (string key, object OBJ)
- String getrealpath (string path) returns the actual path of a virtual path.
- Void setcharacterencoding (string charset) specifies the Request Encoding
1.3 Common Methods for response objects
- String getcharacterencoding () character encoding used to return the response
- Servletoutputstream getoutputstream () returns a binary output stream of the response.
- Printwriter getwriter () returns an object that can output characters to the client.
- Void setcontentlength (INT Len) set the Response Header Length
- Void setcontenttype (string type) sets the MIME type of the response
- Sendredirect (Java. Lang. string location) Redirects client requests
2 cookie
Cookies are the data (usually encrypted) stored on the user's local terminal by websites to identify users and track sessions ).
2.1 use of cookies
Cookie c= new Cookie("name","value");
Here, name is the name of the created cookie, which will be used to read the cookie in the future; value is the value of the cookie object.
Cookie cookies[] = request.getCookies();
The returned result is a cookie array that uses the for loop traversal:
for(int i=0;i<cookies.length;i++)System.out.println(cookies[i].getValue());
Put the cookie into the HTTP Response Header. You can use the addcookie method of httpservletresponse.
response.addCookie(c);
When deleting a cookie, you only need to set the maximum validity period of the cookie to 0.
for(int i=0;i<cookies.length;i++){cookies[i].setMaxAge(0);response.addCookie(cookies[i])}
3. Common Methods for session httpsession3.1
- Object getattribute (string name) obtains the set attribute value based on name.
- Void setattribute (string name, object value)
- Void removeattribute (string name): deletes the name attribute of the session object.
- Enumeration getattributename () returns all attribute names bound to the current session
- Long getcreationtime () indicates a long integer value created by the session.
- Long getlastaccessedtime () is a long integer value of the last access date and time.
- String GETID () returns the session ID
- Int getmaxinactiveinterval () returns the maximum number of seconds for session survival.
- Void setmasinactiveinterval (INT seconds) sets the maximum lifetime.
Exchange to explore my Sina Weibo: http://weibo.com/tianrui1990