You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by ciAnd7 <ci...@gmail.com> on 2008/03/03 17:58:23 UTC

Request.getContextPath() after RequestDispatcher.forward()

Hi!
I am using tomcat 6.0.13 on linux.
I try to deploy my application in to the root context. I.e. it should be
available as root application on one of  virtual host (http://vhost1/). So,
I add <Context ... path="/" /> into the server.xml (I know, it is not
recommended to do it in server.xml, but it is simple and I just want to do
simple testing right now).
The application works fine. But there is a problem with
request.getContextPath() method.  
My Servlets use RequestDispatcher.forward() to JSP. And then, JSP use
Request.getContextPath() to generate URLs (more precisely, getContextPath()
used by JSTL`s <c:url ..> implementation). 
But getContextPath() returns "/", so urls generated by <c:url> looks like
"//image/blah.gif" - i.e. they have double leading slashes. As result -
browser can`t process urls correctly. As I understand specification,
getContextPath() should returns empty string in such case... (And JSTL`s
implementation  of <c:url> expects such behavior).

If I deploy application under "ordinal" context path, all works correctly
(getContextPath() returns string like "/myapp").
If my Servlet use RD.include() instead of forward(), all works fine:
getContextPath() returns empty string.

Could somebody help me with solution.

Thanks.

-- 
View this message in context: http://www.nabble.com/Request.getContextPath%28%29-after-RequestDispatcher.forward%28%29-tp15808317p15808317.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Request.getContextPath() after RequestDispatcher.forward()

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> On Mon, Mar 3, 2008 at 11:49 AM, ciAnd7 <ci...@gmail.com> wrote:
> 
>  No. I mean ' path="/" '.

The real problem is refusal to do things the recommended way, which is
to name your default webapp ROOT (case sensitive).  If you'd simply done
that, you wouldn't have to mess around trying to emulate Rube Goldberg.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Request.getContextPath() after RequestDispatcher.forward()

Posted by Hassan Schroeder <ha...@gmail.com>.
On Mon, Mar 3, 2008 at 11:49 AM, ciAnd7 <ci...@gmail.com> wrote:

>  No. I mean ' path="/" '.

Then that's the problem -- it's wrong. The path of the default webapp
is the empty string "".  :-)

-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Request.getContextPath() after RequestDispatcher.forward()

Posted by ciAnd7 <ci...@gmail.com>.


Christopher Schultz-2 wrote:
> 
> ciAnd7 wrote:
> | <Context ... path="/" />
> 
> Perhaps you meant:
> 
> <Context path="">
> 
> - -chris
> 
> 
> 

No. I mean ' path="/" '. 
I try empty string putting <Context /> in server.xml, and in
app.war/META-INF/context.xml but without success: ROOT application from
default host directory executed for '/' path on every virtual host. I found
solution with 'path="/" ' and it seems to works except forward() ....
gerContextPath().
-- 
View this message in context: http://www.nabble.com/Request.getContextPath%28%29-after-RequestDispatcher.forward%28%29-tp15808317p15812168.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Getting the URL of a DataSource

Posted by "Katilie, John" <Jo...@softwareagusa.com>.
Phil, Thanks.. I'll take a look.

jfk

-----Original Message-----
From: Phil Steitz [mailto:phil.steitz@gmail.com] 
Sent: Tuesday, March 04, 2008 10:41 PM
To: Tomcat Users List
Subject: Re: Getting the URL of a DataSource

On Mon, Mar 3, 2008 at 2:08 PM, Katilie, John
<Jo...@softwareagusa.com> wrote:
> All, this may be a dumb question but I've exhausted my research and
just
>  wondering what other users out there are doing.
>
>  I would like to get the URL for a DataSource to display for debugging
>  and/or
>  Informational reasons. I know I can get the URL after I get the
>  DataSource and get a connection from the connection metadata. My
problem
>  is what if the
>  Connection fails? It seems without the connection I can not get the
URL
>  for the DataSource.
>
>  This is an application running under Tomcat 6.0.14. I guess I could
get
>  the URL from the JMX DataSource objects created by Tomcat (I can see
>  them with jconsole). But is there another way? Am I missing
something? I
>  hope this question makes sense...
>
I may get flamed for condoning hacking here, but there is a
too-fragile-to-depend-on way to do this for debugging / testing.

If the DataSource implementation that you are using exposes a getUrl
method, and you really want to get this (or any other extended
property) in application code, you can get the URL by specializing the
cast on your JNDI lookup and then invoking that method.  For example,
if you are using the tomcat-provided DBCP DataSource,
org.apache.tomcat.dbcp.dbcp.BasicDataSource ds  =
(org.apache.tomcat.dbcp.dbcp.BasicDataSource)
initContext.lookup("java:/comp/env/jdbc/yourDB");
String urlString = ds.getUrl();
That obiously only works if you are using BasicDataSource bundled with
Tomcat or if whatever datasource you use supports something like this
(If you are using Commons DBCP directly, just change "tomcat.dbcp" to
"commons").  It is not something you want to depend on, though, as all
Tomcat or your DS provider really promises you is a
javax.sql.DataSource.  In particular, there is no guarantee that code
such as the above will not start generating ClassCastExceptions or
ClassNotFound when the container (Tomcat) or datasource provider
release new versions.

Phil


>  Thanks for any and all comments.
>
>  Regards, John.
>
>  ---------------------------------------------------------------------
>  To start a new topic, e-mail: users@tomcat.apache.org
>  To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>  For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Getting the URL of a DataSource

Posted by "Katilie, John" <Jo...@softwareagusa.com>.
Phil, I switched over to using BasicDataSource instead of DataSource and
I'm able to get everything I need. I've tested it against 10 different
Data Bases and everything seems to work wonderfully. 

Thank you for your response and help. I hope some day I can also help
you!

Regards, jfk. 

-----Original Message-----
From: Phil Steitz [mailto:phil.steitz@gmail.com] 
Sent: Tuesday, March 04, 2008 10:41 PM
To: Tomcat Users List
Subject: Re: Getting the URL of a DataSource

On Mon, Mar 3, 2008 at 2:08 PM, Katilie, John
<Jo...@softwareagusa.com> wrote:
> All, this may be a dumb question but I've exhausted my research and
just
>  wondering what other users out there are doing.
>
>  I would like to get the URL for a DataSource to display for debugging
>  and/or
>  Informational reasons. I know I can get the URL after I get the
>  DataSource and get a connection from the connection metadata. My
problem
>  is what if the
>  Connection fails? It seems without the connection I can not get the
URL
>  for the DataSource.
>
>  This is an application running under Tomcat 6.0.14. I guess I could
get
>  the URL from the JMX DataSource objects created by Tomcat (I can see
>  them with jconsole). But is there another way? Am I missing
something? I
>  hope this question makes sense...
>
I may get flamed for condoning hacking here, but there is a
too-fragile-to-depend-on way to do this for debugging / testing.

If the DataSource implementation that you are using exposes a getUrl
method, and you really want to get this (or any other extended
property) in application code, you can get the URL by specializing the
cast on your JNDI lookup and then invoking that method.  For example,
if you are using the tomcat-provided DBCP DataSource,
org.apache.tomcat.dbcp.dbcp.BasicDataSource ds  =
(org.apache.tomcat.dbcp.dbcp.BasicDataSource)
initContext.lookup("java:/comp/env/jdbc/yourDB");
String urlString = ds.getUrl();
That obiously only works if you are using BasicDataSource bundled with
Tomcat or if whatever datasource you use supports something like this
(If you are using Commons DBCP directly, just change "tomcat.dbcp" to
"commons").  It is not something you want to depend on, though, as all
Tomcat or your DS provider really promises you is a
javax.sql.DataSource.  In particular, there is no guarantee that code
such as the above will not start generating ClassCastExceptions or
ClassNotFound when the container (Tomcat) or datasource provider
release new versions.

Phil


>  Thanks for any and all comments.
>
>  Regards, John.
>
>  ---------------------------------------------------------------------
>  To start a new topic, e-mail: users@tomcat.apache.org
>  To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>  For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Getting the URL of a DataSource

Posted by Phil Steitz <ph...@gmail.com>.
On Mon, Mar 3, 2008 at 2:08 PM, Katilie, John
<Jo...@softwareagusa.com> wrote:
> All, this may be a dumb question but I've exhausted my research and just
>  wondering what other users out there are doing.
>
>  I would like to get the URL for a DataSource to display for debugging
>  and/or
>  Informational reasons. I know I can get the URL after I get the
>  DataSource and get a connection from the connection metadata. My problem
>  is what if the
>  Connection fails? It seems without the connection I can not get the URL
>  for the DataSource.
>
>  This is an application running under Tomcat 6.0.14. I guess I could get
>  the URL from the JMX DataSource objects created by Tomcat (I can see
>  them with jconsole). But is there another way? Am I missing something? I
>  hope this question makes sense...
>
I may get flamed for condoning hacking here, but there is a
too-fragile-to-depend-on way to do this for debugging / testing.

If the DataSource implementation that you are using exposes a getUrl
method, and you really want to get this (or any other extended
property) in application code, you can get the URL by specializing the
cast on your JNDI lookup and then invoking that method.  For example,
if you are using the tomcat-provided DBCP DataSource,
org.apache.tomcat.dbcp.dbcp.BasicDataSource ds  =
(org.apache.tomcat.dbcp.dbcp.BasicDataSource)
initContext.lookup("java:/comp/env/jdbc/yourDB");
String urlString = ds.getUrl();
That obiously only works if you are using BasicDataSource bundled with
Tomcat or if whatever datasource you use supports something like this
(If you are using Commons DBCP directly, just change "tomcat.dbcp" to
"commons").  It is not something you want to depend on, though, as all
Tomcat or your DS provider really promises you is a
javax.sql.DataSource.  In particular, there is no guarantee that code
such as the above will not start generating ClassCastExceptions or
ClassNotFound when the container (Tomcat) or datasource provider
release new versions.

Phil


>  Thanks for any and all comments.
>
>  Regards, John.
>
>  ---------------------------------------------------------------------
>  To start a new topic, e-mail: users@tomcat.apache.org
>  To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>  For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Getting the URL of a DataSource

Posted by "Katilie, John" <Jo...@softwareagusa.com>.
All, this may be a dumb question but I've exhausted my research and just
wondering what other users out there are doing.

I would like to get the URL for a DataSource to display for debugging
and/or
Informational reasons. I know I can get the URL after I get the
DataSource and get a connection from the connection metadata. My problem
is what if the
Connection fails? It seems without the connection I can not get the URL
for the DataSource. 

This is an application running under Tomcat 6.0.14. I guess I could get
the URL from the JMX DataSource objects created by Tomcat (I can see
them with jconsole). But is there another way? Am I missing something? I
hope this question makes sense...

Thanks for any and all comments.

Regards, John.  

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Request.getContextPath() after RequestDispatcher.forward()

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

To whom it may concern,

ciAnd7 wrote:
| <Context ... path="/" />

Perhaps you meant:

<Context path="">

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkfML38ACgkQ9CaO5/Lv0PCHoACeI3d+Ch7reozfQtAzsVINH6MT
TTEAn35opET+yf75yVHO/0FfpnURyDFT
=Uouv
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org