标签:
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(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String info = name+","+pwd;
//把信息分别存储到3个容器中
request.setAttribute("info", info+" ---Request");
request.getSession().setAttribute("info", info+" ---Session"); //JSP页面中的隐藏对象: session
getServletContext().setAttribute("info", info+" ---Application"); //JSP页面中的隐藏对象: application
//一个小功能:实现让用户关闭浏览器之后,10分钟之内身份还有效
//本质上就是向客户端写一个名为JSESSIONID的cookie--有效期为10分钟
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(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
//把信息分别从3个容器中读取出来
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">
姓名:<input type="text" name="name"/> <br/>
密码:<input type="password" name="pwd"/> <br/>
<input type="submit" value="信息存储到3个容器"/>
</form>
<a href="GetServlet">显示3个容器中的信息</a><br/>
-----------------------------------------------------------------------------------------------------------------------------------------
显示结果:request中是null而session跟application中有内容,可以那上面的代码测试一下;
session中的安全退出代码:
HttpSession session = request.getSession();
session.invalidate();//让该session对象失效
out.println("已经安全退出");
//seesion是在网页中,而application是在服务器中,在不同的浏览器中session不同
Cookie中的三个容器request,session,application的设置和获取
标签:
原文地址:http://www.cnblogs.com/1314wamm/p/5951591.html