Not able to retrieve session/request scope values using JSTL - jstl

In my xhtml page am setting values in some myValue variable. Code is as below,
<body>
<c:set var="myValue" value="someValue" scope="request"></c:set>`
</body>
Am hitting a login.jsp (Login page) from this xhtml page and trying to print the value on my login.jsp as below,
myValue ID retrieved from request <c:out value="${param.myValue}" />
This is not printing someValue. I also checked by putting it in session scope like below,
<c:set var="myValue" value="someValue" scope="session"></c:set>
Even this is not working
And in login.jsp,
myValue ID retrieved from session <c:out value="${sessionScope.myValue}" />

You should have just ${myValue}, as param.myValue means to find value from URL params.

Related

how do i create an arraylist inside jsp using jstl

I want to collect values from a JSP page and pass it dynamically to another JSP page with the help of JSTL. How can I do this?
You use a request scoped HashMap for that.
Example
1) Declare the HashMap in each JSP you want to insert or access the list of values.
<jsp:useBean id="map" class="java.util.HashMap" scope="request"/>
Note: The scope="request" is what makes it accessible in other JSPs.
2) Stuff information into the HashMap
<c:set target="${requestScope.map}" property="city" value="${param.city}"/>
<c:set target="${requestScope.map}" property="state" value="${param.state}"/>
<c:set target="${requestScope.map}" property="phone" value="${param.phone}"/>
3a) You can now pull out the values in a different JSP by simply doing:
<c:out value="${requestScope.map['city']}"/>
-or-
3b) You can also iterate over that HashMap in a different JSP:
<c:forEach items="${requestScope.map}" var="item">
${item.key} = ${item.value}<br/>
</c:forEach>

jstl conditional not working

I'm trying to separate my Java Code from my JSP files and I'm having a bit of a problem. I'm trying to find out whether the user is a guest or not and then printing the appropriate action EG login form or their Username.
Heres my index.jsp file:
<% if(view.guest) { %>
<%= "Scriptlet: Login Form Here" %>
<% } else { %>
<%= "Scriptlet: User Name Here" %>
<% } %>
<br/><br/>
<c:choose>
<c:when test="${view.guest eq true}">
JSTL Tag: Login Form Here
</c:when>
<c:otherwise>
JSTL Tag: User Name Here
</c:otherwise>
</c:choose>
This produces the following output:
Scriptlet: Login Form
JSTL Tag: User Name
As you can see the Scriptlet produces the expected results, but the JSTL tags produce the opposite. Infact if I reverse the JSTL's conditional to false (for debugging purposes) it still produces the same result "JSTL Tag: User Name"
Btw view.guest is a public boolean variable of the object view.
The JSP EL doesn't access local variables. It accesses attributes of the page, request, session or application scope. And it also assumes you're not using public fields (which should never be used), but respect the Java Bean conventions.
And, just as in Java, comparing a boolean with true is unnecessary. You just need
<c:when test="${view.guest}">
And the condition will evaluate to true if there is a page, request, session or application attribute named "view", having a public getGuest() or isGuest() method returning true.
If you didn't use scriptlets at all, you would never have local variables in your pages, and you would never have this problem.

jstl, how to fill current request with map

I have a map which contain request parameters and their value, which will be used later on the jsp page. I'm usinng jsp page inclusion later and I don't know what exact params would I use
How I suppose, the filling should be implemented:
<c:forEach var="theParameter" items="${ parametersMap }" >
<c:set var="${theParameter.key}" value="${theParameter.value}" />
</c:forEach>
But I'm getting the error that the 'var' attribute can't use expression
Do you have any ideas about workaround?
EDIT:
Example:
parametersMap = [ 'param1' : 'value1' ; 'param2' : 'value2']
as result I would like something like this:
<c:set var="param1" value="value1" />
<c:set var="param2" value="value2" />
You need to do like this
<c:set target="${RequestScope[aNewMap]}"
property="${theParameter.key}" value="${theParameter.value}"/>
I solved my problem by adding custom tag, which on java part retrieves the Hashmap object and fill the request (which can be retieved from pageContext)

How to get sessionScope attributes on JSTL?

The task is to retrieve parameters from session via JSTL. The session parameter name is "programId" .
I tried:
<c:if test="${sessionScope.programId != null}" >
please execute
</c:if>
Then I tried:
<c:if test="${sessionScope:programId != null}" > please execute </c:if>
Here I get: The function applicationScope:programId is undefined
On top I have:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Oracle has in examples:
http://docs.oracle.com/javaee/1.4/tutorial/doc/JSTL5.html
<c:if test="${applicationScope:booklist == null}" >
<c:import url="${initParam.booksURL}" var="xml" />
<x:parse doc="${xml}" var="booklist" scope="application" />
</c:if>
where applicationScope can be swapped by sessionScope.
again "trivialism" complexity drives me nuts. Why corp. examples never work?
Thank You Guys,
You're reading the wrong tutorial page. The <c:xxx> tags does not belong to the JSTL XML taglib which supports XPath syntax. Instead, it belongs to the JSTL Core taglib for which the proper tutorial page is here.
You need to use the normal ${bean.property} notation instead.
<c:if test="${applicationScope.booklist == null}">
<c:import url="${initParam.booksURL}" var="xml" />
<x:parse doc="${xml}" var="booklist" scope="application" />
</c:if>
In normal EL (not XPath!) syntax, the : identifies the start of an EL function. See also the JSTL Functions taglib for several EL function examples for which the tutorial page is here.
See also:
Our JSTL tag wiki page
Our EL tag wiki page
I don't find in your code you have set your variable programID somewhere in your SessionScope using EL something like the one shown below.
<c:set var="programID" value="SomeValue" scope="session"/>
If you indeed didn't set that variable, try setting it first like the above then try the following.
<c:if test="${sessionScope:programId != null}" > please execute </c:if>
It should work.
if you have set session in your controller
session.setAttribute("programID",someValue);
then you should try this
${sessionScope.programID} in your jsp
which is part of jstl core library
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
and if you are getting exception
"${sessionScope:programId != null}" contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${sessionScope:programId != null}]
include jasper.jar in your lib folder
java brushup for basic concepts of java

Spring Security: How do I reset SPRING_SECURITY_LAST_EXCEPTION.message?

I am able to display the SPRING_SECURITY_LAST_EXCEPTION.message ("Bad Credentials") when a user tries to log in with incorrect credentials.
My login jsp currently uses the following code:
<c:if test="${not empty SPRING_SECURITY_LAST_EXCEPTION.message}">
<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
</c:if>
My problem is that the "Bad Credentials" message is still there when the user navigates away from the login page and then comes back.
How can I reset SPRING_SECURITY_LAST_EXCEPTION.message when a user refreshes the login page?
The typical approach is to display error message only after failed login, where failed login is determined by request parameter. That is, you configure Spring Security as
<form-login ... authentication-failure-url = "/login?error=1" />
and show error message as
<c:if test="${not empty param['error']}">
<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
</c:if>
However, since SPRING_SECURITY_LAST_EXCEPTION is a session attribute, I guess you can reset it using the following approach:
<c:remove var = "SPRING_SECURITY_LAST_EXCEPTION" scope = "session" />
For me it was more easy at this way
<c:if test="${param.error != null}">
<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
</c:if>
So you don't worry about removing vars and change the default URL,if there is some error it will be print as the URL got defined as parameter ?error and the exception will be printed (if you pass the parameter error manually nothing will happen because the exception does't exist).

Resources