0

在这条线之后,它一直通过空点,System.out.println("Inside the filter.............." ); 所以谁能告诉我我的代码和逻辑有什么问题

并且 home.jsp(index page) 没有显示。在这里,我想过滤每个 url 并只处理有效的登录用户请求。这是我的逻辑..

UserServlet.java

 private void loginDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

            User u = new User();
            UserService us =new UserServiceImpl() ;

            String Uname = request.getParameter("txtUname");        
            String Pwrd = request.getParameter("txtPwrd");  

            u.setUname(Uname);
            u.setPwrd(Pwrd);

            System.out.println(Uname+""+Pwrd);
            try {
                if(us.Userlogin(u.getUname(),u.getPwrd())){
                    String message = "Thank you, " + Uname +"..You are now logged into the system";             
                    request.setAttribute("message", message);
                    //RequestDispatcher rd = getServletContext().getRequestDispatcher("/menu.jsp");
                    //rd.forward(request, response); 
                    HttpSession session = request.getSession(true);
                    session.setAttribute("loggedUser", u);
                    String reqUrl = (String)session.getAttribute("requestedURL");
                    session.removeAttribute("requestedURL");
                    response.sendRedirect(reqUrl);              
                }else{
// direct to login}

FilterRequest.java

public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
      HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) resp;
      System.out.println("Inside the filter.............."  );
      HttpSession session = request.getSession(true);
      User u = null;
      if(session.getAttribute("loggedUser")!=null){
          u = (User) session.getAttribute("loggedUser");
      }       
      if (u!= null)
      {
          System.out.println("user does exits.." + u.getUname() );
          chain.doFilter(req, resp);          
      }else{
          String message = "Please Login!";             
          req.setAttribute("loginMsg", message);
          //response.sendRedirect("login2.jsp");
      }
    }

web.xml

  <filter>
        <filter-name>FilterRequest</filter-name>
        <filter-class>com.mobitel.bankdemo.web.FilterRequest</filter-class>
  </filter>
  <filter-mapping>
        <filter-name>FilterRequest</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>

堆栈跟踪

 `Jul 11, 2013 10:24:26 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Kaspersky Lab\Kaspersky Anti-Virus 6.0 for Windows Workstations MP4\;C:\Program Files\Java\jdk1.6.0_07/bin;C:\Program Files\MySQL\MySQL Server 5.2\bin;D:\common libs\com.mysql.jdbc_5.1.5.jar;;C:\Users\lcladmin\Documents\Softwares\java related\eclipse-jee-indigo-win32_2\eclipse;
Jul 11, 2013 10:24:26 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:BankDemoWeb' did not find a matching property.
Jul 11, 2013 10:24:27 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jul 11, 2013 10:24:27 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jul 11, 2013 10:24:27 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 554 ms
Jul 11, 2013 10:24:27 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 11, 2013 10:24:27 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.41
Jul 11, 2013 10:24:27 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jul 11, 2013 10:24:27 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jul 11, 2013 10:24:27 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 517 ms
Inside the filter..............
user does exits..`

谢谢..

4

1 回答 1

1

你有一个非常简单的问题:用户没有登录;)

澄清事情:

在您Filter中,您正在检查用户是否已登录,在这种情况下执行链

chain.doFilter(req, resp);

到目前为止很好。

但是如果用户没有登录会发生什么?在这种情况下,您没有执行链,因此没有Servlet. Servlet始终是链中的最后一个元素。

因此,您的用户无法登录,因为他们永远无法Servlet登录,因为您之前将它们过滤掉了。

当没有用户登录时,您必须扩展过滤器以允许登录。这可以通过例如更改url-pattern.Filter

于 2013-07-11T06:04:54.900 回答