如何运行servlet过滤器?

时间:2010-09-08 19:24:30

标签: java servlets servlet-filters

对于我的网络应用程序,我创建了登录页面。为了阻止访问其他页面,我设置了一个过滤器。但在运行网络应用时,它会提供Servlet class com.pricar.grid.AuthenticationFilter is not a javax.servlet.Servlet

我也无法得到正确的结果。

此处我的代码:在web.xml中过滤配置

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Staff Management</display-name>
<filter>
    <filter-name>AuthenticationFilter</filter-name>
    <display-name>AuthenticationFilter</display-name>
    <description></description>
        <filter-class>com.pricar.grid.AuthenticationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>AuthenticationFilter</filter-name>
    <url-pattern>/StaffManagementSystem/*</url-pattern>
    </filter-mapping>
   <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <display-name>DWR Servlet</display-name>
        <description>Direct Web Remoter Servlet</description>
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>UserLogin.html</welcome-file>
    </welcome-file-list> 
    </web-app>

过滤器代码为:

package com.pricar.grid;

public class AuthenticationFilter implements Filter {
public AuthenticationFilter() {
    // TODO Auto-generated constructor stub
}
public void destroy() {
    // TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    WebContext ctx = WebContextFactory.get();
    HttpServletRequest req = ctx.getHttpServletRequest();
    HttpSession session = req.getSession(false);
    String name = (String) session.getAttribute("userName");
    System.out.print ("Within Simple Filter ... ");
    System.out.println ("Filtering the Request ...");
    System.out.print ("Within Simple Filter ... ");
    System.out.println ("Filtering the Response ...");
    if ( name == null ){
        //I have to redirect to the person to index page.
        ((HttpServletResponse) response).sendRedirect("index.html");
    }
    chain.doFilter(request, response);  
}
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
}
}

我要测试的网址是http://localhost:8080/StaffManagementSystem/EmployeeManagement.html

我使用jetty作为服务器。

任何建议都会感激不尽! 提前致谢!!


最终更新:

提到的所有更改都已完成。它被编译。我甚至无法在我的控制台中获得“sysout”输出。它只是传递URL。

2 个答案:

答案 0 :(得分:2)

如果您的网址为http://localhost:8080/StaffManagementSystem/EmployeeManagement.html,则表示您不想将此模式用于过滤器映射:

    <url-pattern>/StaffManagementSystem/*</url-pattern>

您的web.xml可能在其他地方出错。您是否在配置中的任何位置使用过滤器作为Servlet?


您的doFilter()方法在检查值之前会执行chain.doFilter (request, response);。你也应该删除这一行。


最后,doFilter()会进行重定向。您应该删除整个else部分。


实施Filter意味着您必须编写方法:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

(不是HttpServlet ******)


您应该避免在Web应用程序中使用System.out。而是选择记录器并检查servlet容器的日志。

答案 1 :(得分:1)

不确定,但我认为你有问题:

  

if(name == null){           //我必须重定向到索引页面的人。           ((HttpServletResponse)请求)。sendRedirect(“index.html”);

将其更改为回复