簡體   English   中英

如何使運行servlet過濾器?

[英]How to make run a servlet filter?

對於我的Web應用程序,我已經創建了登錄頁面。 為了阻止訪問其他頁面,我設置了一個過濾器。 但是,在運行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
}
}

我要測試的URL是http:// localhost:8080 / StaffManagementSystem / EmployeeManagement.html

我正在使用碼頭作為服務器。

任何建議將不勝感激!!! 提前致謝!!


最終更新:

提到的所有更改均已完成。 它正在編譯。 我什至無法在控制台中獲得“ sysout”輸出。 它只是傳遞URL。

如果您的網址是http://localhost:8080/StaffManagementSystem/EmployeeManagement.html ,那么您不是要將此模式用於過濾器映射:

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

而且您的web.xml中的其他地方可能有錯誤。 您不是在配置的任何地方都將Filter用作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。 而是選擇loggers並檢查servlet容器的日志。

不確定,但我認為您在這里遇到問題:

if(name == null){//我必須重定向到要索引頁面的人。 (((HttpServletResponse) request ).sendRedirect(“ index.html”);

將其更改為響應

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM