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