【问题标题】:How to make run a servlet filter?如何运行 servlet 过滤器?
【发布时间】:2011-04-09 22:20:25
【问题描述】:

对于我的 Web 应用程序,我创建了登录页面。为了阻止对其他页面的访问,我设置了一个过滤器。但是在运行网络应用程序时,它会提供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

我正在使用码头作为服务器。

任何建议都将不胜感激!!! 提前致谢!


最终更新:

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

【问题讨论】:

  • web.xml 中有 servlet-mappings 吗?
  • @matt b:现在更新了我的问题。看看吧!!!

标签: java servlets servlet-filters


【解决方案1】:

如果您的 URL 是 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 容器的日志。

【讨论】:

  • @Colin HEBERT:是的……我看过……然后删除了。我以为是问题制造者。是这样吗??!!!
  • @BalusC:最大的问题是什么?
  • @Colin:这些与 实际 问题无关。它仍然存在(并且 OP 在编辑期间删除了提示)。
  • 能否将请求视为响应? ((HttpServletResponse) request) 还是作为 WebContext? ((WebContext) request) 我猜真正的问题是程序员不了解他/她正在使用的 API :-)
  • @rsp:OP 之后改变了这一点,显然是在黑暗中拍摄。现在的代码甚至无法编译。
【解决方案2】:

不确定,但我认为您在这里遇到了问题:

如果(名称==空){ //我必须重定向到要索引页面的人。 ((HttpServletResponse) 请求).sendRedirect("index.html");

将其更改为响应

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 2011-12-24
    • 2017-01-13
    • 1970-01-01
    • 2012-06-24
    • 2012-02-17
    • 2011-02-26
    • 2013-04-09
    相关资源
    最近更新 更多