Cookie:
- 浏览器保存在本地的文本数据(数据保存在本地)
- 常用于保存登录状态,用户资料等小文本
- 具有时效性,内容伴随请求发送给tomcat
- cookie默认有效期为当前浏览器窗口,浏览器被关闭cookie消失
Cookie cookie=new Cookie("user","admin");
//设置最大有效期为7天
cookie.setMaxAge(60*60*24*7);
例子:
设置cookie
UlikemeLoginServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("用户登录成功");
Cookie cookie=new Cookie("user","admin");
//设置最大有效期为7天
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie); //保存cookie
response.getWriter().println("login success");
}
读取cookie:
UlikemeIndexServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取所有cookie信息
Cookie cs[]= request.getCookies();
String user=null;
for(Cookie c:cs) {
System.out.println(c.getName()+"--"+c.getValue());
if (c.getName().equals("user")) {
user=c.getValue();
break;
}
}
if(user==null) {
response.getWriter().println("user not login");
}else {
response.getWriter().println("user has login,user:"+user);
}
}
浏览器输入:http://localhost:9999/servlet-advance/cookies/index
控制台显示
Session:
- 保存与浏览器窗口对应的数据(数据保存在服务器端)
- 数据存储在tomcat内存中,有时效性,默认为30min
- Session通过浏览器Cookie的sessionId值提取用户数据
SessionLoginServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("用户登录成功");
//获取到用户会话Session对象
HttpSession session= request.getSession();
String sessionId=session.getId();
System.out.println(sessionId);
session.setAttribute("name", "jack");
request.getRequestDispatcher("/session/index").forward(request, response);
}
SessionIndexServlet.jsp
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session=request.getSession();
String sessionId=session.getId();
System.out.println(sessionId);
String name=(String) session.getAttribute("name");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("这是首页,当前用户是:"+name);
}
ServletContent对象:
SerevletContextInitServlet .java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context= request.getServletContext();
context.setAttribute("copyright", "@ 2021 ulikeme.com ");
context.setAttribute("title", " hello my web ");
response.getWriter().println("init success");
}
ServletContextDefaultServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = (ServletContext) request.getServletContext();
String copyright = (String) context.getAttribute("copyright");
String title = (String) context.getAttribute("title");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("<h1>"+copyright+"</h1>"+ title);
}
java web 三大作用域对象
- HttpServletRequest:请求对象
- HttpSession:用户会话对象
- ServletContext:web应用全局对象
(作用域和生命周期依次递增)