我正在JSP中开发一个应用程序,我知道Apache Tomcat不支持.htaccess文件。
我想将所有请求重定向到index.jsp以进行URI路由
在.htaccess文件中我有
Options -Indexes -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
如何使用Tomcat 7实现JSP URI重写?
答案 0 :(得分:2)
您可以使用应用程序中的过滤器
来执行此操作创建Filter类。像这样:
public class SessionFilter implements javax.servlet.Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
RequestDispatcher requestDispatcher = req.getRequestDispatcher("index.jsp");
try {
requestDispatcher.forward(req, res);
} catch (ServletException e) {
} catch (IOException e) {
}
}
@Override
public void destroy() {}
}
然后在您的部署描述符(web.xml)中包含以下内容:
<filter>
<filter-name>MySessionFilter</filter-name>
<filter-class>filters.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MySessionFilter</filter-name>
<url-pattern>*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
这会将每个请求(<url-pattern>*</url-pattern>
)重定向到页面req.getRequestDispatcher("index.jsp");
希望这有帮助