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

后端 未结 3 1416

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条回答
  •  情深已故
    2020-12-18 16:02

    You should avoid scriptlets because they are java code in HTML, they break MVC pattern, they are ugly, odd and deprecated.

    Simply replace :

    <% 
    Object sss = request.getAttribute("MyAttribute"); 
    String a = "22";
    
    %>
    

    with simply using EL ${MyAttribute}

    But if you want to stick with scriptlets, you should get the attribute from the proper scope which is session in your case.

提交回复
热议问题