总是空点

时间:2013-07-11 04:23:58

标签: java session servlet-filters

在此行之后传递空点的所有方法都是System.out.println("Inside the filter.............." );所以任何人都可以告诉我我的代码和逻辑有什么问题

和home.jsp(索引页面)没有显示。在这里,我想过滤每个网址,只进行有效的登录用户请求。在这里我的逻辑..

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..`

谢谢..

1 个答案:

答案 0 :(得分:1)

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

澄清事情:

在您Filter中,您正在检查用户是否已登录,并且在这种情况下使用

执行链接
chain.doFilter(req, resp);

到目前为止一切都很好。

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

因此,您的用户无法登录,因为他们从未到达允许他们在您之前过滤掉的情况下登录的Servlet

当没有用户登录时,您必须扩展过滤器以允许登录。这可以通过以下方式完成:通过更改url-pattern的{​​{1}}。