java 在 Tomcat 6.0 中重定向子目录的所有请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1121858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect all requests for a subdirectory in Tomcat 6.0
提问by D. Wroblewski
I'm running a local Tomcat 6.0 server on my desktop.
我在我的桌面上运行本地 Tomcat 6.0 服务器。
I'm trying to redirect any and all requests matching http://localhost:8080/RedirectDirectory/abc/efg/morejunkto a single JSP page.
我正在尝试将匹配http://localhost:8080/RedirectDirectory/abc/efg/morejunk 的所有请求重定向到单个 JSP 页面。
In my RedirectDirectory project's web.xml I have
在我的 RedirectDirectory 项目的 web.xml 我有
<servlet>
<servlet-name>IOPRedirect</servlet-name>
<jsp-file>/RedirectDirectory/filetree.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>IOPRedirect</servlet-name>
<url-pattern>/RedirectDirectory/*</url-pattern>
</servlet-mapping>
I would really like it to go to that JSP whether the directory exists or not.
无论目录是否存在,我都非常希望它转到该 JSP。
I thought this is how to do it, but I guess not.
我认为这是如何做到的,但我想不是。
Any ideas?
有任何想法吗?
Thanks
谢谢
回答by D. Wroblewski
I usually use the UrlRewriteFilter when solving problems like this.
在解决此类问题时,我通常使用 UrlRewriteFilter。
- Downloadand add the urlrewrite.jar to your classpath (WEB-INF/lib)
- Add the following to your WEB-INF/web.xml:
- 下载urlrewrite.jar 并将其添加到您的类路径 (WEB-INF/lib)
- 将以下内容添加到您的 WEB-INF/web.xml:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- Edit WEB-INF/urlrewrite.xml and add the following to it:
- 编辑 WEB-INF/urlrewrite.xml 并将以下内容添加到其中:
<rule>
<from>^/RedirectDirectory/(.*)$</from>
<to>/RedirectDirectory/filetree.jsp</to>
</rule>
Having UrlRewriteFilter in your project is very handy for solving a lot of common problems like setting cache headers, canonical hostnames, forcing https on certain urls etc.
在您的项目中使用 UrlRewriteFilter 可以非常方便地解决许多常见问题,例如设置缓存头、规范主机名、在某些 url 上强制使用 https 等。
回答by dborba
What you did works fine for servlets - haven't tried doing with JSPs.
您所做的对于 servlet 工作正常 - 还没有尝试过使用 JSP。
Edit: After trying more less exactly what you did, I found that it works fine. The exception was when forwarding to a specific jsp which was using a security constraint, which caused an error. The error was due to the fact the redirection got around the user login, and therefore required data was omitted in the request.
编辑:在尝试了更多不准确的操作后,我发现它运行良好。例外是转发到使用安全约束的特定 jsp 时,这会导致错误。该错误是由于重定向绕过了用户登录,因此请求中省略了所需的数据。
A round about way of doing it would be to forward all requests to a servlet & have the servlet forward to your desired jsp.
一种方法是将所有请求转发到 servlet 并将 servlet 转发到您想要的 jsp。
Your web.xml would then be:
您的 web.xml 将是:
<servlet>
<servlet-name>IOPRedirect</servlet-name>
<servlet-class>IOPRedirect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>IOPRedirect</servlet-name>
<url-pattern>/RedirectDirectory/*</url-pattern>
</servlet-mapping>
And you'd have to create a IOPredirect servlet with the following inside your doGet() method:
并且您必须在 doGet() 方法中使用以下内容创建一个 IOPredirect servlet:
String url="/RedirectDirectory/filetree.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);

