How can I set a session variable in a servlet and get it in a JSP?

后端 未结 3 1399

I\'m learning java and try to pass some variable from servlet to jsp page. Here is code from servlet page

@WebServlet(\"/Welcome\")
public class WelcomeServl         


        
3条回答
  •  梦毁少年i
    2020-12-18 15:55

    You set an attribute in a session. You have to retrieve it from a session:

    Object sss = session.getAttribute("MyAttribute");
    

    And since you are dispatching the request, you actually don't need a session. You can set the attribute in a request object in your servlet:

    request.setAttribute("MyAttribute", "test value");
    

    Then read it as you are already doing in you JSP.

提交回复
热议问题