Print session attributes in jsp

后端 未结 4 658
温柔的废话
温柔的废话 2020-12-03 07:31

My webapp is ready but I just wanted to add a little dropdown menu with the username as title. This is my jsp code:

                

        
相关标签:
4条回答
  • 2020-12-03 07:55

    In your servlet:

     1) get your parameter:
    
        String param = request.getParameter("param");
    
     2) send it to the request object as an attribute:
    
        request.setAttribute("param", param);
    

    In your JSP:

       use JSTL, and EL to return the attribute you sent from your servlet:
    
       <input type="text" name="param" value="<c:out value="${param}" />" />
    

    and there you go.

    0 讨论(0)
  • 2020-12-03 07:59

    You can try an alternative:

    <% request.getSession().getAttribute("name") %>
    

    The reason that session is not getting resolved is because you must have set session="false" in your page directive.

    Read this for further reference.

    0 讨论(0)
  • 2020-12-03 08:04

    You can use EL, which is prefered in JSP.

    <c:out value="${sessionScope.name}"/>
    

    Or if the name value is HTML safe, you can use

    ${sessionScope.name}
    

    Make sure the JSP is allow access session.

    <%@ page session="true" %>
    

    To use core JSTL, make sure the following code is included.

    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    0 讨论(0)
  • 2020-12-03 08:14

    I agree with answer given by @Pau Kiat Wee. But you can also set this user name from the controller in modelmap and then just simple use it in a EL. That would also be a good option. Hope this helps you. Cheers.

    0 讨论(0)
提交回复
热议问题