简体   繁体   中英

Null point always

It all ways passing null point after this line, 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. here I want to filter every url and proceed only valid login users requests. here my logic..

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>

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

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 . The Servlet is always the last element in the chain.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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