2010-03-24 65 views
6

UPDATE:写检查,看看是否JSP的存在,并转发到另一个JSP,如果他们是一个Servlet不

要澄清一个一般性错误捕手,捕捉404的没有足够的粒度对我来说。只有当jsp位于特定的目录中,并且仅当文件名包含某个字符串时,我才需要执行此操作。

/UPDATE

我一直在负责编写拦截在一个特定的目录和JSP调用一个servlet,检查文件是否存在,如果它不只是转发该文件,如果它那么我不会转发到默认的JSP。 我设置了web.xml如下:

<servlet> 
<description>This is the description of my J2EE component</description> 
<display-name>This is the display name of my J2EE component</display-name> 
<servlet-name>CustomJSPListener</servlet-name> 
<servlet-class> ... CustomJSPListener</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
... 
<servlet-mapping> 
    <servlet-name>CustomJSPListener</servlet-name> 
    <url-pattern>/custom/*</url-pattern> 
</servlet-mapping> 

并且servlet的doGet方法如下:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    logger.debug(String.format("Intercepted a request for an item in the custom directory [%s]",request.getRequestURL().toString())); 
    String requestUri = request.getRequestURI(); 
      // Check that the file name contains a text string 
    if (requestUri.toLowerCase(Locale.UK).contains("someText")){ 
    logger.debug(String.format("We are interested in this file [%s]",requestUri)); 
    File file = new File(requestUri); 
    boolean fileExists = file.exists(); 
    logger.debug(String.format("Checking to see if file [%s] exists [%s].",requestUri,fileExists)); 
        // if the file exists just forward it to the file 
    if (fileExists){ 
    getServletConfig().getServletContext().getRequestDispatcher(
      requestUri).forward(request,response); 
    } else { 
        // Otherwise redirect to default.jsp 
    getServletConfig().getServletContext().getRequestDispatcher(
      "/custom/default.jsp").forward(request,response); 
    } 
    } else { 
        // We aren't responsible for checking this file exists just pass it on to the requeseted jsp 
    getServletConfig().getServletContext().getRequestDispatcher(
      requestUri).forward(request,response); 
    } 
} 

这似乎从Tomcat导致错误500,我想这是因为servlet重定向到同一个文件夹,然后被servlet再次拦截,导致无限循环。 有没有更好的方法来做到这一点?我导致我相信我可以使用过滤器来做到这一点,但我对它们不甚了解。

回答

8
File file = new File(requestUri); 

这是不对的。 java.io.File知道什么都没有关于它正在运行的webapp上下文。文件路径将相对于当前工作目录,这取决于你如何启动应用服务器。例如,它可能与C:/Tomcat/bin相关,而不是您所期望的webapp根目录。你不想拥有这个。

使用ServletContext#getRealPath()将相对网络路径转换为绝对磁盘文件系统路径。 ServletContext在继承的getServletContext()方法的servlet中可用。因此,下面要指出正确的文件:

String absoluteFilePath = getServletContext().getRealPath(requestUri); 
File file = new File(absoluteFilePath); 

if (new File(absoluteFilePath).exists()) { 
    // ... 
} 

或者,如果目标容器不扩大物理磁盘文件系统的战争,但在替代内存,那么你最好使用ServletContext#getResource()

URL url = getServletContext().getResource(requestUri); 

if (url != null) { 
    // ... 
} 
+0

谢谢,我会试试这个。 – 2010-03-24 11:40:24

+0

这仍然无法阻止它进入无限循环的问题。 – 2010-03-24 11:45:35

+1

将' REQUEST'添加到''。顺便说一句:过滤器确实是一个更好的地方,你可以在'doFilter()'中放入相同的代码,你只需要向下施放请求和响应。 – BalusC 2010-03-24 12:17:02

3

这可以在更容易和内置的方式完成。

web.xml有<error-page>元素。你可以这样做:

<error-page> 
    <error-code>404</error-code> 
    <location>/pageNotFound.jsp</location> 
<error-page> 
+0

我只希望发生这种情况,如果它在一个特定的目录中,并且文件名包含某个字符串。所以抓住所有的404对我来说没有足够的粒度。 – 2010-03-24 11:06:08

+0

然后看到BalusC的回应。 – Bozho 2010-03-24 11:29:56

0

我会做的是为初始servlet请求创建一个虚拟目录,然后转发到真实目录或404类型页面。无论你在发布一个前锋,为什么不直接重定向到不同的目录呢?这将避免任何循环问题。

+0

我想到了这一点,但我无法做到这一点,不幸的是它需要与JSP相同的目录。 – 2010-03-24 12:07:40

相关问题