从JSTL显示httpServletResponse.sendError(403,“我的消息”)状态的最简单的方法是什么 [英] What is the simplest way to display httpServletResponse.sendError(403, "My Message") status from JSTL

查看:492
本文介绍了从JSTL显示httpServletResponse.sendError(403,“我的消息”)状态的最简单的方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个servlet做一些错误检查,如果有什么问题,我通常会这样做:

  response.sendError(403,我的消息)
return;

不要想从servlet中抛出异常 - 因为我想符合HTTP状态代码。



在web.xml中,我已经配置了以下内容:

 < error-page> 
< error-code> 403< / error-code>
< location> /failure.jsp< / location>
< / error-page>

在failure.jsp中我已声明使用JSTL,我想得到显示的错误信息。我知道我可以在scriptlet中执行以下操作:

 
<%= request.getAttribute(javax.servlet.error.message )%>

但是我想使用JSTL与一些c:if子句,所以如果我可以删除使用scriptlet,这将不胜感激。 p>

如何使用JSTL轻松地从错误页面的servlet中的sendError语句中获取值?

解决方案

该scriptlet:

 <%= request.getAttribute(javax.servlet。 error.message)%> 

可以翻译成以下EL:

  $ {requestScope ['javax.servlet.error.message']} 

如果您在地图中有点,则支架符号 bean ['foo.bar'] 非常有用>或作用域名称,因为 bean.foo.bar 显然不会返回所需的 Map 或范围值。


I have a servlet which does some error checking and if something is wrong I will typically do this:

response.sendError(403, "My message")
return;

I don't want to throw an exception from the servlet - because I would like to conform with HTTP status codes.

In the web.xml I have configured the following:

<error-page>
    <error-code>403</error-code>
    <location>/failure.jsp</location>
</error-page>

In the failure.jsp I have declared usage of JSTL and I would like to get the error messages displayed. I know that I can do the following in scriptlets:

<%= request.getAttribute("javax.servlet.error.message") %>

But I would like to use JSTL with some c:if clause so if I can drop using scriptlets, this would be appreciated.

How can I easily fetch the values from the sendError statement in the servlet in the error page by using JSTL?

解决方案

The scriptlet:

<%= request.getAttribute("javax.servlet.error.message") %>

can be translated to the following EL:

${requestScope['javax.servlet.error.message']}

The brace notation bean['foo.bar'] is very useful if you have dots in Map or scoped key names, because bean.foo.bar obviously doesn't return the desired Map or scoped value.

这篇关于从JSTL显示httpServletResponse.sendError(403,“我的消息”)状态的最简单的方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆