Cookie 属性 和 构造
Cookie的添加 和 获取
记录最后访问时间
@WebServlet(name = "ServletDemo25", value = "/ServletDemo25")
public class ServletDemo25 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
pw.write("欢迎访问本网站,您的最后访问时间为:<br>");
//创建Cookie对象,记录最后访问时间
Cookie cookie = new Cookie("time", System.currentTimeMillis() + "");
//设置最大存活时间 1h
cookie.setMaxAge(3600);
//将cookie对象添加到客户端
response.addCookie(cookie);
//获取cookie
Cookie[] arr = request.getCookies();
for (Cookie c : arr){
if ("time".equals(c.getName())){
//获取cookie对象中的value,进行写出
String value = c.getValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
pw.write(sdf.format(new Date(Long.parseLong(value))));
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
Cookie 使用细节
HttpSession
HttpSession实现类对象获取
@WebServlet(name = "ServletDemo26", value = "/ServletDemo26")
public class ServletDemo26 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数值
String username = request.getParameter("username");
//获取session对象
HttpSession session = request.getSession();
System.out.println(session);
System.out.println(session.getId());
//设置参数到session中
session.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
@WebServlet(name = "ServletDemo27", value = "/ServletDemo27")
public class ServletDemo27 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession();
System.out.println(session);
System.out.println(session.getId());
//获取共享数据
Object username = session.getAttribute("username");
//响应
response.getWriter().write(username + "");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
cookie禁用
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数值
String username = request.getParameter("username");
//获取session对象
HttpSession se1 = request.getSession(false);
HttpSession session = request.getSession();
System.out.println(session);
System.out.println(session.getId());
//设置参数到session中
session.setAttribute("username",username);
if (session != se1){
response.setContentType("text/heml;charset=UTF-8");
response.getWriter().write("为了不影响正常使用,请不要禁用浏览器的Cookie");
}
}
//实现url重写 相当于在地址栏后面拼接了jsessionid
response.getWriter().write("<a href='" + response.encodeURL("http://localhost/8080/session/servletDemo") + "'></a>");