如何在Java EE 8中从Java代码正确创建过滤器?

huangapple go评论74阅读模式
英文:

How to create correctly a Filter in Java EE 8 from Java code?

问题

我想通过Java代码在Java EE 8中以编程方式创建一个过滤器,所以我在我的应用程序中编写了以下代码。

Gif 演示: gif 演示一 - gif 演示二

我的过滤器是 LoginFilter.java

package afrominga.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Hello from LoginFilter.");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("I am running the LoginFilter tasks.");
    }

    @Override
    public void destroy() {
        System.out.println("Goodbye from LoginFilter");
    }
}

我在我的 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_4_0.xsd"
         version="4.0">
    <display-name>Archetype Created Web Application</display-name>

    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>afrominga.filters.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/error/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/error/500.jsp</location>
    </error-page>
</web-app>

因此,在打开我的浏览器并使用应用程序的根路径上下文时,过滤器会在输出中打印System.out的文本,但我的页面是空白的,不显示主页。我不明白为什么会发生这种情况,应该转发到我的主页,因为它只在控制台中打印文本。

有人可以帮助我吗?请

英文:

I want to create programmatically a Filter in Java EE 8 from Java Code, so I did this code in my application.

Gif captures: gif capture one - gif capture two

My filter is LoginFilter.java

package afrominga.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(&quot;Hello from LoginFilter.&quot;);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println(&quot;I am running the LoginFilter tasks.&quot;);
    }

    @Override
    public void destroy() {
        System.out.println(&quot;Goodbie from LoginFilter&quot;);
    }
}

I have in my web.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;
         version=&quot;4.0&quot;&gt;
    &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;

    &lt;filter&gt;
        &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt;
        &lt;filter-class&gt;afrominga.filters.LoginFilter&lt;/filter-class&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
        &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;

    &lt;error-page&gt;
        &lt;error-code&gt;404&lt;/error-code&gt;
        &lt;location&gt;/error/404.jsp&lt;/location&gt;
    &lt;/error-page&gt;
    &lt;error-page&gt;
        &lt;error-code&gt;500&lt;/error-code&gt;
        &lt;location&gt;/error/500.jsp&lt;/location&gt;
    &lt;/error-page&gt;
&lt;/web-app&gt;

So, at the time of to open my browser with the root path context of my application, the filter print in the output the text of my System.out, but my page is left blank and does not show me the home page. I don't understand why this happens, this would must forward to my home page because only print a text in my console.

can would someone help me? please

答案1

得分: 2

一个问题是你的doFilter()方法。现在,你的过滤器只是打印而没有将请求传递给下一个过滤器/你的应用程序。你只需要添加一行。

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("我正在运行LoginFilter任务。");
        chain.doFilter(request, response);
    }
英文:

One problem is that your doFilter() method. Right now, your filter is just printing and not passing the request along to the next filter/your application. You just need to add one line.

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println(&quot;I am running the LoginFilter tasks.&quot;);
        filterChain.doFilter(request, response)
    }

huangapple
  • 本文由 发表于 2020年8月7日 06:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63292870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定