ref:https://blog.csdn.net/tao_ssh/article/details/53486449

在项目中,经常会抛出异常,输出比较友好的信息来提示用户,并指导用户行为。
大体思路:首先自定义一个异常类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();此处过滤器捕获异常,注意filter在servlet外层监听*/
            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);//将错误信息转发到msp.jsp
        }
    }
 
    @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配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>ee</servlet-name>
        <servlet-class>com.myee.servlet.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ee</servlet-name>
        <url-pattern>/myee</url-pattern>//访问myee页面才抛出异常
    </servlet-mapping>
 
    <filter>
        <filter-name>exefilter</filter-name>
        <filter-class>com.myee.filter.ExeceptionFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>exefilter</filter-name>
        <url-pattern>/*</url-pattern>//其他页面正常访问
    </filter-mapping>
 
</web-app>

五、index.jsp和msg.jsp

①index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
<div style="width: 300px;height: 500px;margin: 10px auto;padding: 10px">
    <h4>hello world!</h4>
    <a href="/myee">访问</a>
</div>
</body>
</html>

 

②msg.jsp

<%@ page contentType="text/html;charset=utf-8" language="java" %>
<head>
    <title>提示窗</title>
 
</head>
<body>
<div style="width: 300px;height: 500px;margin: 10px auto;padding: 10px">
     
    <div><h4>系统提示</h4></div>
    <p>msg:${msg}</p><br>
    <p>提示信息:${msgStr}</p>
    <a href="${url}">确定</a>
</div>
 
</body>
</html>
posted on 2018-08-29 09:25  studyskill  阅读(453)  评论(0编辑  收藏  举报