You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Alec Swan <au...@yahoo.com> on 2006/07/03 06:20:20 UTC

Serving CSS files from a certain URL

Hi,

I have a problem I've been struggling with for the last couple of hours.

Basically, I need to configure Tomcat (preferably using web.xml) to server requests for CSS pages from a certain context path. For example, I want Tomcat to serve the request for /css/styles.css from /site/css/styles.css.

I have a servlet, which I deploy as a WAR file. I want this servlet to service all .jsp requests, but have Tomcat service .css files from /site/css/ directory inside the servlet WAR file. I don't want Tomcat to invoke my servlet when serving CSS files.

I found a similar thread on the Resin forum: http://www.caucho.com/support/resin-interest/0212/0295.html

Is this possible in Tomcat?

Thanks.

Re: Serving CSS files from a certain URL

Posted by Alec Swan <au...@yahoo.com>.
Beautiful! I'll give it a try!
Thanks.

Hassan Schroeder <ha...@gmail.com> wrote: On 7/5/06, Alec Swan  wrote:

> I like your idea of using filters to rewrite CSS paths. Could you give
> me a hint on how to do that best?

Actually, if you're in a hurry, you could just use the URL Rewrite Filter
  -- see  -- an incredibly handy tool for lots
of other things as well  :-)

Writing your own would be pretty simple, though -- grab any request
that ends in '.css' , substring off the unwanted parts and forward to
the "correct" URL. Or if you only have a small number of style sheets,
you could put the original and "fixed" URLs in a HashMap and save
the string processing.  TMTOWTDI. :-)

HTH,
-- 
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: Serving CSS files from a certain URL

Posted by Hassan Schroeder <ha...@gmail.com>.
On 7/5/06, Alec Swan <au...@yahoo.com> wrote:

> I like your idea of using filters to rewrite CSS paths. Could you give
> me a hint on how to do that best?

Actually, if you're in a hurry, you could just use the URL Rewrite Filter
  -- see <http://tuckey.org/urlrewrite/> -- an incredibly handy tool for lots
of other things as well  :-)

Writing your own would be pretty simple, though -- grab any request
that ends in '.css' , substring off the unwanted parts and forward to
the "correct" URL. Or if you only have a small number of style sheets,
you could put the original and "fixed" URLs in a HashMap and save
the string processing.  TMTOWTDI. :-)

HTH,
-- 
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: Serving CSS files from a certain URL

Posted by Alec Swan <au...@yahoo.com>.
You are right, the problem is with the way JSPs are deployed. I do use the first servlet-mapping pattern, which does NOT match the full path, but rather the context path /testapp.

I like your idea of using filters to rewrite CSS paths. Could you give me a hint on how to do that best?

Thanks.

Hassan Schroeder <ha...@gmail.com> wrote: On 7/5/06, Alec Swan  wrote:
> In any case I don't understand why this works in your case.

Now that I have more coffee in me, me neither :-)  It really shouldn't, and
I'm at another location now, so I can't revisit that, but...

The problem is that "relative" to the container depends on how your JSP
is being invoked; consider:

 
  Test
  /WEB-INF/jsp/Test.jsp
 
 
  Test
  /test
 
 
  Test
  /foo/bar/test
  

The second mapping will work with your ../../css/Styles.css link, the
first will not.

> BTW, why do you think it's a bad idea to keep web designers focused on the look and feel of the site and releaving them from the deployment details?

OT to the list -- but I think a development team should work within a
common (consistent) environment. Otherwise you wind up jumping
through the kind of hoops we're discussing now. :-)

In any case, if I *had* to solve this problem, I'd use the relative links
that work for your 'static' situation, and then either

1) serve all *.css files with a servlet that "fixes" the paths or
2) rewrite the css file paths with a Filter

HTH,
-- 
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: Serving CSS files from a certain URL

Posted by Alec Swan <au...@yahoo.com>.
I have a different perspective on using consistent environments across development and design teams. I think it will be unreasonable to expect designers to change a CSS file, update the WAR file and redeploy it on the local Tomcat instane just to see how CSS changes affected the look of the site. Instead designers should be able to change CSS file and just preview the JSP in the browser to see how the look of the JSP was affected.

Hassan Schroeder <ha...@gmail.com> wrote: On 7/5/06, Alec Swan  wrote:
> In any case I don't understand why this works in your case.

Now that I have more coffee in me, me neither :-)  It really shouldn't, and
I'm at another location now, so I can't revisit that, but...

The problem is that "relative" to the container depends on how your JSP
is being invoked; consider:

 
  Test
  /WEB-INF/jsp/Test.jsp
 
 
  Test
  /test
 
 
  Test
  /foo/bar/test
  

The second mapping will work with your ../../css/Styles.css link, the
first will not.

> BTW, why do you think it's a bad idea to keep web designers focused on the look and feel of the site and releaving them from the deployment details?

OT to the list -- but I think a development team should work within a
common (consistent) environment. Otherwise you wind up jumping
through the kind of hoops we're discussing now. :-)

In any case, if I *had* to solve this problem, I'd use the relative links
that work for your 'static' situation, and then either

1) serve all *.css files with a servlet that "fixes" the paths or
2) rewrite the css file paths with a Filter

HTH,
-- 
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: Serving CSS files from a certain URL

Posted by Hassan Schroeder <ha...@gmail.com>.
On 7/5/06, Alec Swan <au...@yahoo.com> wrote:
> In any case I don't understand why this works in your case.

Now that I have more coffee in me, me neither :-)  It really shouldn't, and
I'm at another location now, so I can't revisit that, but...

The problem is that "relative" to the container depends on how your JSP
is being invoked; consider:

	<servlet>
		<servlet-name>Test</servlet-name>
		<jsp-file>/WEB-INF/jsp/Test.jsp</jsp-file>
	</servlet>
	<servlet-mapping>
		<servlet-name>Test</servlet-name>
		<url-pattern>/test</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>Test</servlet-name>
		<url-pattern>/foo/bar/test</url-pattern>
	</servlet-mapping>	

The second mapping will work with your ../../css/Styles.css link, the
first will not.

> BTW, why do you think it's a bad idea to keep web designers focused on the look and feel of the site and releaving them from the deployment details?

OT to the list -- but I think a development team should work within a
common (consistent) environment. Otherwise you wind up jumping
through the kind of hoops we're discussing now. :-)

In any case, if I *had* to solve this problem, I'd use the relative links
that work for your 'static' situation, and then either

1) serve all *.css files with a servlet that "fixes" the paths or
2) rewrite the css file paths with a Filter

HTH,
-- 
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: Serving CSS files from a certain URL

Posted by Alec Swan <au...@yahoo.com>.
I don't have the logs in front of me right now, but I will send them later today. In any case I don't understand why this works in your case. What URL do you use to access your Test.jsp? I would assume it's http://localhost:8080/testapp/jsp/Test.jsp. Therefore, if this jsp page references ../../css/Styles.css, then Tomcat will try to pick it up from http://localhost:8080/css/Styles.css and not from http://localhost:8080/testapp/css/Styles.css, which is where it is!

BTW, why do you think it's a bad idea to keep web designers focused on the look and feel of the site and releaving them from the deployment details?

Thanks.

Hassan Schroeder <ha...@gmail.com> wrote: On 7/5/06, Alec Swan  wrote:
> Web designers don't run web servers or servlet containers on their machines. (I call their environment "static").

I understand -- and I think that's a bad idea. But whatever.

> So, in my running example Test.jsp would have the following relative link ../../css/Styles.css in order for it to work in the "static" environment.

Exactly -- which is what I tried, and which works just fine for me.

> However, this relative link doesn't work when I deploy Test.jsp in Tomcat.

So, what's different? What path do your logs show as being requested?

-- 
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: Serving CSS files from a certain URL

Posted by Hassan Schroeder <ha...@gmail.com>.
On 7/5/06, Alec Swan <au...@yahoo.com> wrote:
> Web designers don't run web servers or servlet containers on their machines. (I call their environment "static").

I understand -- and I think that's a bad idea. But whatever.

> So, in my running example Test.jsp would have the following relative link ../../css/Styles.css in order for it to work in the "static" environment.

Exactly -- which is what I tried, and which works just fine for me.

> However, this relative link doesn't work when I deploy Test.jsp in Tomcat.

So, what's different? What path do your logs show as being requested?

-- 
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: Serving CSS files from a certain URL

Posted by Alec Swan <au...@yahoo.com>.
David, the problem is that there is no servlet container or web server running on the local machine. I need the JSP page to load the CSS page correctly whether it's deployed on tomcat or not.

David Smith <dn...@cornell.edu> wrote: In your Test.jsp, why don't you have something similar to:


location="${pageContext.request.contextPath}/css/Styles.css" />

I'm of course assuming tomcat 5.x.x and the web.xml file is declared for 
servlet spec 2.4.  This should work whether the running tomcat instance 
is on your local machine or the production server.

--David

Alec Swan wrote:

>Web designers don't run web servers or servlet containers on their machines. (I call their environment "static"). They just create web pages using Dreamweaver and preview them in different browsers. So, in my running example Test.jsp would have the following relative link ../../css/Styles.css in order for it to work in the "static" environment.
>
>However, this relative link doesn't work when I deploy Test.jsp in Tomcat. Suppose I deploy my web-app under /testapp context. In this case, Test.jsp would have to link to the css file through one of the following links:
>../testapp/css/Styles.css or
>/css/Styles.css
>Neither of which work in the "static" environment used by web designers.
>
>I hope this elucidates the problem a little further. So, the question persists, what is the right way to get links to CSS files to work in static as well as deployed environment?
>
>Thanks.
>
>Hassan Schroeder  wrote: On 7/4/06, Alec Swan  wrote:
>
>  
>
>>I need to reference Styles.css from Test.jsp in such a way that it works in static as well as in deployed mode. Note that for this to work in the static mode (no web servers or servlet containers) all paths should be relative. Therefore, when I deploy Test.jsp I want Tomcat either to handle relative paths correctly or ignore the path altogether and just serve all CSS file from a certain location (directory or URL).
>>
>>The goal of all this is to maintain only one source tree used by web designers and Tomcat administrator and avoid changing paths in JSP files during deployment.
>>
>>I would appreciate any feedback on this.
>>    
>>
>
>OK, since you asked :-)  -- this seems a really strange requirement;
>are your "web designers" unable to run a Tomcat instance to work
>in?
>
>But in any case, using relative links to a css file works fine on a test
>page I just did that mirrors your described files. So *exactly* how is
>this not working for you?
>
>  
>


---------------------------------------------------------------------
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: Serving CSS files from a certain URL

Posted by David Smith <dn...@cornell.edu>.
In your Test.jsp, why don't you have something similar to:

<stylesheet type="text/css" 
location="${pageContext.request.contextPath}/css/Styles.css" />

I'm of course assuming tomcat 5.x.x and the web.xml file is declared for 
servlet spec 2.4.  This should work whether the running tomcat instance 
is on your local machine or the production server.

--David

Alec Swan wrote:

>Web designers don't run web servers or servlet containers on their machines. (I call their environment "static"). They just create web pages using Dreamweaver and preview them in different browsers. So, in my running example Test.jsp would have the following relative link ../../css/Styles.css in order for it to work in the "static" environment.
>
>However, this relative link doesn't work when I deploy Test.jsp in Tomcat. Suppose I deploy my web-app under /testapp context. In this case, Test.jsp would have to link to the css file through one of the following links:
>../testapp/css/Styles.css or
>/css/Styles.css
>Neither of which work in the "static" environment used by web designers.
>
>I hope this elucidates the problem a little further. So, the question persists, what is the right way to get links to CSS files to work in static as well as deployed environment?
>
>Thanks.
>
>Hassan Schroeder <ha...@gmail.com> wrote: On 7/4/06, Alec Swan  wrote:
>
>  
>
>>I need to reference Styles.css from Test.jsp in such a way that it works in static as well as in deployed mode. Note that for this to work in the static mode (no web servers or servlet containers) all paths should be relative. Therefore, when I deploy Test.jsp I want Tomcat either to handle relative paths correctly or ignore the path altogether and just serve all CSS file from a certain location (directory or URL).
>>
>>The goal of all this is to maintain only one source tree used by web designers and Tomcat administrator and avoid changing paths in JSP files during deployment.
>>
>>I would appreciate any feedback on this.
>>    
>>
>
>OK, since you asked :-)  -- this seems a really strange requirement;
>are your "web designers" unable to run a Tomcat instance to work
>in?
>
>But in any case, using relative links to a css file works fine on a test
>page I just did that mirrors your described files. So *exactly* how is
>this not working for you?
>
>  
>


---------------------------------------------------------------------
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: Serving CSS files from a certain URL

Posted by Pid <p...@pidster.com>.
Where do your logs say that it is looking for the CSS files?
There must be a record somewhere of the hit for each file, maybe that
can tell you what's wrong.

Post the log entries?


Alec Swan wrote:
> Web designers don't run web servers or servlet containers on their machines. (I call their environment "static"). They just create web pages using Dreamweaver and preview them in different browsers. So, in my running example Test.jsp would have the following relative link ../../css/Styles.css in order for it to work in the "static" environment.
> 
> However, this relative link doesn't work when I deploy Test.jsp in Tomcat. Suppose I deploy my web-app under /testapp context. In this case, Test.jsp would have to link to the css file through one of the following links:
> ../testapp/css/Styles.css or
> /css/Styles.css
> Neither of which work in the "static" environment used by web designers.
> 
> I hope this elucidates the problem a little further. So, the question persists, what is the right way to get links to CSS files to work in static as well as deployed environment?
> 
> Thanks.
> 
> Hassan Schroeder <ha...@gmail.com> wrote: On 7/4/06, Alec Swan  wrote:
> 
>> I need to reference Styles.css from Test.jsp in such a way that it works in static as well as in deployed mode. Note that for this to work in the static mode (no web servers or servlet containers) all paths should be relative. Therefore, when I deploy Test.jsp I want Tomcat either to handle relative paths correctly or ignore the path altogether and just serve all CSS file from a certain location (directory or URL).
>>
>> The goal of all this is to maintain only one source tree used by web designers and Tomcat administrator and avoid changing paths in JSP files during deployment.
>>
>> I would appreciate any feedback on this.
> 
> OK, since you asked :-)  -- this seems a really strange requirement;
> are your "web designers" unable to run a Tomcat instance to work
> in?
> 
> But in any case, using relative links to a css file works fine on a test
> page I just did that mirrors your described files. So *exactly* how is
> this not working for you?
> 

---------------------------------------------------------------------
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: Serving CSS files from a certain URL

Posted by Alec Swan <au...@yahoo.com>.
Web designers don't run web servers or servlet containers on their machines. (I call their environment "static"). They just create web pages using Dreamweaver and preview them in different browsers. So, in my running example Test.jsp would have the following relative link ../../css/Styles.css in order for it to work in the "static" environment.

However, this relative link doesn't work when I deploy Test.jsp in Tomcat. Suppose I deploy my web-app under /testapp context. In this case, Test.jsp would have to link to the css file through one of the following links:
../testapp/css/Styles.css or
/css/Styles.css
Neither of which work in the "static" environment used by web designers.

I hope this elucidates the problem a little further. So, the question persists, what is the right way to get links to CSS files to work in static as well as deployed environment?

Thanks.

Hassan Schroeder <ha...@gmail.com> wrote: On 7/4/06, Alec Swan  wrote:

> I need to reference Styles.css from Test.jsp in such a way that it works in static as well as in deployed mode. Note that for this to work in the static mode (no web servers or servlet containers) all paths should be relative. Therefore, when I deploy Test.jsp I want Tomcat either to handle relative paths correctly or ignore the path altogether and just serve all CSS file from a certain location (directory or URL).
>
> The goal of all this is to maintain only one source tree used by web designers and Tomcat administrator and avoid changing paths in JSP files during deployment.
>
> I would appreciate any feedback on this.

OK, since you asked :-)  -- this seems a really strange requirement;
are your "web designers" unable to run a Tomcat instance to work
in?

But in any case, using relative links to a css file works fine on a test
page I just did that mirrors your described files. So *exactly* how is
this not working for you?

-- 
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: Serving CSS files from a certain URL

Posted by Hassan Schroeder <ha...@gmail.com>.
On 7/4/06, Alec Swan <au...@yahoo.com> wrote:

> I need to reference Styles.css from Test.jsp in such a way that it works in static as well as in deployed mode. Note that for this to work in the static mode (no web servers or servlet containers) all paths should be relative. Therefore, when I deploy Test.jsp I want Tomcat either to handle relative paths correctly or ignore the path altogether and just serve all CSS file from a certain location (directory or URL).
>
> The goal of all this is to maintain only one source tree used by web designers and Tomcat administrator and avoid changing paths in JSP files during deployment.
>
> I would appreciate any feedback on this.

OK, since you asked :-)  -- this seems a really strange requirement;
are your "web designers" unable to run a Tomcat instance to work
in?

But in any case, using relative links to a css file works fine on a test
page I just did that mirrors your described files. So *exactly* how is
this not working for you?

-- 
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: Serving CSS files from a certain URL

Posted by Alec Swan <au...@yahoo.com>.
The problem is that my JSP uses relative paths to reference CSS files. These paths work fine in the static mode, i.e. when I open JSP file directly in the browser without deploying it on Tomcat. However, when I deploy the JSP all relative links stop working.

Here is an example. Suppose I have the following directory structure:
WEB-INF/jsp/Test.jsp
css/Styles.css

I need to reference Styles.css from Test.jsp in such a way that it works in static as well as in deployed mode. Note that for this to work in the static mode (no web servers or servlet containers) all paths should be relative. Therefore, when I deploy Test.jsp I want Tomcat either to handle relative paths correctly or ignore the path altogether and just serve all CSS file from a certain location (directory or URL).

The goal of all this is to maintain only one source tree used by web designers and Tomcat administrator and avoid changing paths in JSP files during deployment.

I would appreciate any feedback on this.

Thanks.






Hassan Schroeder <ha...@gmail.com> wrote: On 7/2/06, Alec Swan  wrote:

> I have a servlet, which I deploy as a WAR file. I want this servlet to service all .jsp requests, but have Tomcat service .css files from /site/css/ directory inside the servlet WAR file. I don't want Tomcat to invoke my servlet when serving CSS files.

So what's the problem? You map your servlet to *.jsp, and everything
else goes to the DefaultServlet. Right?

How is this not working?
-- 
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: Serving CSS files from a certain URL

Posted by Hassan Schroeder <ha...@gmail.com>.
On 7/2/06, Alec Swan <au...@yahoo.com> wrote:

> I have a servlet, which I deploy as a WAR file. I want this servlet to service all .jsp requests, but have Tomcat service .css files from /site/css/ directory inside the servlet WAR file. I don't want Tomcat to invoke my servlet when serving CSS files.

So what's the problem? You map your servlet to *.jsp, and everything
else goes to the DefaultServlet. Right?

How is this not working?
-- 
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