62

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.

Martin
  • 11,577
  • 16
  • 80
  • 110

3 Answers3

95

EL expression:

${requestScope.Error_Message}

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

Gary Sheppard
  • 4,764
  • 3
  • 25
  • 35
Christoph Seibert
  • 1,451
  • 10
  • 5
  • 45
    That `requestScope` is by the way optional. The `${Error_message}` will scan in all scopes, in order of page, request, session and application and return the first match. – BalusC Feb 07 '11 at 11:53
  • 3
    @BalusC Thanks for the hint. But I probably keep the requestScope anyway. – Martin Feb 09 '11 at 13:27
  • Thanks, I provided a new one now. – Christoph Seibert Mar 05 '14 at 11:41
  • Thanks a lot. I used Client client = new Client(); client.setName("María"); request.setAttribute("client", client); and in my jsp page I used ${ client.name } and It wroks fine. – Aron Aug 15 '14 at 05:45
4

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}"/>
Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
1

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.

forumulator
  • 836
  • 12
  • 12