This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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?
Similar Threads
Session is lost in new browser window.
Simple login filter causing issues
setting attribute in session in filter
Problems with servlet filter
Servlet Filter problems