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,
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.
Session attributes contain application-specific information that is passed between a bot and a client application during a session.
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.
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.
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"%>
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With