在JSP中返回特定的HTTP响应状态

在JSP中返回特定的HTTP响应状态,jsp,tomcat,tomcat7,httpresponse,Jsp,Tomcat,Tomcat7,Httpresponse,我希望在JSP中有一个自定义错误代码,以防止发生特殊情况: boolean error; // do error checking here if (error) { throw new javax.xml.ws.http.HTTPException(400); } 但是,这最终会向客户机抛出一个500,因为服务器将异常解释为内部问题。如何将状态400发送到客户端?您应该能够通过响应对象发送: <% response.sendError(418, "I'm a teapot"

我希望在JSP中有一个自定义错误代码,以防止发生特殊情况:

boolean error;
// do error checking here

if (error) {
    throw new javax.xml.ws.http.HTTPException(400);
}

但是,这最终会向客户机抛出一个500,因为服务器将异常解释为内部问题。如何将状态400发送到客户端?

您应该能够通过响应对象发送:

<%
 response.sendError(418, "I'm a teapot" );
%>