The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

tempusfugit (144) [Avatar] Offline
#1
Under 'Note' later in the section it is stated:

"Context paths and servlet paths start with a / but do not end with it."

Then later the servlet mapping

<servlet-mapping>
<servlet-name>BlueServlet</servlet-name>
<url-pattern>/blue/</url-pattern>
</servlet-mapping>

creates a situation were the servlet path does end with a '/', contradicting the statement above for servlet paths.

It would have been helpful to have an additional entry in "Table 5.2 Mapping a request URI to a servlet" that illustrates that the url-pattern "/blue/" is in fact legal, i.e.:

Request URL: /colorapp/blue/
Servlet used: BlueServlet
Servlet path: /blue/
Path Info: null
Comments: See Step 1.

Otherwise the reader may, in order to reconcile all the previously presented information, be inclined to 'guess' that the "/blue/" url-pattern is illegal or that the result would be:

Servlet path: /blue
Path Info: /
mikec (11) [Avatar] Offline
#2
Re: 5.2.4 Mapping a URL to a servlet
> Under 'Note' later in the section it is stated:
>
> "Context paths and servlet paths start with a / but
> do not end with it."
>
> Then later the servlet mapping
>
> <servlet-mapping>
> <servlet-name>BlueServlet</servlet-name>
> <url-pattern>/blue/</url-pattern>
> </servlet-mapping>
>
> creates a situation were the servlet path does end
> with a '/', contradicting the statement above for
> servlet paths.

What's specified is an url-pattern, not a servlet path, and as such, it's legal. Because it doesn't end with /*, it is only eligible for 'exact matches'.

>
> It would have been helpful to have an additional
> entry in "Table 5.2 Mapping a request URI to a
> servlet" that illustrates that the url-pattern
> "/blue/" is in fact legal, i.e.:

There is an explanation of this type of url-pattern, but for the /red and /red/ URIs (the first two rows of table 5.2)

>
> Request URL: /colorapp/blue/
> Servlet used: BlueServlet
> Servlet path: /blue/
wrong, it would be /blue
> Path Info: null
wrong, it would be '/'
> Comments: See Step 1.
>
> Otherwise the reader may, in order to reconcile all
> the previously presented information, be inclined to
> 'guess' that the "/blue/" url-pattern is illegal or
> that the result would be:
>
> Servlet path: /blue
> Path Info: /
This is the correct result. The legal URI /blue/ is an exact match for the legal url-pattern: /blue/
Then when you use request.getServletPath(), the container 'snips' out the section of the URI that starts with the slash after the contextPath (if any) and ends (in this example) just before the next slash. Path Info then contains that last slash.
tempusfugit (144) [Avatar] Offline
#3
Re: 5.2.4 Mapping a URL to a servlet
Well, Tomcat 5.0.28 disagrees with your statement (haven't decoded the spec thus far)

===================================
URL:
http://localhost:8080/fwdinc/blue/

===================================
Output:
BlueServlet
Request URI: /fwdinc/blue/
Context Path: /fwdinc
Servlet Path: /blue/
Path Info: null

===================================
From web.xml:
<servlet>
<servlet-name>BlueServlet</servlet-name>
<servlet-class>BlueServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BlueServlet</servlet-name>
<url-pattern>/blue/</url-pattern>
</servlet-mapping>

===================================
BlueServlet.java:

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

public class BlueServlet extends HttpServlet {

public void doGet(
HttpServletRequest req,
HttpServletResponse res
) throws
ServletException,
IOException
{
PrintWriter pw = res.getWriter();
pw.println( "<html>" );
pw.println( "<head>" );
pw.println( "</head>" );
pw.println( "<body>" );

pw.println( "<h3>" + this.getClass().getName() + "</h3>" );
pw.println( "Request URI: " + req.getRequestURI() + "
");
pw.println( "Context Path: " + req.getContextPath() + "
" );
pw.println( "Servlet Path: " + req.getServletPath() + "
");
pw.println( "Path Info: " + req.getPathInfo() + "
");

pw.println( "</body>" );
pw.println( "</html>" );
}
}

===================================
And as far as Tomcat goes '/blue/' is a ServletPath as that is the value that is returned by req.getServletPath()