How to access at request attributes in JSP?

2019-01-07 10:35发布

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 jstl el
3条回答
Animai°情兽
2楼-- · 2019-01-07 10:38

EL expression:

${requestScope.Error_Message}

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

查看更多
来,给爷笑一个
3楼-- · 2019-01-07 10:44

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.

查看更多
别忘想泡老子
4楼-- · 2019-01-07 10:47

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}"/>
查看更多
登录 后发表回答