Settings and acquisition of three container request,session,application in a cookie

Source: Internet
Author: User

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

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.