Is it possible to use the method ServletContext.getRequestDispatcher() to ascertain the presence or the absence of a server resource?

Avi Kak

Some examples of servlets in the community literature seem to suggest that ServletContext.getRequestDispatcher() could be used to determine the presence or the absence of a server resource. But the truth of the matter is that it cannot.

In what follows, I will first mention an example that is posted at a tutorial at the Java Developer Connection that could potentially create this wrong impression about the return value of ServletContext.getRequestDispatcher(). I'll then show a simple servlet that someone just beginning with servlets could find useful for testing this method.

The otherwise very instructive servlet code for the Duke's BooksStore example at Java Developer Connection has the following code fragment for BookStoreServlet.java:


  RequestDispatcher dispatcher =
    getServletContext().getRequestDispatcher( ....some server resource ...);

  if (dispatcher == null) {
    System.out.println("There was no dispatcher");
    // No dispatcher means the html file could not be found.
    response.sendError(response.SC_NO_CONTENT);
  } 
  else {
    System.out.println("There is a dispatcher");
    HttpSession session = request.getSession();  
    dispatcher.forward(request, response);
  }

The manner in which the value of dispatcher is tested and the comment that follows definitely create the impression that the return value of ServletContext.getRequestDispatcher() could be used to determine whether the server resource supplied as the argument to the method is actually present at its specified location.

You can use the following servlet to verify that the return value of ServletContext.getRequestDispatcher() is non-null regardless of the presence or the absence of the html file that is mentioned as the argument to the method. In either case, the message "There is a dispatcher" will be printed out in the window in which the servlet engine is running.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class CheckDispatcherServlet extends HttpServlet 
{
  public void service (HttpServletRequest request,
                       HttpServletResponse response)
            throws ServletException, IOException
  {
    RequestDispatcher dispatcher =
            getServletContext().getRequestDispatcher(
                "/servlets/nonExistentFile.html");     // for Tomcat3.1
//                "/servlets/welcome.html");               // for Tomcat3.1
//                "/WEB-INF/servlets/nonExistentFile.html");  // for jsdk2.1

    System.out.println( "" + dispatcher );

    if (dispatcher == null) {
      System.out.println("There was no dispatcher");
      response.sendError(response.SC_NO_CONTENT);
    } 
    else {
      System.out.println("There is a dispatcher");
      dispatcher.forward(request, response);
    }
  }
}

The lines that are commented out are for verifying that the servlet does indeed work for a file that is actually present at the specified location. If you are just starting out with servlets, the following additional comments might be helpful. (These comments apply specifically to a Windows NT environment.)
  • For Tomcat3.1, I have placed this servlet in the following directory
    
          C:	omcatwebappsavi-servletsWEB-INFclasses
    
    
    where avi-servlets is a directory that I created for some servlets.
    
    
  • For jsdk2.1, I put this servlet in
    
          C:jsdk2.1avi-servletsWEB-INFservlets
    
    
  • Since Tomcat3.1 is not allowed to serve any files under WEB-INF, the "welcome.html" resource file in the commented out line in the code sits in the directory
    
          C:	omcatwebappsavi-servletsservlets
    
    
    whereas for jsdk2.1, it can sit in the same directory as the servlet.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact