title: How come request.getPathInfo() in service method returns null?
body: <p>I wrote Front Controller Pattern and ran the test. Somehow request.getPathInfo() is returning null when it should return the path info.</p>
  
  <p><strong>1. HTML that calls servlet</strong></p>
  
  <pre><code>&lt;a href="tmp.do"&gt;Test link to invoke cool servlet&lt;/a&gt;
  </code></pre>
  
  <p><strong>2. Map the servlet in DD.</strong><br />
  Anything that has .do extension (ex tmp.do) will invoke the servlet "Redirector"</p>
  
  <pre><code>&lt;!-- SERVLET (centralized entry point) --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;RedirectHandler&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.masatosan.redirector.Redirector&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;RedirectHandler&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p><strong>3. The servlet that takes request from *.do</strong></p>
  
  <pre><code> public class Redirector extends HttpServlet {
  
          protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              try {
                  //test - THIS RETURNS NULL!!!!
                  System.out.println(request.getPathInfo());
  
                  Action action = ActionFactory.getAction(request); //return action object based on request URL path
                  String view = action.execute(request, response); //action returns String (filename) 
                  if(view.equals(request.getPathInfo().substring(1))) {
                      request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
                  }
                  else {
                      response.sendRedirect(view);
                  }
              }
              catch(Exception e) {
                  throw new ServletException("Failed in service layer (ActionFactory)", e);
              }
          }
      }//end class
  </code></pre>
  
  <p>The problem is that request.getPathInfo() returns null. Based on the Head First book, </p>
  
  <blockquote>
    <p>The servlet life cycle moves from
    <code>"does not exist"</code> state to
    <code>"initialized"</code> state (meaning ready
    to service client's request) beginning
    with its constructor. The init()
    always completes before the first call
    to service().</p>
  </blockquote>
  
  <p>This tells me that somewhere between the constructor and init() method, the servlet is not-fully-grown servlet.</p>
  
  <p>So, that means, by the time service() method is called, the servlet should be fully-grown servlet and request method should be able to call getPathInfo() and expect the valid value to return instead of null.</p>
  
  <h1>UDPATE</h1>
  
  <p>Very interesting. (<a href="http://forums.sun.com/thread.jspa?threadID=657991" rel="nofollow noreferrer">http://forums.sun.com/thread.jspa?threadID=657991</a>)</p>
  
  <p><strong>(HttpServletRequest - getPathInfo() )</strong></p>
  
  <p>If the URL is like below:</p>
  
  <p><a href="http://www.myserver.com/mycontext/myservlet/hello/test?paramName=value" rel="nofollow noreferrer">http://www.myserver.com/mycontext/myservlet/hello/test?paramName=value</a>.</p>
  
  <p>If you web.xml discribe the servlet pattern as /mycontext/* getPathInfo() will return myservlet/hello/test and getQueryString() will return paramName=value </p>
  
  <p><strong>(HttpServletRequest - getServletPath() )</strong></p>
  
  <p>If the URL is like below:</p>
  
  <p><a href="http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789" rel="nofollow noreferrer">http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789</a> </p>
  
  <p>String servletPath = req.getServletPath();</p>
  
  <p>It returns "/servlet/MyServlet"</p>