简体   繁体   English

为什么我的Servlet筛选器程序显示404 http错误?

[英]Why my Servlet Filter program is showing 404 http error?

I'm a beginner and trying for deploying a program for Log fileter but it's showing error: 我是一个初学者,正在尝试为Log fileter部署程序,但显示错误:

HTTP Status 404 - /LogFilter

type Status report

message /LogFilter

description The requested resource is not available.
Apache Tomcat/8.0.5

Here is my URL when put this to run: http://localhost:9999/LogFilter 当运行此命令时,这是我的URL: http://localhost:9999/LogFilter

and here is my source code for LogFilter : 这是我的LogFilter源代码:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class LogFilter implements Filter{

    public void init(FilterConfig config)throws ServletException{
        String testParam=config.getInitParameter("test-param");

        //here we are printing the testParam...
        System.out.println("Test param: "+testParam);
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws ServletException, IOException{
        String ipAddress=request.getRemoteAddr();
        System.out.println("IP address: "+ipAddress+" Data: "+new Date().toString());

        chain.doFilter(request, response);
    }

    public void destroy(){}
}

And here is my source code for web.xml : 这是我的web.xml源代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <filter>
        <filter-name>LogFilter</filter-name>
        <filter-class>LogFilter</filter-class>
        <init-param>
            <param-name>test-param</param-name>
            <param-value>Initialization Paramter</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>LogFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

A filter offers a useful way of performing filtered functionality in a Java web application. 过滤器提供了一种在Java Web应用程序中执行过滤功能的有用方法。 Typically, filters do not generate content themselves. 通常,过滤器本身不会生成内容。

Use a Filter when you want to filter and/or modify requests based on specific conditions. 当您要根据特定条件过滤和/或修改请求时,请使用“过滤器”。 Use a Servlet when you want to control, preprocess and/or postprocess requests. 当您要控制,预处理和/或后处理请求时,请使用Servlet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM