Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2004

Tip: Looking for answers? Try searching our database.

calling a stylesheet from a servlet..

Thread view: 
Frances Del Rio - 26 Nov 2004 06:14 GMT
I'm trying to call a stylesheet from a servlet but it's being ignored..

in servlet I have:

println(all the usu. stuff, <html>,<head>...)
println("<link rel=\"stylesheet\" type=\"text/css\"
href=\"css-ham.css\">\n");

stylesheet is in same dir where serlvet is..  but it's being ignored by
servlet.. (am wondering if the same thing would happen if I call a .js
file from servlet, this would be a pain..)  thank you..  Frances
SMC - 26 Nov 2004 09:54 GMT
> I'm trying to call a stylesheet from a servlet but it's being ignored..
>
[quoted text clipped - 3 lines]
> println("<link rel=\"stylesheet\" type=\"text/css\"
> href=\"css-ham.css\">\n");

You're not calling anything. You're actually just printing out HTML. Use
your browser and point at http://yoursite/css-ham.css. Nothing come up?
That would be because your webserver can't find the css file. Are you
using a your servlet engine as your webserver?

> stylesheet is in same dir where serlvet is..  

That's probably the problem. What's your webserver's document root? Your
servlet engine probably isn't configured to serve css files.

> servlet.. (am wondering if the same thing would happen if I call a .js
> file from servlet, this would be a pain..)

Yes, for the same reasons.

Now if you actually want to have the servlet print out the contents of the
css file, that's another story. What you're doing (whether you meant it or
not) is probably better, but requires you to understand what your servlet
engine and/or webserver does and how they're configured to serve content.

Sean
Frances Del Rio - 26 Nov 2004 16:01 GMT
>>I'm trying to call a stylesheet from a servlet but it's being ignored..
>>
[quoted text clipped - 23 lines]
> not) is probably better, but requires you to understand what your servlet
> engine and/or webserver does and how they're configured to serve content.

Sean, thank you very much for yr help..  first off, this is in my tomcat
locally, haven't even gotten as far as transferring all this to my
website yet (which I will be doing also..) in
webapps/appname/WEB-INF/classes/pkgname I have the class (plus another
little class I wrote to generate the some of the HTML, this is where css
 sheet is being called..) and of course the stylesheet..  no, of course
I don't want servlet to PRINT the contents of the stylesheet, I want the
HTML generated in the servlet to you know, pull the stylesheet and USE
it, the way it happens in HTML..  gosh this is weird..  I mean
alternative would be to put all the css code in servlet, which as you
can imagine would be a pain.. there has to be way to do this (also for
.js files..) just in case this is code for the HTML class..  you will
see I call a bg image in body tag, that works w/no problems...
http://www.francesdelrio.com/htmlGen.html..  thank you again..  Frances
Andrew Thompson - 26 Nov 2004 16:41 GMT
> I'm trying to call a stylesheet from a servlet but it's being ignored..
>
[quoted text clipped - 3 lines]
> println("<link rel=\"stylesheet\" type=\"text/css\"
> href=\"css-ham.css\">\n");

Where *exactly* is your stylesheet, Frances?

Report the full path* you would need to put in the browser
to load the stylesheet itself in the browser.  * Please *test*
this path, actually copy/paste the URL that you used.

[ I suspect I know the problem, but will know more once you've
reported back. ]

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Frances Del Rio - 26 Nov 2004 16:50 GMT
>>I'm trying to call a stylesheet from a servlet but it's being ignored..
>>
[quoted text clipped - 12 lines]
> [ I suspect I know the problem, but will know more once you've
> reported back. ]

Andrew, thank you for your response..  previous person who responded
asked me the same question, and here's what I responded:

Sean, thank you very much for yr help..  first off, this is in my tomcat
locally, haven't even gotten as far as transferring all this to my
website yet (which I will be doing also..) in
webapps/appname/WEB-INF/classes/pkgname I have the class (plus another
little class I wrote to generate the some of the HTML, this is where css
 sheet is being called..) and of course the stylesheet..  no, of course
I don't want servlet to PRINT the contents of the stylesheet, I want the
HTML generated in the servlet to you know, pull the stylesheet and USE
it, the way it happens in HTML..  gosh this is weird..  I mean
alternative would be to put all the css code in servlet, which as you
can imagine would be a pain.. there has to be way to do this (also for
.js files..) just in case this is code for the HTML class..  you will
see I call a bg image in body tag, that works w/no problems...
http://www.francesdelrio.com/htmlGen.html..  thank you again..  Frances
Andrew Thompson - 26 Nov 2004 17:27 GMT
>>>I'm trying to call a stylesheet from a servlet but it's being ignored..
>>>
[quoted text clipped - 15 lines]
> Andrew, thank you for your response..  previous person who responded
> asked me the same question,

Yes. I read that response.  Now please follow my instructions
*exactly* as I have put them above.  I think it will reveal
part of the problem for you.

I want you to paste the URL (not a *description* of your file
layout) that you have used to bring up the stylesheet in the browser.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Frances Del Rio - 26 Nov 2004 17:42 GMT
>>>>I'm trying to call a stylesheet from a servlet but it's being ignored..
>>>>
[quoted text clipped - 22 lines]
> I want you to paste the URL (not a *description* of your file
> layout) that you have used to bring up the stylesheet in the browser.

Andrew, I'm trying really hard to understand what you mean..  I mean I
typed 'css-ham.css' in browser, but obviously nothing happened (not even
a 404.. I guess b/c it's a .css file..)  I mean the ref to css sheet is
a RELATIVE ref. from where servlet is...  I put a test html file in same
dir where servlet is and where css sheet is and called css sheet from
this test html and it worked fine.. so, I still don't understand.. why
the servlet won't call it ok...  ????  many thanks for your help..  very
much appreciated as always..  Frances
Andrew Thompson - 26 Nov 2004 17:55 GMT
(A.T.)
>>>>Report the full path* you would need to put in the browser
>>>>to load the stylesheet itself in the browser.  * Please *test*
>>>>this path, actually copy/paste the URL that you used.
>>>>
>>>>[ I suspect I know the problem, but will know more once you've
>>>>reported back. ]
...
> Andrew, I'm trying really hard to understand what you mean..  

(sigh) OK.  Let's say I have the following files..
...ROOT/test/index.htm
...ROOT/test/general.css
...ROOT/WEB-INF/test.css

The URL I might type to bring up general.css is

http://localhost:8080/test/general.css

>..I mean I
> typed 'css-ham.css' in browser, but obviously nothing happened (not even
> a 404.. I guess b/c it's a .css file..)  

You seem to be doing a lot of guessing..    ;-)

>..I mean the ref to css sheet is
> a RELATIVE ref. from where servlet is...  I put a test html file in same
> dir where servlet is and where css sheet is and called css sheet from
> this test html and it worked fine..

<hint>
Did you try loading the 'test.html' in Tomcat as
http://localhost:8080/WEB-INF/classes/pkgname/test.html  ?
</hint>

If not, try that.  What happens?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Frances Del Rio - 26 Nov 2004 18:11 GMT
> (A.T.)
>
[quoted text clipped - 35 lines]
>
> If not, try that.  What happens?

ok, got me there, Andrew....  (get a tomcat-404..)   now what?

.....  [thinking.....]   ;)

ok, put the css shett in root of app (where html form is that calls
servlet..)  and modifed call to css sheet in servlet accordingly and
voila!!  :)  now: if you could explain to me why it's not possible to
put css sheet in same dir as servlet and call it accordingly from
servlet that would be great..  again, many thanks!!  Frances
Juha Laiho - 26 Nov 2004 22:44 GMT
Frances Del Rio <fdr58@yahoo.com> said:
>ok, put the css shett in root of app (where html form is that calls
>servlet..)  and modifed call to css sheet in servlet accordingly and
>voila!!  :)

Yep. I think the most common way I've seen is that stylesheets are in
/css/ subdirectory from the context root. But then, if you have just
one or few stylesheet files, there's no gain from the extra directory,
so you can leave them in root.

>now: if you could explain to me why it's not possible to put css sheet
>in same dir as servlet and call it accordingly from servlet that would
>be great..

web.xml is pretty much the key here. That is what you use to map your
server classes to any convenient location in the resource tree available
for browsers. However, web.xml only applies for servlets (ok, can be
made to apply for JSPs, too, but that's not common - at least based on
my experience).

You have the /WEB-INF/ tree, which is something that your server never
allows to be directly accessible to browsers. This is where you have
your servlet classes. Then, with web.xml you map your servlets into
the url-space, to whatever locations you choose (depending on your
ISP/server setup, your choices may be rather limited).

Now, the browsers access your servlets by their url names, they know
nothing about the /WEB-INF/ directory structure, or servlet class names
either, for that matter. Let's assume that you've made a mapping with
web.xml so that there's a servlet responding to url /servlet/test.
Now, the browser will access http://server/servlet/test, and will
see the relative stylesheet link in the HTML generated by the servlet.
The browser will then attempt to access http://server/servlet/file.css,
but there's no way to tell Tomcat that this should be served from
somewhere within /WEB-INF/ (and that wouldn't make sense, either),
you'll get a 404.

Now, to another scenario; you have a servlet that generates a list of
products, which is mapped to /product/list. However, you also have
a real directory called /product (within your application context
directory, so at the same level with /WEB-INF/), containing a file
list.css. The HTML generated by the list servlet will contain
a relative reference to list.css. This will work, because from
the browser point of view the 'list' and 'list.css' resources both
are in the directory /product.

I hope this clarified the issue.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Frances Del Rio - 27 Nov 2004 07:12 GMT
> Frances Del Rio <fdr58@yahoo.com> said:
>
[quoted text clipped - 44 lines]
>
> I hope this clarified the issue.

not 100% but to a certain degree..  I guess one more of those quirky
servlet things...  ;)  I was going to say bottom line is you can't call
from a browser something called by the servlet b/c can't call servlet
directly from browser, but servlet CAN call stylesheet -- IF ss is
outside WEB-INF.. is what I gather..  thank you very much for taking the
time..  Frances
Chris Smith - 27 Nov 2004 07:24 GMT
> not 100% but to a certain degree..  I guess one more of those quirky
> servlet things...  ;)  I was going to say bottom line is you can't call
> from a browser something called by the servlet b/c can't call servlet
> directly from browser, but servlet CAN call stylesheet -- IF ss is
> outside WEB-INF.. is what I gather..  thank you very much for taking the
> time..  Frances

Your messages are somewhat hard to follow because of your unusual use of
the word "call".  It is true that the HTML written by a servlet can
refer to a stylesheet only if the path to the stylesheet works.  Files
inside WEB-INF are never made directly available by the web browser, so
you generally can't put your stylesheet there.

(Of course, if you wanted to be fancy, you could put your stylesheet in
WEB-INF, and then write and map a new servlet to read the file and send
it out to the browser manually.  Your first servlet would then need to
specify the path to this second servlet as the stylesheet path.)

This all makes a lot more sense if you think of the situation about one
level lower than you are currently thinking.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Andrew Thompson - 27 Nov 2004 03:12 GMT
> and modifed call to css sheet in servlet accordingly and
> voila!!  :)  

Yay!

>...now: if you could explain to me why it's not possible to
> put css sheet in same dir as servlet and call it accordingly from
> servlet that would be great..

..and gee am I glad that Juha explained the details. I just
know that Tomcat does not like serving things from out of WEB-INF.  ;-)

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Frances Del Rio - 27 Nov 2004 07:13 GMT
>>and modifed call to css sheet in servlet accordingly and
>>voila!!  :)  
[quoted text clipped - 7 lines]
> ..and gee am I glad that Juha explained the details. I just
> know that Tomcat does not like serving things from out of WEB-INF.  ;-)

indeed...  I gather from Juha's explanation that, just like you say, you
CAN call stuff from servlet -- as long as what you're calling is outside
WEB-INF...  thank you again very much for your help, Andrew...  Frances


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.