Servlet过滤器无法正常工作

时间:2013-08-14 18:28:29

标签: java servlets servlet-filters web.xml

我有一个login.java servlet,在获取用户名和密码之后,如果正确,我们将被定向到welcome.java servlet。名为loginfilter.java的过滤器会验证用户名和密码。如果用户名和密码不正确,我有一个errorpage.java servlet然后被调用。

我的过滤器无法正常工作,我不知道为什么。我认为我的web.xml代码和过滤器代码是正确的。 Plz帮我找出错误。

以下是我的过滤器的代码

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>

2 个答案:

答案 0 :(得分:3)

删除行:

throw new UnsupportedOperationException("Not supported yet.");

这些行会阻止过滤器初始化,并且还会停止应用程序。

  

服务器调用init(FilterConfig)一次以准备服务过滤器,然后针对专门设置使用过滤器的请求多次调用doFilter()FilterConfig接口具有检索过滤器名称,其init参数和活动servlet上下文的方法。服务器调用destroy()以指示过滤器已停止服务。

http://www.servlets.com/soapbox/filters.html

答案 1 :(得分:1)

init方法中的抛出异常无效 它将停止过滤器的初始化 删除以下行

  throw new UnsupportedOperationException("Not supported yet.");