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

后端 未结 3 1391

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 15:47

    you are getting if from request not session.

    It should be

    session.getAttribute("MyAttribute")
    

    I suggest you to use JavaServer Pages Standard Tag Library or Expression Language instead of Scriplet that is more easy to use and less error prone.

    ${sessionScope.MyAttribute}
    

    or

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
    
    

    you can try ${MyAttribute}, ${sessionScope['MyAttribute']} as well.

    Read more

    • Oracle Tutorial - Using JSTL

    • Oracle Tutorial - Expression Language

提交回复
热议问题