在项目中,经常会抛出异常,输出比较友好的信息来提示用户,并指导用户行为。
大体思路:首先自定义一个异常类MyException,继承RuntimeException,定义一些变量和对应的setter、getter方法。
然后创建servlet,在需要抛出异常的地方throw new MyException()。再创建一个过滤器捕获抛出的异常,转发到提示页面msg.jsp,显示对应的异常信息。
代码示例如下:
一、异常MyException
package com.myee.exception;
/**
* Created by want on 2016/12/6.
*/
public class MyException extends RuntimeException {
public static final String INFO = "INFO";
public static final String ERROR = "ERROR";
public static final String SUCCESS = "SUCCESS";
protected String type = "INFO";
protected String code = "0";
protected String codeStr = "respCode";
protected String msgStr = "respMsg";
protected String url = "";
public String getCodeStr() {
return this.codeStr;
}
public void setCodeStr(String codeStr) {
this.codeStr = codeStr;
}
public String getMsgStr() {
return this.msgStr;
}
public void setMsgStr(String msgStr) {
this.msgStr = msgStr;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public MyException() {
}
public MyException(String msg) {
super(msg);
}
public MyException(String msg, String url) {
super(msg);
this.url = url;
}
public MyException(String msg, String url, String msgStr) {
super(msg);
this.url = url;
this.msgStr = msgStr;
}
public String getType() {
return this.type;
}
}
二、过滤器ExeceptionFilter
package com.myee.filter;
import com.myee.exception.MyException;
import javax.servlet.*;
import java.io.IOException;
/**
* Created by want on 2016/12/6.
*/
public class ExeceptionFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
filterChain.doFilter(servletRequest, servletResponse);
} catch (MyException e) {
/* e.printStackTrace();*/
servletRequest.setAttribute("url", e.getUrl());
servletRequest.setAttribute("type", e.getType());
servletRequest.setAttribute("code", e.getCode());
servletRequest.setAttribute("codeStr", e.getCodeStr());
servletRequest.setAttribute("msgStr", e.getMsgStr());
servletRequest.getRequestDispatcher("/msg.jsp").forward(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
}
}
三、Servlet
package com.myee.servlet;
import com.myee.exception.MyException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by want on 2016/12/6.
*/
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req , resp );
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if(req !=null){
throw new MyException("我的自定义异常!!!","/index.jsp","正常抛出了");
}
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
}
四、web.xml配置
ee
com.myee.servlet.MyServlet
ee
/myee
exefilter
com.myee.filter.ExeceptionFilter
exefilter
/*
五、index.jsp和msg.jsp
①index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
hello world!
访问
②msg.jsp
<%@ page contentType="text/html;charset=utf-8" language="java" %>
提示窗