I'd like to see a Servlet 2.2 API version of Alex Chaffee's CookieDetector servlet.

Avi Kak

To create a Servlet 2.2 API version of Alex Chaffee's CookieDetector servlet, replace the statement

     String self = req.getScheme() + "://" + req.getServerName() +
         (port == 80 ? "" : ":"+port) + req.getServletPath();

by the following statements:

     String webapp = null;
     String realPath = getServletContext().getRealPath( req.getServletPath() );
     String fileSeparator = System.getProperty( "file.separator" );
     StringTokenizer st = new StringTokenizer( realPath, fileSeparator );
     while ( st.hasMoreTokens() ) {
         if ( st.nextToken().equals( "webapps" ) ) {
             webapp = st.nextToken();
     String self = req.getScheme() + "://" + req.getServerName() +
         (port == 80 ? "" : ":"+port) + "/" + webapp + req.getServletPath();

This change in the code is needed to reflect the directory structure for a typical webapp for Servlet 2.2 and the fact that the URL for accessing a servlet usually includes the name of the webapp. For illustration, I have a webapp named test-suite. The servlets for this webapp are in the directory


and the URL I'd use to access, say, a HelloServlet in this webapp would be


All that the replacement code does is to extract the name of the webapp from the pathname string returned by the getRealPath method.

[Thanks Avi! Now I guess I'll have to go integrate these changes... -Alex]

0 Comments  (click to add your comment)
Comment and Contribute






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



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.