JSP requests, responses, cookies, and session HttpSession

Source: Internet
Author: User

1. Basic concepts of requests and responses in JSP 1.1: 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 method of request object: object getAttribute (String name) returns the Enumeration getAttributeNames () Attribute Value of the specified attribute. returns the enumerated String getCharacterEncoding () of all available attribute names. returns the character encoding method int getContentLength () returns the length of the Request body (in bytes) String getContentType (). obtains the MIME type of the Request body, ServletInputStream getInputStream (), and obtains the binary stream String getParameter (String name) of a row in the Request body) returns the parameter value Enumeration getParameterNames () of the specified name. returns the enumerated String [] getParameterValues (String name) of the available parameter names) Returns the String getProtocol () array containing all values of the parameter name. returns the request protocol type and version String getScheme (). returns the request scheduler name, for example, http. string getServerName (), such as https and ftp, returns the Host Name of the server that receives the request, int getServerPort (), and returns the port number BufferedReader getReader () used by the server to accept the request () returns the decoded Request body String getRemoteAddr () returns the Client IP address that sent the request String getRemoteHost () returns the client host name void setAttribute (String key, Object obj) that sent the request) set the attribute value String getRealPath (String path) to return the real path void setCharacter of a virtual path. Encoding (String charset) specifies the Request Encoding 1.3 Common method of response object String getCharacterEncoding () returns the type of character Encoding used in the response ServletOutputStream getOutputStream () returns a returned binary output stream PrintWriter getWriter (). returns an object void setContentLength (int len) that can output characters to the client. Sets the Response Header Length void setContentType (String type) set sendRedirect (java. lang. string location) redirect client requests 2 CookieCookie refers to the data (usually encrypted) stored on the user's local terminal by some websites to identify users and track sessions ). 2.1 Cookie creation Cookie [java] Cookie c = new Cookie ("name", "value"); where, name is the name of the created Cookie and will be used to read the Cookie in the future; value is the value of the Cookie object. Read Cookie [html] Cookie cookies [] = request. getCookies (); a Cookie array is obtained, which uses the for loop traversal: [java] for (int I = 0; I <cookies. length; I ++) System. out. println (cookies [I]. getValue (); add a Cookie to the client to put the cookie in the HTTP response Header. You can use the addCookie method [java] response of HttpServletResponse. addCookie (c); When deleting a Cookie, you only need to set the maximum Cookie validity period to 0 [java] for (int I = 0; I <cookies. length; I ++) {cookies [I]. setMaxAge (0); response. addCookie (cookies [I])} 3 session HttpSession3.1 common method of session www.2cto. comObject getAttribute (String name) Get the set attribute value based on name. Void setAttribute (String name, Object value) sets the session Object property value void removeAttribute (String name) to delete the name attribute Enumeration getAttributeName () of the session Object () return all attribute names bound to the current session long getCreationTime (). Return a long integer value long getLastAccessedTime () that indicates the last access date and time of the session. String getId () return session ID int getMaxInactiveInterval () returns the maximum number of seconds of session survival. Void setMasInactiveInterval (int seconds) sets the maximum lifetime.

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.