Cookie中的三個容器request,session,application的設定和擷取

來源:互聯網
上載者: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(" <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的設定和擷取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.