我的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 ...
正如你所看到的......
我需要将&lt;%@ page errorPage =“error.jsp”%&gt; 翻译成Servlet代码......
和error.jsp完全...例如如何翻译:&lt;%@ page isErrorPage =“true”%&gt; ?
抱歉,但我不知道怎么做......
PD : 我正在寻找我的问题,但网站告诉我需要更改 web.xml 文件,我不想这样做,我认为(我的JSP脚本工作正常,并且转换为Tomcat servlet在没有更改该文件的情况下工作正常。)
答案 0 :(得分:1)
使用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 Application JSP Servlet页面中的Error页面如下:
默认错误页面基于例外:
<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>
答案 1 :(得分:0)
try/catch
,否则使用该方法显示异常错误,如下面的代码:
package com.jmail.servlet.exception;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/MyExceptionServlet")
public class MyExceptionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
throw new ServletException("GET method is not supported.");
}
}
现在,你只是得到这样的错误页面:
GET method is not supported.