Servlet ---- cookie & session
2022/1/10 6:05:46
本文主要是介绍Servlet ---- cookie & session,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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>");
这篇关于Servlet ---- cookie & session的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)
- 2025-01-03停止思考数据管道,开始构建数据平台:介绍Analytics Engineering Framework
- 2025-01-03如果 Azure-Samples/aks-store-demo 使用了 Score 会怎样?
- 2025-01-03Apache Flink概述:实时数据处理的利器
- 2025-01-01使用 SVN合并操作时,怎么解决冲突的情况?-icode9专业技术文章分享
- 2025-01-01告别Anaconda?试试这些替代品吧
- 2024-12-31自学记录鸿蒙API 13:实现人脸比对Core Vision Face Comparator