简体   繁体   English

总是零点

[英]Null point always

It all ways passing null point after this line, System.out.println("Inside the filter.............." ) ; System.out.println("Inside the filter.............." )在该行之后传递空点, System.out.println("Inside the filter.............." ) ; so can anyone tell me what's wrong in my code and logic 所以谁能告诉我我的代码和逻辑有什么问题

and home.jsp(index page) did not display. 和home.jsp(索引页)未显示。 here I want to filter every url and proceed only valid login users requests. 在这里,我想过滤每个URL,并仅处理有效的登录用户请求。 here my logic.. 这是我的逻辑

UserServlet.java 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 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 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>

stack trace 堆栈跟踪

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

Thank you.. 谢谢..

You have a real simple problem: The user is not logged in ;) 您有一个真正的简单问题:用户未登录;)

To clarify things: 要澄清的事情:

In you Filter you're checking if the user is logged in and in this case execute the chain with 在“ Filter您正在检查用户是否已登录,在这种情况下,使用

chain.doFilter(req, resp);

That's fine so far. 到目前为止还好。

But what happens if the user is not logged in? 但是,如果用户未登录怎么办? In this case you're not executing the chain and therefore no Servlet . 在这种情况下,您没有执行链,因此没有Servlet The Servlet is always the last element in the chain. Servlet始终是链中的最后一个元素。

So your users cannot log in as they never get to the Servlet that allows them to log in as you filter them out before. 因此,您的用户无法登录,因为他们从未访问过允许您在过滤之前登录的Servlet

You have to extend your filter to allow logins when no user is logged in. This can be done eg by changing the url-pattern of the Filter . 您必须扩展过滤器以允许在没有用户登录时允许登录。这可以例如通过更改Filterurl-pattern来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM