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

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

问题描述

我有一个 servlet 可以进行一些错误检查,如果出现问题,我通常会这样做:

response.sendError(403, "我的消息")返回;

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

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

<错误代码>403</错误代码><location>/failure.jsp</location></错误页面>

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

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

但我想将 JSTL 与一些 c:if 子句一起使用,因此如果我可以放弃使用 scriptlet,这将不胜感激.

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

解决方案

脚本:

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

可以翻译成以下EL:

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

大括号符号 bean['foo.bar']Map 或作用域键名中有点时非常有用,因为 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, “My Message") 状态的最简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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