当前位置: 技术问答>java相关
关于servlet异常处理
来源: 互联网 发布时间:2015-10-16
本文导语: 当servlet出现异常的时候,我需要重定向的但别的页面统一处理 我的代码如下 if ( error1 ) response.sendRedirect(url1); .... 可我发现出现异常error1的时候,代码还会继续往下面执行,按道理应该转到ur1去了,不会再回来. 我...
当servlet出现异常的时候,我需要重定向的但别的页面统一处理
我的代码如下
if ( error1 )
response.sendRedirect(url1);
....
可我发现出现异常error1的时候,代码还会继续往下面执行,按道理应该转到ur1去了,不会再回来.
我也用了下面的方法处理,但也还是有同样的问题
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", url1);
我的代码如下
if ( error1 )
response.sendRedirect(url1);
....
可我发现出现异常error1的时候,代码还会继续往下面执行,按道理应该转到ur1去了,不会再回来.
我也用了下面的方法处理,但也还是有同样的问题
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", url1);
|
jsp中设个errorpage就可以了
servlet中用如下方法也可以安全导入errorpage:
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class ErrorpageDemo1 extends HttpServlet{
private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
final public ServletConfig getServletConfig() {
return config;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
JspFactory jf = JspFactory.getDefaultFactory();
PageContext pageContext = jf.getPageContext(this, request, response, "/errorpage.jsp", true, 8192, true);
JspWriter out = pageContext.getOut();
HttpSession session = pageContext.getSession();
try {
//your code here, below is a sample
out.print("rnrnrntjsp error page demo rnrnrnrn jsp 1.0 error page demorn");
String s = "eee";
s.getBytes();
int i = Integer.parseInt(s);
out.print("rnrnrn");
} catch(Throwable t) {
if(!(t instanceof Exception)) {
t = new ServletException("JSP Page threw a non-Exception Throwable.",t);
}
//pageContext.handlePageException((Exception) t);
pageContext.handlePageException(t);
} finally {
jf.releasePageContext(pageContext);
}
}
}
servlet中用如下方法也可以安全导入errorpage:
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class ErrorpageDemo1 extends HttpServlet{
private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
final public ServletConfig getServletConfig() {
return config;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
JspFactory jf = JspFactory.getDefaultFactory();
PageContext pageContext = jf.getPageContext(this, request, response, "/errorpage.jsp", true, 8192, true);
JspWriter out = pageContext.getOut();
HttpSession session = pageContext.getSession();
try {
//your code here, below is a sample
out.print("rnrnrntjsp error page demo rnrnrnrn jsp 1.0 error page demorn");
String s = "eee";
s.getBytes();
int i = Integer.parseInt(s);
out.print("rnrnrn");
} catch(Throwable t) {
if(!(t instanceof Exception)) {
t = new ServletException("JSP Page threw a non-Exception Throwable.",t);
}
//pageContext.handlePageException((Exception) t);
pageContext.handlePageException(t);
} finally {
jf.releasePageContext(pageContext);
}
}
}
|
if ( error1 ){
response.sendRedirect(url1);
return;
}
在jsp中也是
response.sendRedirect(url1);
return;
}
在jsp中也是
|
第一种方法,加个return;
第二种方法,
if ( error1 )
{
response.sendRedirect(url1);
}
else
{
....
}
第二种方法,
if ( error1 )
{
response.sendRedirect(url1);
}
else
{
....
}