session:例子代码(一个session为一个会话而服务)

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpSession session=request.getSession();
String id=session.getId();
Cookie cookie=new Cookie("JSESSIONID",id);
cookie.setPath("/servletdemo");
cookie.setMaxAge(1*60*30);
response.addCookie(cookie);
session.setAttribute("name","洗衣机");

}

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

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw=response.getWriter();
HttpSession session= request.getSession();
String product=(String) session.getAttribute("name");
pw.write("购买的商品是:"+product);

}

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

<body>
<a href ="/servletDemo/servlet/Response5">查看图书</a>
<a href ="/servletdemo/servlet/session1">购买</a>
<a href ="/servletdemo/servlet/Session2">结账</a>
<br>
</body>

posted @ 2017-03-24 09:16  贱贱的小帅哥  阅读(208)  评论(0编辑  收藏  举报