为什么给 web.xml 添加过滤器会导致 404 错误?
Why does adding filter to web.xml cause 404 error?
为了 study/practice 使用 struts 2,我使用 struts2-archetype-starter maven 原型。
我在尝试添加自己的过滤器时遇到了问题,希望有人能为我指明正确的方向。
我在构建过程中使用 eclipse 和 maven,Tomcat 8.5 作为本地主机服务器。
我已经能够设置一些基本操作。我现在正在尝试添加一个过滤器来设置请求的编码,以便我可以处理日语输入。为此,我参考了有关过滤器的资源,以创建我自己的自定义过滤器,我在我的项目 web.xml 文件中引用了它
筛选参考来源:https://www.oracle.com/java/technologies/filters.html
但是,当我尝试访问我的项目 url 时,出现 404 错误。
我已经尝试在我的过滤器中添加断点并在服务器上调试项目,但从未命中断点。 (否则我可以调试和使用断点)
我在 web.xml 文件中添加了过滤器声明:
<filter>
<filter-name>MyEncoder</filter-name>
<filter-class>jono_group.mav_arch_2.filters.MyChaEnFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>Shift_JIS</param-value>
</init-param>
</filter>
和这个过滤器映射
<filter-mapping>
<filter-name>MyEncoder</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
它们分别位于所有其他过滤器和过滤器映射的前面,目的是在过滤器链的前面执行。
使用上面的过滤器和过滤器映射,maven build (clean package runs successfully, reporting no errors. 但是我得到了 404。一旦我删除了它们,404 错误就消失了,我的操作按预期工作。
如有任何帮助,我们将不胜感激。
我的过滤器class如下:
package jono_group.mav_arch_2.filters;
import java.io.IOException;
import java.nio.file.DirectoryStream.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyChaEnFilter implements Filter
{
private FilterConfig filterConfig = null;
private String encoding;
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain) throws
IOException, ServletException {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws
ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
public void destroy() {
this.filterConfig = null;
}
@Override
public boolean accept(Object entry) throws IOException {
// TODO Auto-generated method stub
return false;
}
}
import java.nio.file.DirectoryStream.Filter
不是 servlet 过滤器。
来自您提供的 link,具体来自 Programming Filters 部分:
The filter API is defined by the Filter
, FilterChain
, and FilterConfig
interfaces in the javax.servlet package.
FilterChain
和 FilterConfig
正确导入,Filter
没那么多。
为了 study/practice 使用 struts 2,我使用 struts2-archetype-starter maven 原型。
我在尝试添加自己的过滤器时遇到了问题,希望有人能为我指明正确的方向。
我在构建过程中使用 eclipse 和 maven,Tomcat 8.5 作为本地主机服务器。
我已经能够设置一些基本操作。我现在正在尝试添加一个过滤器来设置请求的编码,以便我可以处理日语输入。为此,我参考了有关过滤器的资源,以创建我自己的自定义过滤器,我在我的项目 web.xml 文件中引用了它
筛选参考来源:https://www.oracle.com/java/technologies/filters.html
但是,当我尝试访问我的项目 url 时,出现 404 错误。
我已经尝试在我的过滤器中添加断点并在服务器上调试项目,但从未命中断点。 (否则我可以调试和使用断点)
我在 web.xml 文件中添加了过滤器声明:
<filter>
<filter-name>MyEncoder</filter-name>
<filter-class>jono_group.mav_arch_2.filters.MyChaEnFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>Shift_JIS</param-value>
</init-param>
</filter>
和这个过滤器映射
<filter-mapping>
<filter-name>MyEncoder</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
它们分别位于所有其他过滤器和过滤器映射的前面,目的是在过滤器链的前面执行。
使用上面的过滤器和过滤器映射,maven build (clean package runs successfully, reporting no errors. 但是我得到了 404。一旦我删除了它们,404 错误就消失了,我的操作按预期工作。
如有任何帮助,我们将不胜感激。
我的过滤器class如下:
package jono_group.mav_arch_2.filters;
import java.io.IOException;
import java.nio.file.DirectoryStream.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyChaEnFilter implements Filter
{
private FilterConfig filterConfig = null;
private String encoding;
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain) throws
IOException, ServletException {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws
ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
public void destroy() {
this.filterConfig = null;
}
@Override
public boolean accept(Object entry) throws IOException {
// TODO Auto-generated method stub
return false;
}
}
import java.nio.file.DirectoryStream.Filter
不是 servlet 过滤器。
来自您提供的 link,具体来自 Programming Filters 部分:
The filter API is defined by the
Filter
,FilterChain
, andFilterConfig
interfaces in the javax.servlet package.
FilterChain
和 FilterConfig
正确导入,Filter
没那么多。