<error-page> 不适用于 servlet
<error-page> is not working for servlet
当 servlet 中的异常不工作但 jsp 它正常工作时
web.xml代码
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
servlet 代码
protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
String a=null;
a.toString();//this line will throw exception
}
finally {
out.close();
}
}
它不会重定向到 error.jsp
当我们在 servlet 中编写 catch 块时它不起作用,而当我们不编写 catch 块时它也能正常工作。
Servlet 代码
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
DAL db=null;
ResultSet rs=null;
parameter p1=new parameter();
// Object param[];
// List<Object> param=new ArrayList<Object>();
long myId=0;
try {
db=new DAL();
String name=request.getParameter("fn");
db.setQuery("{call usp_StudentInsertData(?,?)}");
db.setInParam(1,name);
db.setInParam(2,1000);
db.insertUpdate();
out.println("insert");
response.sendRedirect("home.jsp");
}
catch(Exception e)
{
}
finally {
}
}
Servlet 代码
这将抛出异常到一般错误页面
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
String a=null;
a.toString();
}
catch(Exception e)
{
throw new ServletException(e);
}
finally {
//dont write out.close();
}
}
当 servlet 中的异常不工作但 jsp 它正常工作时
web.xml代码
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
servlet 代码
protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
String a=null;
a.toString();//this line will throw exception
}
finally {
out.close();
}
}
它不会重定向到 error.jsp
当我们在 servlet 中编写 catch 块时它不起作用,而当我们不编写 catch 块时它也能正常工作。 Servlet 代码
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
DAL db=null;
ResultSet rs=null;
parameter p1=new parameter();
// Object param[];
// List<Object> param=new ArrayList<Object>();
long myId=0;
try {
db=new DAL();
String name=request.getParameter("fn");
db.setQuery("{call usp_StudentInsertData(?,?)}");
db.setInParam(1,name);
db.setInParam(2,1000);
db.insertUpdate();
out.println("insert");
response.sendRedirect("home.jsp");
}
catch(Exception e)
{
}
finally {
}
}
Servlet 代码 这将抛出异常到一般错误页面
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
String a=null;
a.toString();
}
catch(Exception e)
{
throw new ServletException(e);
}
finally {
//dont write out.close();
}
}