【问题标题】:Creating Java filter for image requests rewritings为图像请求重写创建 Java 过滤器
【发布时间】:2011-04-22 13:16:11
【问题描述】:

我创建了一个过滤器,它适用于我的应用程序收到的所有请求,为此我编写了新类ResourceRequestWrapper,它扩展了HttpServletRequestWrapper 来修改请求。

例如,当请求/ui/tx/images/abc.png 时,我已经覆盖了HttpServletRequestWrappergetServletPath()getRequestURL()getRequestURI() 方法,以将传入请求的文本修改为/txeditor/images/abc.png

现在,我通过修改 server.xml 并添加以下内容,在 JBoss/Tomcat 中创建了虚拟目录,

<Context path="/txeditor" docBase="C:\resources\web_resources\txeditor.war" unpackWAR="false">               
</Context>  

我还在C:\resources\web_resources\txeditor.war 中定义了WEB-INF,并创建了如下部署描述符,

<?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 中定义的虚拟目录时,这是不可能的?

谁能有其他想法?

【问题讨论】:

    标签: java servlets servlet-filters


    【解决方案1】:

    这确实行不通。您想改为重定向请求。

    String oldUrl = request.getRequestURI();
    String newUrl = oldUrl.replaceFirst("/ui/tx/", "/txteditor/");
    response.sendRedirect(newUrl);
    

    如果您想要永久 (301) 重定向,请执行以下操作而不是最后一行:

    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", newUrl);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-03
      • 2014-08-21
      • 2011-03-08
      • 2012-08-13
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2021-11-21
      相关资源
      最近更新 更多