我有一个 login.java servlet,在获取用户名和密码后,如果正确,我们将被定向到 welcome.java servlet。名为 loginfilter.java 的过滤器验证用户名和密码。如果用户名和密码不正确,我会调用一个 errorpage.java servlet。
我的过滤器不工作,我不知道为什么。我认为我的 web.xml 代码和过滤器代码是正确的。请帮我找出错误。
这是我的过滤器的代码
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.");
}
}
这是 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>