cookie和session的使用
http://www.cnblogs.com/linguoguo/p/5106618.html
1:在控制器中添加session和cookie
@RequestMapping("/getindex") public String index(HttpServletRequest request){ Cookie cookie=new Cookie("name","liyafei"); HttpSession session = request.getSession(); session.setAttribute("name", "liyafei"); return "index"; }
2:jsp中打印出cookie和session
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> hello,index <% Cookie[] cookies=request.getCookies(); out.println(cookies[0].getValue()+"cookie"); out.println(session.getAttribute("name")+"session"); %> </body> </html>
3:结果
本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究