Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone tell me whatz wrong with my logic? It all ways passing null point after this line,
Java
System.out.println("Inside the filter.............."  );
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..

FilterRequest.java

Java
try{
         HttpServletRequest request = (HttpServletRequest) req;
             HttpServletResponse response = (HttpServletResponse) resp;
             System.out.println("Inside the filter.............."  );
         HttpSession session = request.getSession();
         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{
             System.out.println("user does exits..");
             String message = "Please Login!";
             req.setAttribute("loginMsg", message);
             //response.sendRedirect("login2.jsp");
         }
     }catch(Exception e){
         e.printStackTrace();
     }


web.xml

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>


Thank you!
Posted
Updated 10-Jul-13 19:21pm
v2
Comments
mali_angel 11-Jul-13 1:45am    
In here I wanted to show index page and after that all requests should filter and process.

1 solution

The problem is here
Java
HttpSession session = request.getSession();
		  User u = null;
		  if(session.getAttribute("loggedUser")!=null){


you are looking in the request for any existing session. so for the first time it will return null.
And your doing the null.getAttribute(). that is the reason you get the exception.

try this
Java
HttpSession session = request.getSession(true);


It first check if any session exist then return it otherwise create one and return.
 
Share this answer
 
v2
Comments
mali_angel 11-Jul-13 5:36am    
thank u :)
Shubhashish_Mandal 11-Jul-13 5:48am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900