• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there anyboby has used the Filter?

 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a website using session. each user of the website must have session. and there are three user categories: guest, common user, manager. and every user category has a corresponding class: Guest, Common, Manager.
When design and coding, i encountered a problem.
i don't want anybody nevigate my website without a session, and i don't want them type the url directely(which means they can get everything they want). I heard there's a Filter servlet after Servlet 2.3. I read some articles about that and then wrote one. the doFilter is quite simple, the main code is as follows:
try {
HttpSession session=((HttpServletRequest)request).getSession();
if (session.getAttribute("user").equals(null)){
session.setAttribute("user",new callcenter.web.Guest());
((HttpServletResponse)response).sendRedirect("guest.jsp");
return;
}
filterChain.doFilter(request, response);
}
catch(ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
catch(IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
after building and deployment and run, the result is NullPointerException. I don't know why. maybe there is something wrong with the casting to HttpSession. Because I don't know how to use session in the Filter servlet. can anybody help me?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see this link for an example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic