繁体   English   中英

浏览器参数FilterConfig?=null

[英]browser parameter FilterConfig?=null

当我在 url 地址中键入以下内容时,为什么我得到 null 的参数值? http://192.168.0.38:8080/demo-rest//SimpleServlet?example-param=freddie

21-02-14 19:39:56:567 INFO 默认任务 1 标准输出:71 - 测试参数 null

21-02-14 19:39:56:568 信息默认任务 1 示例.SimpleHttpServlet:29 - SimpleServlet::constructor 21-02-14 19:39:56:568 信息默认任务 1 示例.SimpleHttpServlet:34 - SimpleServlet::init() 21-02-14 19:39:56:569 INFO 默认任务 1 标准输出:71 - IP 192.168.0.38 时间是 2 月 14 日星期日 19:39:56 GMT 2021 21-02-14 19: 39:56:569 INFO 默认任务 1 示例。SimpleHttpServlet:47 - SimpleServlet::doGet

@WebFilter("/*") public class LoggingFilter 实现过滤器 {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    // Log the IP address and get the current time stamp.       
    String ipAddress = request.getRemoteAddr();
    System.out.println("IP "+ ipAddress + " Time is " + new Date().toString());
    chain.doFilter(request, response);      
}

public void init(FilterConfig config) throws ServletException {
    
    // Get init Parameter
    String testParam = config.getInitParameter("example-param");
    // print the init Parameter
    System.out.println("Test Param " + testParam);              
}

public void Destroy() {
    // release any resource
}

}

我已经对其进行了排序,但我想知道是否可以不使用 web.xml 00:18:38,639 21-02-15 00:18:53:809FO 默认任务-1 FreddyFilter:2 INFO 默认任务-1 示例 - 测试记录。 RocknRolla 12 秒前 编辑 删除

I have discovered that if you use web.xml to store init-param then an object of the concrete class of interface javax.servlet.Filter is called twice.Your code will run twice, picking the init-param value once and null the second时间。 但是,如果您对 webinitparams 使用注释,则 object 方法只会被调用一次。 奇怪但真实。!

@WebFilter(filterName = "filterSimpleServlet", urlPatterns ={"/SimpleServlet"}, initParams = {@WebInitParam(name = "mood", value = "awake")})

暂无
暂无

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

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