开发者

Writing a Servlet that checks to see if JSP's exist and forwards to another JSP if they aren't

UPDATE:

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

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>

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 {开发者_StackOverflow社区
                    // 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);   
  }
 }

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);

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.

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 (file.exists()) { 
    // ...
}

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) { 
    // ...
}


This can be done in a lot easier, and built-in way.

web.xml has <error-page> element. You can do something like:

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


What I would do is to create a dummy directory for the initial servlet request, and then forward to the real directory or to the 404 type page. You're issuing a forward regardless, so why not just redirect to a different directory? That would avoid any looping issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜