manic bubble
manic bubble

Reputation: 147

class not found exception for Servlet filter

I'm trying to create a basic servlet filter, I have created the filter and mapped it in the web.xml file but get filter not found exceptions.

Here's the web.xml file

 <display-name>Disertation</display-name>
  <filter>
       <filter-name>AuthorizationFilter</filter-name>
        <filter-class>Disertation.servlets.AuthorizationFilter</filter-class>       
    </filter>   
    <filter-mapping>
        <filter-name>AuthorizationFilter</filter-name>
        <servlet-name>LoginServlet</servlet-name>
    </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Here is the path for the filter class: Disertation/src/servlets/AuthorizationFilter.java

Am i missing something or is my configuration wrong?

EDIT: I took the com out of the web.xml and Here is the filter class

    package servlets;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import JavaBeans.User;


/**
 * Servlet Filter implementation class AuthorizationFilter.
 * Its purpose is to check logged-in user's role and
 * and accordingly allow or prevent access to the web resources.
 */
public class AuthorizationFilter implements Filter {
    private FilterConfig filterConfig;

    /**
     * @see Filter#init(FilterConfig)
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        ServletContext sc= filterConfig.getServletContext();
     String username = request.getParameter("user");
     String pwd = request.getParameter("pwd");
     System.out.println("first check");

     User user = new User (username, pwd, "employee");
     System.out.println("is this getting here?");
     request.setAttribute("role", user.getRole());
        if (request.getAttribute("role").equals("employee")|| request.getAttribute("role").equals("admin")) 

            chain.doFilter(request, res);

    }


    /**
     * @see Filter#destroy()
     */
    @Override
    public void destroy() {
        filterConfig=null;
    }

}

Upvotes: 2

Views: 3274

Answers (2)

mkk
mkk

Reputation: 36

Are you using the correct package? In AuthorizationFilter.java look for the package name that is declared at the top of the file. Then put that in your web.xml:

<filter-class>com.some.package.AuthorizationFilter</filter-class> 

Also, there is a space in your closing tag after the hyphen

Upvotes: 0

Feras Odeh
Feras Odeh

Reputation: 9296

Your filter should be under the following directory Disertation/src/com/Disertation/servlets/AuthorizationFilter.java to match your filter configuration or change your filter configuration to

 <filter-class>servlets.AuthorizationFilter</filter- class> 

Upvotes: 1

Related Questions