日期:2014-05-18  浏览次数:20740 次

大家说一下
Java code
package com.fox_ice.Filter;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LogonFiler implements Fileter
{
    private static final String LOGON_URI="logon_uri";
  private static final String HOME_URI="home_uri";    
  
  private String logon_page;
  private String home_page;
  
  public void init(FilterConfig filterConfig) throws ServletException
  {
       logon_page=filterConfig.getInitParameter(LOGON_URI);
       home_page=filterConfig.getInitParameter(HOME_URI);
       
       if(null==logon_page||null==home_page)
       {
            throw new ServletException("没有指定主页");
       }
  }
  
  public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain)
  {
      HttpServletRequest httpReq=(HttpServletRequest)request;
      HttpServletResponse httpResp=(HttpServletRequest)response;
      HttpSession session=httpReq.getSession();
      
      String request_uri=httpReq.getRequestURI();
      String ctxPath=httpReq.getContextPath();
      String uri=request_uri.subString(ctxPath.length());
      
      
      if(logon_page.equals(uri))
      {
              String strLogon=httpReq.getParameter("action");
              if("logon".equals(strLogon))
              {
                          String name=httpReq.getParameter("name");
                          String password=httpReq.getParameter("password");
                          if("zhangsan".equals(name)&&"1234".equals(password))
                          {
                              session.setAttribute("isLogon","true");
                              session.setAttribute("user",name);
                              
                              String origin_uri=httpReq.getParameter("origin_uri");
                              if(null!=origin_uri&&!"".equals(origin_uri))
                              {
                                  httpResp.sendRedirect(origin_uri);
                              }
                              else
                              {
                                  httpResp.sendRedirect(ctxPath+home_page);
                                  return;
                              }
                          }
              }
              else
              {
                      String origin_uri=httpReq.getParameter("origin_uri");
                      if(null!=origin_uri&&"".equals(origin.uri))
                      {
                              httpReq.setAttribute("origin_uri",origin_uri);
                      }
                      
                      httpResp.setContentType("text/html;charset=gb2312");
                      PrintWriter out=httpResp.getWriter();
                      out.println("<h2>用户名密码错误</h2>");
                      requestDispatcher rs=httpReq.getRequestDispatcher(logon_page);
                      rs.include(request,response);
                      return ;
                      
              }
      }
      else
      {
                  chain.doFilter(request,response);
                  return;
      }
      
      else
      {
          String isLogon=(String)session.getAttribute("isLogon");
          if("true".equals(isLogon))
          {
              chain.doFilter(request,response);
              return;

          }
          else
          {
              String strQuery=httpReq.getQueryString();
              if(null!=strQuery)
              {
                  request_uri=request_uri+"?"+strQuery;
              }
              httpReq.setArrtibu