简体   繁体   English

从 JSP 内部返回 500 错误包括

[英]Return with a 500 error from inside a JSP include

Is there a way to, from inside of a jsp:include page, get its requesting page to respond with an HTTP 500 error?有没有办法从jsp:include页面内部获取其请求页面以响应 HTTP 500 错误? I've tried using the response.sendError(418, "I'm a teapot.");我试过使用response.sendError(418, "I'm a teapot."); , but that only works in the JSP that contains the jsp:include , and only if it is the first line because you can't call it after the response has been committed. ,但这仅适用于包含jsp:include的 JSP,并且仅当它是第一行时才有效,因为您无法在响应提交后调用它。 What I have is this:我有的是这个:

Index.jsp:索引.jsp:

// other HTML

<jsp:include page="Exapmle.jsp">
    <jsp:param name="aVariable" value="aValue" />
</jsp:include>

// other HTML

Example.jsp:例子.jsp:

<%
    String aVariable = request.getParameter("aVariable");
    if (aVariable != null && !aVariable.trim().isEmpty) {
        // code to generate content
        %><%=someContent%><%
    } else { 
        response.sendError(418, "I'm a teapot");
    }
%>

So is there any way to do this?那么有没有办法做到这一点? I'm doubtful based on how JSP's work, but hoping somewhere here can help.我对 JSP 的工作方式表示怀疑,但希望这里的某个地方可以提供帮助。 Also, servlets aren't an option (right now, at least).此外,servlet 不是一种选择(至少现在是这样)。

If you get it to work, you shouldn't rely on it to always work on all platforms and future updates.如果你让它工作,你不应该依赖它总是在所有平台和未来更新上工作。 As you state correctly: Once the response has been committed (eg all the HTTP headers are on the way to the client) there's no more way for the server to add any other HTTP headers.正如您正确陈述的那样:一旦响应已提交(例如,所有 HTTP 标头都在发送到客户端的途中),服务器就无法再添加任何其他 HTTP 标头了。 Let alone change the status code.更不用说更改状态代码了。

That's the reason why JSPs are considered the VIEW layer of an application, not the CONTROLLER.这就是为什么 JSP 被视为应用程序的 VIEW 层而不是 CONTROLLER 的原因。 The times when all application logic was written in JSPs are long over and you should have proper request handling somewhere (if only in a servlet, but probably rather in a more powerful abstraction/framework) - decide about the result and either redirect to an error message or the proper visualization in that code.所有应用程序逻辑都用 JSP 编写的时代已经过去了,您应该在某处进行适当的请求处理(如果仅在 servlet 中,但可能在更强大的抽象/框架中) - 决定结果并重定向到错误该代码中的消息或适当的可视化。

JSP is good to render the content that you send along with the proper status code. JSP 可以很好地呈现您发送的内容以及正确的状态代码。 Not to decide which status code to send.不决定发送哪个状态码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM