为图像请求重写创建 Java 过滤器 [英] Creating Java filter for image requests rewritings
问题描述
我创建了一个过滤器,它可以很好地处理我的应用程序收到的所有请求,为此我编写了新类 ResourceRequestWrapper
,它扩展了 HttpServletRequestWrapper
以修改请求.
I have created one filter which is working fine for all requests which my application gets, for this I have written new class ResourceRequestWrapper
which extends HttpServletRequestWrapper
to modify requests.
例如,当请求 /ui/tx/images/abc.png
时,我覆盖了 getServletPath()
, getRequestURL()
HttpServletRequestWrapper
的 > 和 getRequestURI()
方法将传入请求的文本修改为 /txeditor/images/abc.png
.
For example, when request comes for /ui/tx/images/abc.png
I have overridden getServletPath()
, getRequestURL()
and getRequestURI()
methods of HttpServletRequestWrapper
to modify text of incoming request to /txeditor/images/abc.png
.
现在,我通过修改 server.xml
并添加以下内容,在 JBoss/Tomcat 中创建了虚拟目录,
Now, I have created virtual directory in JBoss/Tomcat by modifying server.xml
and adding following,
<Context path="/txeditor" docBase="C:\resources\web_resources\txeditor.war" unpackWAR="false">
</Context>
我还在 C:\resources\web_resources\txeditor.war
中定义了 WEB-INF
并创建了如下部署描述符,
I have also defined WEB-INF
in C:\resources\web_resources\txeditor.war
and created deployment descriptor as follows,
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>Web descriptor for the html adaptor</description>
</web-app>
以上一切正常,每个请求都被修改,但是当我的页面打开时,我看不到任何图像,我想当我们修改来自过滤器的请求以访问 server.xml<中定义的虚拟目录时/code>,不可能吗?
Everything above works fine, every request gets modified, but when my page gets opened I am not able to see any images and I think when we are modifying request from filter to access virtual directory defined in server.xml
, its not possible?
有人能有其他想法吗?
推荐答案
这确实行不通.您想改为重定向请求.
This is indeed not going to work. You want to redirect the request instead.
String oldUrl = request.getRequestURI();
String newUrl = oldUrl.replaceFirst("/ui/tx/", "/txteditor/");
response.sendRedirect(newUrl);
如果您想要永久 (301) 重定向,请执行以下操作而不是最后一行:
If you want a permanent (301) redirect, do the following instead of the last line:
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", newUrl);
这篇关于为图像请求重写创建 Java 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!