java - 将错误处理从 JSP 替换为 Servlet

标签 java jsp servlets error-handling

我的 JSP 脚本中有此代码

<%@ page errorPage ="error.jsp"%>
<%!
  ...My JSP Code... //No matter for this question
%>

我的error.jsp脚本是:

<%@ page isErrorPage = "true"%>
<% if (exception != null) { %>
  The cause of the exception error has been: 
        <% exception.printStackTrace(new java.io.PrintWriter(out)); %>
<% } %>

喜欢: http://www.tutorialspoint.com/jsp/jsp_exception_handling.htm

但我想将所有之前的代码替换为 servlet...

如你所见...

我需要将 <%@ page errorPage ="error.jsp"%> 转换为 Servlet 代码...

和 error.jsp 完全...例如如何翻译这个:<%@ page isErrorPage = "true"%>

抱歉,但我不知道该怎么做...

PD: 我正在寻找我的问题,但网站告诉我需要更改 web.xml 文件,我不想这样做,我认为(我的 JSP 脚本工作正常,并且由Tomcat servlet 也可以正常工作,无需更改该文件)。

最佳答案

嘿伙计@Luigi Giuseppe。我想这会消除你的疑虑。

在使用 Servlet 和 JSP 编写的 Java Web 应用程序中定义错误页面的两种方法,如您的问题相关。

 1. First way is page wise error page which is defined on each jsp page and if there is any unhanded exception thrown from that page, corresponding error page will be displayed. 

 2. Second approach is an application wide general or default error page which is shown if any Exception is thrown from any Servlet or JSP and there is no page specific error page defined.

HTTP 标准错误代码:

 1. Information This one is a new return code which is not 100%
    supported and normally and only provides information to the client
    about the request.

 2. Success response, the request has been correctly executed ->
    expected answer from server (http code 200).

 3. Redirection response. The resource has moved and is not any more at
    this URL.

 4. Error on client side. Probably most known of all is error 404 : Not
    Found.

定义 error.jsp 页面,例如:

//error.jsp
<%@ page isErrorPage="true"%>
//login.jsp
<%@ page errorPage="error.jsp"%>

并且,Java Web 应用程序 JSP Servlet 页面中的错误页面如下:

基于异常的默认错误页面:

<error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error.htm</location>
</error-page>

基于 HTTP 错误代码的默认错误页面:

<error-page>
    <error-code>500</error-code>
    <location>/internal-server-error.htm</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/page-not-found-error.htm</location>
</error-page>

创建自定义错误页面:

 1. 400.html -> telling the user did something wrong

 2. 500.html -> telling the server did something wrong

400.html:

<error-page>
        <error-code>400</error-code>
        <location>/404.html</location>
 </error-page>
 <error-page>
        <error-code>401</error-code>
        <location>/404.html</location>
 </error-page>

500.html:

     <error-page>
          <error-code>500</error-code>
          <location>/500.html</location>
       </error-page>
       <error-page>
          <error-code>501</error-code>
          <location>/500.html</location>
       </error-page>

关于java - 将错误处理从 JSP 替换为 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21792096/

相关文章:

Java do while, while

java - Double.parseDouble() 在我认为它应该是 NumberFormatException 时返回 Infinity

java - 无法将 spring boot 应用程序部署到另一个 vm

java - 在 WindowBuilder Eclipse 中更改 JFrame

java - 在 Java servlet 中显式管理 session 以保护 Web 应用程序

java - 如何从我的 java 类将字符串发送到 servlet

java - 如何在jsp中显示java中的ArrayList项目?

java - Java EE 网站的默认 session 超时是多少?

java - 子容器在启动 java.util.concurrent.ExecutionException 期间失败

javascript - Java-将javascript连接到servlet