You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by bu...@apache.org on 2008/10/30 14:52:42 UTC

DO NOT REPLY [Bug 46131] New: for a root context getContextPath returns "/" after a forward

https://issues.apache.org/bugzilla/show_bug.cgi?id=46131

           Summary: for a root context getContextPath returns "/" after a
                    forward
           Product: Tomcat 6
           Version: 6.0.18
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Servlet & JSP API
        AssignedTo: dev@tomcat.apache.org
        ReportedBy: denny.strietzbaum@hybris.de


Javadoc for HttpServletRequest#getContextPath() says:
For servlets in the default (root) context, this method returns ""

As long as i do no forward Tomcat does it right.
However, when forwarding via a Filter to another resource the result of
getContextPath() changes from "" to "/".

Small demo code:

public class TestFilter implements Filter {
 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain
chain) {
  String path = ((HttpServletRequest)req).getContextPath();
  System.out.println("Filter context path:" + path);
  //prints ""

  RequestDispatcher disp = req.getRequestDispatcher("/testservlet");
  disp.forward(request, response);
  }
  ...
}

public class TestServlet extends HttpServlet {
 public void service(ServletRequest req, ServletResponse res) {
  String path = ((HttpServletRequest)req).getContextPath();
  System.out.println("Servlet context path:" + path);
  //prints "" but when coming from filter prints "/"
 }
}

Small issue but i've had big problems when using JSF (myfaces) and
UrlRewriteFilter for a root context application.
As long as no rewriterule matches, JSF generates URLs like "/path/to/file.jsf"
which the browser interprets as "http://www.myhost.com/path/to/file.jsf".
But when a rewriterule matches (and a  forward is processed)
JSF generates URLs like "//path/to/file.jsf" which the browser interprets as
"http://path/to/file.jsf"

My workaround:
A HttpServletRequestWrapper which overrides getContextPath()
public String getContextPath() {
 return "/".equals(super.getContextPath()) ? "" : super.getContextPath();
}


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


DO NOT REPLY [Bug 46131] for a root context getContextPath returns "/" after a forward

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=46131





--- Comment #5 from Denny Strietzbaum <de...@hybris.de>  2008-11-05 01:41:00 PST ---
(In reply to comment #4)
> A path setting of "/" is invalid

I know that as i already said this setting is not correct (but it's clear what
is meant).
I don't want discussions about specifications.
Specifications must be kept and when they aren't an error or warning should be
thrown.
It's easy to detected "/" as an invalid value and it's even easy to correct
this value automatically.

My motivation simply is that i wasn't responsible for that setting as our
server.xml gets generated by a build process which i haven't written.
I wouldn't have had lost so much time when Tomcat would let me know that
there's a problem with it's configuration.

On the other side that incorrect setting works (as getContextPath returns "")
as long is i don't do any forwards.
You can't say that this is a consistently behavior.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


DO NOT REPLY [Bug 46131] for a root context getContextPath returns "/" after a forward

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=46131


Mark Thomas <ma...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME




--- Comment #1 from Mark Thomas <ma...@apache.org>  2008-11-01 03:33:25 PST ---
This works for me with a simple filter and servlet. I suspect something else in
your setup is responsible for the incorrect return value. 


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


DO NOT REPLY [Bug 46131] for a root context getContextPath returns "/" after a forward

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=46131


Denny Strietzbaum <de...@hybris.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




--- Comment #3 from Denny Strietzbaum <de...@hybris.de>  2008-11-04 04:12:10 PST ---
Reopened because i've found out more.
The problem only occurs when declare the context within server.xml and
setting the path to "/".

This (correct) context declaration works as expected:
<Context path="" docBase="D:/path/to/docbase" />

Whereas this (not so correct) one produces the decribed issue:
<Context path="/" docBase="D:/path/to/docbase" />

I know it's not a good thing to use the server.xml but unluckily 
i have no influence on that.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


DO NOT REPLY [Bug 46131] for a root context getContextPath returns "/" after a forward

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=46131


Mark Thomas <ma...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |INVALID




--- Comment #4 from Mark Thomas <ma...@apache.org>  2008-11-04 06:50:55 PST ---
A path setting of "/" is invalid.


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


DO NOT REPLY [Bug 46131] for a root context getContextPath returns "/" after a forward

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=46131


Denny Strietzbaum <de...@hybris.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|WORKSFORME                  |INVALID




--- Comment #2 from Denny Strietzbaum <de...@hybris.de>  2008-11-01 14:30:29 PST ---
You're absolutely right.
I've tried it again at a my home machine and the problem was gone.
I still have this problem at work with a preconfigured tomcat installation.
Anyway, i'll change this bug to "invalid".


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org