public class Saveservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (request, response);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
Out.println ("<HTML>");
Out.println ("Out.println ("<BODY>");
Request.setcharacterencoding ("Utf-8");
String name = Request.getparameter ("name");
String pwd = request.getparameter ("pwd");
String info = name+ "," +PWD;
Store information separately in 3 containers
Request.setattribute ("info", info+ "---Request");
Request.getsession (). SetAttribute ("info", info+ "---Session"); Hidden objects in JSP pages: Session
Getservletcontext (). SetAttribute ("info", info+ "---application"); Hidden objects in JSP pages: application
A small feature: after enabling the user to close the browser, the identity is still valid within 10 minutes
Essentially, writing a cookie--named Jsessionid to the client is valid for 10 minutes.
String id = request.getsession (). GetId ();
Cookie C = new Cookie ("Jsessionid", id);
C.setmaxage (60*10);
C.setpath (Request.getcontextpath ());
Response.addcookie (c);
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}
}
--------------------------------------------------------------------------------------------------------------- -------------
public class Getservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (request, response);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
Out.println ("<HTML>");
Out.println ("Out.println ("<BODY>");
Request.setcharacterencoding ("Utf-8");
Read the information from 3 containers, respectively.
String reqinfo = (string) request.getattribute ("info");
String sessioninfo = (string) request.getsession (). getattribute ("info");
String appInfo = (string) getservletcontext (). getattribute ("info");
Out.println ("Reqinfo:" + reqinfo);
Out.println ("Sessioninfo:" + sessioninfo);
Out.println ("AppInfo:" + appInfo);
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}
}
--------------------------------------------------------------------------------------------------------------- -----------------------
<form action= "Saveservlet" method= "POST" >
Name: <input type= "text" name= "name"/> <br/>
Password: <input type= "password" name= "pwd"/> <br/>
<input type= "Submit" value= "information stored in 3 containers"/>
</form>
<a href= "Getservlet" > Display information in 3 containers </a><br/>
--------------------------------------------------------------------------------------------------------------- --------------------------
Show results: The request is null and the session with the application content, you can test the above code;
Safe exit code in session:
HttpSession session = Request.getsession ();
Session.invalidate ();//To invalidate the Session object
Out.println ("has been safely exited");
Seesion is in a Web page, while application is in the server, the session is different in different browsers
Settings and acquisition of three container request,session,application in a cookie