编写一个Servlet来检查JSP是否存在,如果不存在则转发给另一个JSP [英] Writing a Servlet that checks to see if JSP's exist and forwards to another JSP if they aren't

查看:118
本文介绍了编写一个Servlet来检查JSP是否存在,如果不存在则转发给另一个JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

澄清捕获404的一般错误捕获器对我来说没有足够的粒度。我只有在jsp位于特定目录中时才需要这样做,只有当文件名包含某个字符串时才需要这样做。

To clarify a generic error catcher that catches 404's doesn't have enough granularity for me. I need to do this only if the jsp is in a particular directory and then only if the filename contains a certain string.

/ UPDATE

我的任务是编写一个servlet,它拦截对特定目录中的JSP和JSP的调用,检查文件是否存在以及它是否仅转发到该文件,如果没有,那么我将转发到默认的JSP。
我按如下方式设置了web.xml:

I've been tasked with writing a servlet that intercepts a call to and JSP in a specific directory, check that the file exists and if it does just forwarding to that file, if it doesn't then I'm going to forward to a default JSP. I've setup the web.xml as follows:

<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方法如下:

And the doGet method of the servlet is as follows:

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再次拦截,导致无限循环。
有更好的方法吗?我认为我可以使用过滤器来做到这一点,但我对它们的了解不多。

This seems to result in an error 500 from tomcat, I think this is because the servlet is redirecting to the same folder which is then being intercepted again by the servlet, resulting in an infinite loop. Is there a better way to do this? I'm lead to believe that I could use filters to do this, but I don't know very much about them.

推荐答案


File file = new File(requestUri);


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

This is wrong. The java.io.File knows nothing about the webapp context it is running in. The file path will be relative to the current working directory, which is dependent on the way how you start the appserver. It may for example be relative to C:/Tomcat/bin rather than the webapp root as you seem to expect. You don't want to have this.

使用 ServletContext#getRealPath() 翻译相对网页绝对磁盘文件系统路径的路径。 servlet中的 ServletContext 由继承的 getServletContext() 方法。因此,下面应该指出正确的文件:

Use ServletContext#getRealPath() to translate a relative web path to an absolute disk file system path. The ServletContext is available in the servlet by the inherited getServletContext() method. Thus, following should point out the right file:

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

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

或者,如果目标容器没有在物理磁盘文件系统上扩展WAR,而是在内存中扩展,那么你最好使用 ServletContext #getResource()

Or, if the target container doesn't expand the WAR on physical disk file system but instead in memory, then you'd better use ServletContext#getResource():

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

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

这篇关于编写一个Servlet来检查JSP是否存在,如果不存在则转发给另一个JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆