Results 1 to 6 of 6
  1. #1
    agrawal1294 is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Question Having trouble implementing Filter interface for validation

    What m doing is.
    Whenever a user successfully logs in to my web application its sets the loggedUserType attribute as the student or teacher.
    Now i want that some pages should be available to teacher only
    so for this m using filters
    This is my code for filter

    Java Code:
    package ankit;
    import javax.servlet.Filter;
    import javax.servlet.http.*;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    
    public class SessionCheckFilter implements Filter {
    
      private String contextPath,userType;
    
      public void init(FilterConfig fc) throws ServletException {
        contextPath = fc.getServletContext().getContextPath();
      }
    
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
    	userType = String.parseString(req.getSession().getAttribute("loggedUserType"));
        if (req.getSession().getAttribute("loggedUserType") == null) { //checks if there's a loggedUserType set in session...
            res.sendRedirect("http://localhost:8080/vt/index.jsp"); //or page where you want to redirect
        } else {
          System.out.println("Inside Filter="+userType);
          if (!userType.equals("teacher")){ //check if user type is not teacher
            res.sendRedirect("http://localhost:8080/vt/index.jsp"); //or page where you want to
          	}
            // This if part is always executed even though the value of userType is teacher.
          fc.doFilter(request, response);
        }
      }
    
      public void destroy() {
      }
    }
    This is web.xml file content for filter mapping
    Java Code:
    <filter>
        <filter-name>SessionCheckFilter</filter-name>
        <filter-class>ankit.SessionCheckFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SessionCheckFilter</filter-name>
        <url-pattern>/teacher.jsp</url-pattern> <!-- url from where you implement the filtering -->
    </filter-mapping>
    i want that only user with loggerUserType attribute as teacher should be allowed to view /teacher.jsp page
    but in my case even teacher are being redirected to home page i.e. index.jsp

    Whats wrong in my code?
    can anyone please help me
    Last edited by agrawal1294; 12-29-2014 at 12:44 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    14

    Default Re: Having trouble implementing Filter interface for validation

    You should add return statements after you do a sendRedirect() to stop the code from doing anything further.

    Java Code:
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {
    
      HttpServletRequest req = (HttpServletRequest) request;
      HttpServletResponse res = (HttpServletResponse) response;
      userType = String.parseString(req.getSession().getAttribute("loggedUserType"));
      if (req.getSession().getAttribute("loggedUserType") == null) { //checks if there's a loggedUserType set in session...
          res.sendRedirect("http://localhost:8080/vt/index.jsp"); //or page where you want to redirect
          return;
      } else {
        System.out.println("Inside Filter="+userType);
        if (!userType.equals("teacher")){ //check if user type is not teacher
          res.sendRedirect("http://localhost:8080/vt/index.jsp"); //or page where you want to
          return;
        }
      }
      
      // if code did not prematurely return, send further up the filter chain
      fc.doFilter(request, response);
    }
    Basically you want to create the situation where EITHER your filter acts and redirects, OR it doesn't do anything in which case the request is passed further up the filter chain with doFilter().
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    agrawal1294 is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default Re: Having trouble implementing Filter interface for validation

    actually the filter is working exactly it should.
    the prblem is with the if condition
    if (!userType.equals("teacher")){ //check if user type is not teacher
    res.sendRedirect("http://localhost:8080/vt/index.jsp"); //or page where you want to
    return;
    }
    this block is being executed everytime even though the value of userType is "teacher"

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    15

    Default Re: Having trouble implementing Filter interface for validation

    Quote Originally Posted by agrawal1294 View Post
    actually the filter is working exactly it should.
    the prblem is with the if condition

    this block is being executed everytime even though the value of userType is "teacher"
    That does not make sense. If the value of userType is not "teacher", the block executes. If the value of userType is "teacher", the block does not execute.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    14

    Default Re: Having trouble implementing Filter interface for validation

    Quote Originally Posted by agrawal1294 View Post
    actually the filter is working exactly it should.
    You contradict yourself. If the filter was working exactly as it should, you wouldn't be asking here about a problem. Good luck with that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    agrawal1294 is offline Member
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default Re: Having trouble implementing Filter interface for validation

    solved solved

    Actually i was fetching the userType from database and then was setting it as an session Attribute. Therefore the data fetch from database was of length varchar(10) whereas teacher is of only 7 char long. so the last 3 char were spaces..
    and when i was checking userType.equals("teacher");
    the program was actually checking "teacher " with "teacher" thats the reason why the if block was being excuted everytime :)
    after retrieving the session attribute, i just trimmed it and then checked with "teacher" and its working just fine as it should
    any way thanks :)

Similar Threads

  1. NEED HELP in IMPLEMENTING A GIVEN INTERFACE!
    By wasiqjaved in forum New To Java
    Replies: 0
    Last Post: 10-03-2011, 12:40 PM
  2. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 03:33 PM
  3. help! implementing an interface
    By manda147 in forum New To Java
    Replies: 28
    Last Post: 11-17-2008, 04:27 AM
  4. Implementing an interface
    By bugger in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2008, 01:35 PM
  5. Implementing Serializable interface
    By javaplus in forum Advanced Java
    Replies: 4
    Last Post: 12-18-2007, 12:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •