@WebServlet(urlPatterns = "/hello") public class HelloServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().append("Hello world!" + " " + req.getMethod()); HttpSession session = req.getSession(); if (session.getAttribute("name") == null) { session.setAttribute("Name", " Hello world session"); resp.getWriter().append(" No session"); } else { resp.getWriter().append((String)session.getAttribute("name")); } } } 

When the page is refreshed, the browser still shows “Hello world! GET No session”, how to make the value recorded in the session?

  • Is there anything in the cookies? - BigTows
  • @BigTows here it is - Donut
  • You write an attribute with the name Name , and try to get it with the name name . Here: session.getAttribute("name") == null , session.setAttribute("Name", " Hello world session") - not a Programmer

0