Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
823 views
in Technique[技术] by (71.8m points)

jsp - What is the simplest way to display httpServletResponse.sendError(403, "My Message") status from JSTL

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...