How to access at request attributes in JSP?

JspJstlEl

Jsp Problem Overview


Currently I use:

<%
final String message = (String) request.getAttribute ("Error_Message");
%>

and then

<%= message %>

However I wonder if the same can be done with EL or JSTL instead of using a scriptlet.

Jsp Solutions


Solution 1 - Jsp

EL expression:

${requestScope.Error_Message}

There are several implicit objects in JSP EL. See Expression Language under the "Implicit Objects" heading.

Solution 2 - Jsp

Using JSTL:

<c:set var="message" value='${requestScope["Error_Message"]}' />

Here var sets the variable name and request.getAttribute is equal to requestScope. But it's not essential. ${Error_Message} will give you the same outcome. It'll search every scope. If you want to do some operation with content you take from Error_Message you have to do it using message. like below one.

<c:out value="${message}"/>

Solution 3 - Jsp

Just noting this here in case anyone else has a similar issue.
If you're directing a request directly to a JSP, using Apache Tomcat web.xml configuration, then ${requestScope.attr} doesn't seem to work, instead ${param.attr} contains the request attribute attr.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartinView Question on Stackoverflow
Solution 1 - JspChristoph SeibertView Answer on Stackoverflow
Solution 2 - JspMenuka IshanView Answer on Stackoverflow
Solution 3 - JspforumulatorView Answer on Stackoverflow