JSP Basics? Cookie and session (v)

Source: Internet
Author: User

JSP Cookie Processing

  Cookies are text files that are stored in the client computer, and they hold a large amount of trace information. On the basis of servlet technology, JSP is clearly able to provide support for HTTP cookies. The JSP script accesses these cookies through the GetCookies () method in the Request object, which returns an array of Cookie objects.

1、 Public voidSetDomain (String pattern) to set the domain name of the cookie, such as w3cschool.cc2、 PublicString GetDomain () gets the domain name of the cookie, such as w3cschool.cc3、 Public voidSetmaxage (intexpiry) Set the cookie validity period, in seconds, by default to the current session's survival time4、 Public intGetmaxage () Gets the cookie validity period, in seconds, by default-1, indicating that the cookie will live until the browser is closed5、 PublicString GetName () returns the name of the cookie that cannot be modified after the name is created6、 Public voidSetValue (String newvalue) Setting the value of the cookie7、 PublicString GetValue () Gets the value of the cookie8、 Public voidSetPath (String URI) sets the path of the cookie, defaults to all URLs under the current page directory, and all subdirectories under this directory9、 PublicString GetPath () Gets the path of the cookieTen、 Public voidsetsecure (Boolean flag) indicates whether the cookie is to be encrypted for transmission One、 Public voidsetcomment (String purpose) Sets the comment to describe the purpose of the cookie. Annotations become useful when the browser presents a cookie to the user A、 PublicString getcomment () returns a comment describing the purpose of the cookie, or null if not

  Set cookies, you can complete the cookie by Response.addcookie (cookie), another cookie.setmaxage (60), set the cookie expiration date;

JSP Session Processing

  The JSP uses the HttpSession interface provided by the servlet to identify a user and store all of the user's access information. By default, the JSP allows session tracing, and a new HttpSession object is automatically instantiated for the new client. It is forbidden to explicitly turn off the session trace by setting the value of the session property to false in the page directive;

JSP session encapsulates a number of common methods to process session data

1、 PublicObject getattribute (String name) returns the objects in the session object that are bound to the specified name, and returns null if it does not exist2、 Publicenumeration Getattributenames () returns all object names in the Session object3、 Public LongGetCreationTime () returns the time, in milliseconds, that the session object was created, starting from the early hours of January 1, 19704、 PublicString getId () returns the ID of the session object5、 Public LongGetlastaccessedtime () returns the last time the client was accessed, in milliseconds, starting from the early hours of January 1, 19706、 Public intGetmaxinactiveinterval () returns the maximum time interval, in seconds, in which the servlet container will remain open for the duration of the session7、 Public voidinvalidate () invalidates the session and unbind any object bound to the session8、 Publicboolean isnew () returns whether it is a new client, or whether the client refuses to join the session9、 Public voidremoveattribute (String name) removes an object with the name specified in the sessionTen、 Public voidSetAttribute (String name, object value) uses the specified name and value to produce an object and bind to the session One、 Public voidSetmaxinactiveinterval (intinterval) is used to specify the time, in seconds, that the servlet container will keep the session valid for that period of time

To configure the Web. xml file:

If you are using Tomcat, you can configure the Web. xml file as follows:

<session-config>    <session-timeout></session-timeout>  </session-config >

JSP Basics? Cookie and session (v)

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.