当前位置: 代码迷 >> Java Web开发 >> 大家说一下解决办法
  详细解决方案

大家说一下解决办法

热度:66   发布时间:2016-04-17 01:13:16.0
大家说一下
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.setArrtibute("origin_uri",origin_uri);              RequestDispatcher rd=httpReq.getRequestDispatcher(logon_page);              rd.forward(httpReq,httpResp);              return;          }      }  }    public void destroy(){}}
  相关解决方案