EJ Dogar
EJ Dogar

Reputation: 68

Authentication Filter in java, help me in removing this error

the error comes at "session = request.getSession();" when i alter dofilter() method as a normal method "dofilter(HttpServletRequest request, HttpServletResponse response)" then i have to make a real "dofilter(ServletRequest request, ServletResponse response)" method.

package pk.edu.zab.cs;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;

/**
* Servlet Filter implementation class AuthenticationFilter
*/
public class AuthenticationFilter implements Filter {

/**
 * Default constructor. 
 */
public AuthenticationFilter() {
    // TODO Auto-generated constructor stub
}

/**
 * @see Filter#destroy()
 */
public void destroy() {
    // TODO Auto-generated method stub
}

/**
 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    // place your code here
      String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = null;
        RequestDispatcher dispatcher = null;
        if (("admin".equalsIgnoreCase(username) && "mypass".equals(password))) {
            session = request.getSession(); //the error comes here but when i remove i have to make this dofilter() method as a normal method which use HttpServletRequest and afterwards I have to make the original dofilter() method with ServletRequest. 
            session.setAttribute("username", username);
            dispatcher = request.getRequestDispatcher("Welcome.jsp");
        } else {
            dispatcher = request.getRequestDispatcher("failure.jsp");
        }
        dispatcher.forward(request, response);
    // pass the request along the filter chain
    chain.doFilter(request, response);
}

/**
 * @see Filter#init(FilterConfig)
 */
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
}
}

Upvotes: 0

Views: 2012

Answers (1)

JB Nizet
JB Nizet

Reputation: 691655

When you override a method, the signature of your method must match with the signature of the overridden method. So you can't change the type of the arguments of the method. That's basic OO, that you should learn before using servlets.

Now, you know that the servlet request, of type ServletRequest, is actually an HttpServletRequest. So cast it, to be able to get the associated session:

HttpServletRequest httpRequest = (HttpServletRequest) request;
session = httpRequest.getSession();

And, final note: always read error messages. They contain valuable information. If you can't understad them, then post them, so that we know precisely what and where the error is, and that we can explain the message to you.

Upvotes: 3

Related Questions