Reputation: 65
I have a login.java servlet, from where after taking the username and password, if correct, we are directed to welcome.java servlet.A filter named loginfilter.java verifies the username and password. If the username and password are not correct, i have an errorpage.java servlet which is then called.
My filter is not working which i dont know why. i think my web.xml code and filters code are correct. Plz help me find out the error.
Here is the code for my filter
package filters;
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;
public class loginfilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain throws IOException, ServletException {
String u=request.getParameter("username");
String p=request.getParameter("password");
String user="akshay";
String pass="akshay";
if (u.equals(user) && p.equals(pass)){
chain.doFilter(request,response);
}
}
@Override
public void destroy() {
throw new UnsupportedOperationException("Not supported yet.");
}
private RequestDispatcher getReuestDispatcher(String loginpage) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Here is the code for web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>login</servlet-class>
</servlet>
<servlet>
<servlet-name>welcomepage</servlet-name>
<servlet-class>welcomepage</servlet-class>
</servlet>
<servlet>
<servlet-name>errorpage</servlet-name>
<servlet-class>errorpage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/errorpage</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/errorpage</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errorpage</location>
</error-page>
<error-page>
<exception-type>javax.io.IOException</exception-type>
<location>/errorpage</location>
</error-page>
<filter>
<filter-name>loginfilter</filter-name>
<filter-class>filters.loginfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginfilter</filter-name>
<servlet-name>welcomepage</servlet-name>
</filter-mapping>
<servlet-mapping>
<servlet-name>welcomepage</servlet-name>
<url-pattern>/welcomepage</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>errorpage</servlet-name>
<url-pattern>/errorpage</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Upvotes: 0
Views: 6851
Reputation: 114
Throwing exception in init
method is not valid
It will stop the initialization of the filter.
Remove below line
throw new UnsupportedOperationException("Not supported yet.");
Upvotes: 1
Reputation: 42060
Remove the lines with:
throw new UnsupportedOperationException("Not supported yet.");
These lines prevent the filter is initialized, and also stops the application.
The server calls
init(FilterConfig)
once to prepare the filter for service, then callsdoFilter()
any number of times for requests specially set up to use the filter. TheFilterConfig
interface has methods to retrieve the filter's name, its init parameters, and the active servlet context. The server callsdestroy()
to indicate that the filter is being taken out of service.
http://www.servlets.com/soapbox/filters.html
Upvotes: 3