3-2 request, response, cookie, and session httpsession in JSP

Source: Internet
Author: User
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
  • Create cookie
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.

  • Read cookie
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()); 

 

  • Add cookie to client

Put the cookie into the HTTP Response Header. You can use the addcookie method of httpservletresponse.

response.addCookie(c);

 

  • Delete cookie

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.