views:

414

answers:

3

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 beeb tasked with writing a servlet that intercepts a call to and JSP in a specific directoy, check that the file exists and if it does just forwarding to that file, if if doesn't I'm 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 {
                    // 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.

+1  A: 

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>
Bozho
I only want this to happen if it's in a particular directory and the file name contains a certain string. So a catch all 404 doesn't have enough granularity for me.
Omar Kooheji
Then see BalusC's response.
Bozho
+3  A: 
File file = new File(requestUri);

This is wrong. The java.io.File knows nothing about the web 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);
BalusC
Thanks I'll try this.
Omar Kooheji
That still doesn't stop the issue with it entering an infinite loop.
Omar Kooheji
Add `<dispatcher>REQUEST</dispatcher>` to the `<servlet-mapping>`. BTW: a filter is indeed a better place for this, you can just put the same code in the `doFilter()`, you just have to cast request and response down.
BalusC
The request dispatcher code doesn't seem to work. It still enters the infinite loop and eclipse doesn't seem to like the dispatcher element in the servlet Mapping code. I'm going to try to using a filter.
Omar Kooheji
The error eclipse is giving me in the XML: cvc-complex-type.2.4.d: Invalid content was found starting with element 'dispatcher'. No child element is expected at this point.
Omar Kooheji
Sorry, I was wrong, the `<dispatcher>` indeed doesn't apply on servlets. Anyway, to the point, the solution is just to check if the servlet wasn't already invoked before in the same request. A forward will invoke the same servlet again, hence ending in a recursive loop. By the way, the comment `// Otherwise redirect to default.jsp` contradicts with the actual code. It's doing a forward, not a redirect.
BalusC
I got it working with a filter thanks for your help.
Omar Kooheji
You're welcome.
BalusC
A: 

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.

Andrew B
I'd thought of that but I can't do that, it needs to be in the same directory as the JSPs unfortunately.
Omar Kooheji