Cookie中的三個容器request,session,application的設置和獲取

 

public class SaveServlet extends HttpServlet {html

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();
}服務器

}cookie

----------------------------------------------------------------------------------------------------------------------------session

public class GetServlet extends HttpServlet {app

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}post

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();
}orm

}

--------------------------------------------------------------------------------------------------------------------------------------

<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不一樣

相關文章
相關標籤/搜索