File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Is there anyboby has used the Filter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is there anyboby has used the Filter?" Watch "Is there anyboby has used the Filter?" New topic

Is there anyboby has used the Filter?

Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
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());
filterChain.doFilter(request, response);
catch(ServletException sx) {
catch(IOException iox) {
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

Joined: Mar 22, 2002
Posts: 1055
Please see this link for an example.
I agree. Here's the link:
subject: Is there anyboby has used the Filter?
jQuery in Action, 3rd edition