Java Servlet uses session

Source: Internet
Author: User

First servlet:

When the browser accesses the server for the first time, it assigns a session to it

        HttpSession session=request.getsession ();

Outputs the ID of the session and the number of times the session was used

Integer count= (integer) Session.getattribute ("Count"); if(count==NULL) {Count=NewInteger (1); }Else{Count++; } session.setattribute ("Count", Count);        Pw.println (Session.getid ()); Pw.println ("<a href=\ "tsservlet\" > linked to Tsservlet</a>"); Pw.println ("<tr><td> visited:<td>"+count+"\ n")

A second servlet:

Click the hyperlink from the first servlet to go to the servlet, then get its session and output the ID to see if it is equal to the sessionid of the previous servlet

        HttpSession hs=request.getsession ();        PrintWriter pw=response.getwriter ();        String msg=request.getparameter ("Ming");        PW.PRINTLN (msg);        Pw.println (Hs.getid ());

Results:

First servlet:

("AA" is the ID)

Output after refresh:

A second servlet:

The results show that:
Session successfully tracked (same ID, increased number of records accessed)

Java Servlet uses session

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.