• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there anyboby has used the Filter?

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see this link for an example.
 
reply
    Bookmark Topic Watch Topic
  • New Topic