Have a servlet

HttpSession httpSession = request.getSession(true); String Login = (String) request.getParameter("login"); httpSession.setAttribute("login", Login); response.sendRedirect("index.jsp"); 

And there is a jsp page

 String username=(String) session.getAttribute("login"); if(username==null) username=""; <p>Добро пожаловать, <%=username%></p> 

Does not transfer the login to the session. What is the mistake here?

  • Is there a page directive on the page? - Mage
  • Yes, at the very beginning <% @ page language = "java" contentType = "text / html; charset = utf-8" pageEncoding = "utf-8"%> - alex91
  • try adding session = "true" attribute to this directive - Mage
  • It did not help ( - alex91
  • How is the servlet called? Are you sure that the login parameter is passed to it and comes in? - a_gura

2 answers 2

response.sendRedirect() transfers control to the browser so that it redirects to the page - it’s hard to expect that the session will be preserved ...

It is necessary to redirect through the server context - such as pageContext.forward() or as indicated by @SergioVasus (which is the same thing)

    It seems that you simply redirect to the "index.jsp" page. To transfer data try:

     getServletContext().getRequestDispatcher("/index.jsp").forward(request,response)