Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print session attributes in jsp

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

                <i class="icon-user"></i> 
                <%
                    session.getAttribute("name");
                %>
                <span class="caret"></span>

and it sais

session cannot be resolved

9:              <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
10:                     <i class="icon-user"></i> 
11:                     <%
12:                         session.getAttribute("name");
13:                     %>
14:                     <span class="caret"></span>
15:                 </a>

There is a session because I'm logged in.

Kind regards,

like image 930
user1007522 Avatar asked Jun 12 '12 09:06

user1007522


People also ask

What is session attribute in JSP?

The session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.

What is session attribute?

Session attributes contain application-specific information that is passed between a bot and a client application during a session.

What is the default value of session attribute in JSP?

The default value of the attribute is true, meaning that expressions, ${...}, are evaluated as dictated by the JSP specification. If the attribute is set to false, then expressions are not evaluated but rather treated as static text.

What is out Println in JSP?

println for printing anything on console. In JSP, we use only out. print to write something on console because the out we're referring to isn't System. out, it's a variable in the effective method that wraps our JSP page.


4 Answers

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"%>
like image 51
Pau Kiat Wee Avatar answered Oct 26 '22 03:10

Pau Kiat Wee


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.

like image 29
Kazekage Gaara Avatar answered Oct 26 '22 04:10

Kazekage Gaara


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.

like image 1
Japan Trivedi Avatar answered Oct 26 '22 03:10

Japan Trivedi


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.

like image 1
ShadowCrow Avatar answered Oct 26 '22 04:10

ShadowCrow