英文:
How to create correctly a Filter in Java EE 8 from Java code?
问题
我想通过Java代码在Java EE 8中以编程方式创建一个过滤器,所以我在我的应用程序中编写了以下代码。
我的过滤器是 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("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("Goodbie from LoginFilter");
}
}
I have in my 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>
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("I am running the LoginFilter tasks.");
filterChain.doFilter(request, response)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论