Servlet + Firefox + CSS = problem

Here is my problem:

I programmed a servlet which generates some html code. I want the html to look nice so I tried to include a css file.

With the internet explorer everything works quite fine but firefox seems not to support absolute paths. So this does not work with firefox:

<link rel=\"stylesheet\" type=\"text/css\" href=\""+

"Z:/website/style/default.css\"" +

" title=\"default\" media=\"screen\"/>

But what shall I do? The problem is that my servlet is somewhere in the Z:/website/WEB_INF/ directory but my stylesheets are in the Z:/website/style/ directory. Is it possible to call my default.css file with a relative path or is there another possibility to include css in the firefox using an absolute path?

Bye!

[850 byte] By [yellowSubroutinea] at [2007-10-2 16:31:59]
# 1

The location of the CSS file has to be visible from the web server's point of view. The only reason that it works on IE is because you are on the same computer as the server, and because IE makes no difference between URLs and windows paths. Firefox will work with URLs, not windows paths.

If I tried to access that page from my machine, IE will look on MY Z drive, in the folder websit/style/ for a file named default.css. Since I don't have a Z drive (let alone the file you want me to use) it will fail.

Move the CSS to your web server's application path and make the URL either absolute (http://myaddress.com/myapplication/style/default.css) relative to the page (style/default.css) or relative to the domain (/myapplication/style/default.css) but not a windows path (Z:/website/style/default.css).

stevejlukea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanx for your quick answer.

I am still a little confused about what to do. How can I change my path "Z:\..." into a url? I don't have a website, I just want to test my servlet on my PC (Eclipse, Tomcat).

I pass the path "Z:\..." (which can be changed in the web.xml) via getInitParameter() to the servlet. If you want to use my servlet on your PC or website you just have to change the parameter in the web.xml.

I did this because I need to access data from outside the WEB-INF folder. If there is a better method to do so please let me know. The file structure of my build is something like this:

Z:\abc\webapp\

> style\default.css

> lib

> WEB-INF\classes\myservlet\...

> data\...

yellowSubroutinea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> I don't have a website, I just want to test my servlet on my PC

If you don't have a website then there isn't any point in writing servlets. If you're testing on your PC because you are going to install the servlets somewhere else, then you should install something like Tomcat on your PC so you can do proper testing. (File paths instead of URLs aren't proper testing as they won't work in the real environment.)

DrClapa at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

OK, I got the point.

I used a file path because I did not know any other way to access data outside my WEB-INF folder. So that's probably my main problem here.

Why wouldn't this work in the real environment? You only need to change the path "Z:\..." in the web.xml to something like http://www.mywebsite.com

But I am sure there are better ways to handle this problem... Please let me know.

yellowSubroutinea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I moved the folder with my stylesheets into the WEB-INF/classes/ folder. Now I can access the styles with MSIE as well as with Firefox because now I can use the URL http://localhost:8080/myservlets/...

I am no servlet expert as you can see from my questions ;-) Is this really good style? Shouldn't the WEB-INF/classes folder only contain servlets?

yellowSubroutinea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> I moved the folder with my stylesheets into the

> WEB-INF/classes/ folder. Now I can access the styles

> with MSIE as well as with Firefox because now I can

> use the URL http://localhost:8080/myservlets/...

> I am no servlet expert as you can see from my

> questions ;-) Is this really good style? Shouldn't

> the WEB-INF/classes folder only contain servlets?

Yes. The CSS should be in your application's root - the same directory where WEB-INF is located:

<application root>\<static HTML pages and JSP>

<application root>\styles\<css files accessed like /styles/default.css>

<application root>\images\<images accessed like /images/...jpg>

<application root>\WEB-INF\web.xml

<application root>\WEB-INF\classes\<servlet files>

stevejlukea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Then I still have the problem that I can't include the css via an http://... adress but only via file:///Z:/... and the Firefox won't include the css file in my test cases.
yellowSubroutinea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
<application root>\styles\<css files accessed like /styles/default.css><application root>\images\<images accessed like /images/...jpg>The style sheet shlould be accessed as "styles/default.css",an image ilke "images/theImage.jpg" etc.
BIJ001a at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
This won't work because my servlet is in the <application root>/WEB-INF/classes directory, so I can't include the files using a relative path. Any other suggestions?
yellowSubroutinea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
Well if it won't work, you should not bother to try.
BIJ001a at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
^_^ But it bothers me that I can't test my servlet properly with Firefox because Firefox doesn't accept filepaths to my local drive. I also wan't to include some external javascripts which also won't work...
yellowSubroutinea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
Can you try to keep getServletContext().getRealPath("/")+"style/default.css" in the link tag. I guess it'll work.
btarunreddya at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
Thank's for the advice but it still doesn't work because getServletContext().getRealPath("/") returns the filepath Z:/... And the Firefox still doesn't include it.
yellowSubroutinea at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
Stop wasting your time with that Z drive. You already had an excellent explanation of what you should do from stevejluke. Go back and read his posts until you understand them. Especially post #6. Do not use a file: URL or the Z drive at all.
DrClapa at 2007-7-13 17:35:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

Break down the problem.

You want to access (read) a CSS file by including a link to it in your HTML page which is interpreted by the browser. So it is, in fact, the browser that is issuing the request for the CSS file.

A browser is a client application which sends file GET requests to a web server (serving files to a client).

A folder on our system is defined as the document root which is specified in a config file for your web server. From that folder, all documents requested by a browser must reside in that folder. It doesn't matter where that folder is. In fact, when the browser makes that request, it does so by specifying the document root "/" and a path following that. So a file, such as mycss.css located in c:\mydocumentroot\mycssfiles would be requested by the browser as:

GET /cssfiles/mycss.css

No drives letters anywhere in this request. Drive letters are a hold over from the DOS days and have no place in the web. The fact that I.E. allows for them is just confusing for newbies.

As others have stated. You can't test a servlet properly without deploying it to a running servlet container (such as tomcat) and to to server up static files, you'll want a web server (such as Apache). Host your static files in apache (such as mycss.css) and even go so far as to connect apache to tomcat (see docs).

Do it right and you'll have less problems when you finally do deploy your app.

st.murphya at 2007-7-20 22:57:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
You can use Tomcat as a stand-alone web server and servlet container.
BIJ001a at 2007-7-20 22:57:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 17

Thanks a lot guys. I set up the apache http server and adressed my static files with a url. Everything worked just fine.

@BIJ001 How can I use Tomcat as a web server? I didn't know that. I thought it was only for the servlet container.

Still I have some other problems because I must make some objects persistent. I managed to serialize them with xstream to xml files but now I have the problem that I'm not too sure about how to storage these files using urls. Perhaps someone has a good advice for me.

yellowSubroutinea at 2007-7-20 22:57:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 18
Check out Hibernate for persistent storage in a non-EJB environment.
st.murphya at 2007-7-20 22:57:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 19

> How can I use Tomcat as a web server?

> I didn't know that. I thought it was only for the servlet container.

Tomcat has a stand-alone setting where it works as a web server too.

Static stuff put to the document root should be served statically, that is, being a servlet engine requires being a web server too.

BIJ001a at 2007-7-20 22:57:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...