Java 请求参数错误异常处理

package com.accp.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

 

 

public class MyFilter extends HttpServlet implements Filter {

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
 

  HttpServletRequest arg0 =(HttpServletRequest)request;
  arg0.getSession().setAttribute("eurl", arg0.getRequestURI());
  chain.doFilter(request, response);
    /***
     *  类需要继承HttpServlet  和 实现接口 Filter (过滤器)
     *  添加一个doFilter 方法(右击选择 Source---- Override/mplement Methods)
     *  定义HttpServletRequest,变量arg0 接收 request (需要转换类型)
     *  arg0.getSession().setAttribute("ErrorUrl", arg0.getRequestURI());
     */
 }
 

 public void init(FilterConfig filterConfig) throws ServletException {
  // TODO Auto-generated method stub
  
 } 
}


------------------Web.Xml的配置
 
   names
   com.accp.filter.MyFilter
 

 
 
    names
    /*
 

 
    500
    /Error.jsp
 

 
    404
    /NullPage.jsp
 

 

------------------------错误页面
 <%String str = (String)request.getSession().getAttribute("eurl");
    String strId= request.getParameter("item.id");
     Integer id = Integer.parseInt(strId);
    
    if(str.equals("/news/news.do")){
    
     response.sendRedirect("news.do?item.id="+id+"&opt=doUpdateNews");
    }else
    {
     response.sendRedirect(str+"?opt=doFind");
    }
    %>

你可能感兴趣的:(Java 请求参数错误异常处理)