转发到索引页面的过滤器
filter that forwards to index page
我想制作一个过滤器,用于ward 到 /WEB-INF/index.html
请求看起来像这样的应用程序
http://localhost:8080/basic-application-web
这是我的过滤器
public class RootFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
if (req.getRequestURI().equals("/basic%2Dapplication%2Dweb/")) {
req.getRequestDispatcher("/WEB-INF/index.html").forward(req, resp);
}
chain.doFilter(request, response);
}
@Override
public void destroy() {}
}
我的web.xml
看起来像这样
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://java.sun.com/xml/ns/javaee'
xmlns:web='http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'
xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaeeweb-app_2_5.xsd'
id='basic_web' version='2.5'>
<display-name>Basic web application</display-name>
<servlet>
<servlet-name>serviceServlet</servlet-name>
<servlet-class>com.pack.ServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serviceServlet</servlet-name>
<url-pattern>/messaging</url-pattern>
</servlet-mapping>
<filter>
<filter-name>rootFilter</filter-name>
<filter-class>com.pack.RootFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>rootFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
然而,我有时会遇到一些奇怪的行为,当我尝试直接使用URL。
虽然通过 tomcat 管理器它工作正常。问题是什么?可能是因为缺少 root
servlet?
我将 index.html
移到了 WEB-INF
之外,所以基本上布局开始看起来像这样
webapp
WEB-INF\web.xml
index.html
并调整 filter
转发到 /index.html
而不是 WEB-INF/index.html
req.getRequestDispatcher("/WEB-INF/index.html").forward(req, resp);
有帮助。
我想制作一个过滤器,用于ward 到 /WEB-INF/index.html
请求看起来像这样的应用程序
http://localhost:8080/basic-application-web
这是我的过滤器
public class RootFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
if (req.getRequestURI().equals("/basic%2Dapplication%2Dweb/")) {
req.getRequestDispatcher("/WEB-INF/index.html").forward(req, resp);
}
chain.doFilter(request, response);
}
@Override
public void destroy() {}
}
我的web.xml
看起来像这样
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://java.sun.com/xml/ns/javaee'
xmlns:web='http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'
xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaeeweb-app_2_5.xsd'
id='basic_web' version='2.5'>
<display-name>Basic web application</display-name>
<servlet>
<servlet-name>serviceServlet</servlet-name>
<servlet-class>com.pack.ServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serviceServlet</servlet-name>
<url-pattern>/messaging</url-pattern>
</servlet-mapping>
<filter>
<filter-name>rootFilter</filter-name>
<filter-class>com.pack.RootFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>rootFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
然而,我有时会遇到一些奇怪的行为,当我尝试直接使用URL。
虽然通过 tomcat 管理器它工作正常。问题是什么?可能是因为缺少 root
servlet?
我将 index.html
移到了 WEB-INF
之外,所以基本上布局开始看起来像这样
webapp
WEB-INF\web.xml
index.html
并调整 filter
转发到 /index.html
而不是 WEB-INF/index.html
req.getRequestDispatcher("/WEB-INF/index.html").forward(req, resp);
有帮助。