请问一下在java中自定义异常拦截器(不要使用其他框架,如spring),怎么拦截?

2024-12-22 20:23:23
推荐回答(2个)
回答1:

public class MyException extends Exception{
public MyExceprion(){
System.out.println("自定义异常");
}
}

try catch(MyException e){
}

当然 你也可以定义一个拦截器放在web.xml
那你就要继续实现 filter 接口了 继承Exception 实现 Filter 接口 那不就完了
implements Filter

public class CharFilter implements Filter {
private String encode;
public void destroy() {

}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain fc) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)resp;
request.setCharacterEncoding(this.encode);
response.setCharacterEncoding(this.encode);
fc.doFilter(request,response);
}

public void init(FilterConfig arg0) throws ServletException {
this.encode=arg0.getInitParameter("encode");//��xml��init-param
}

}

web.xml中 配置拦截器

CharFilter
neusoft.team101.dms.common.filter.CharFilter

encode
UTF-8



CharFilter
/*

回答2:

spring AOP就行了,设置好。。百度里输入springAOP就有答案了,用dom4j也行