aspose file tools*
The moose likes Servlets and the fly likes Servlet css file location Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet css file location" Watch "Servlet css file location" New topic
Author

Servlet css file location

William Rouse
Ranch Hand

Joined: Apr 12, 2006
Posts: 73
I have a servlet that is doing some JDBC work and all is well. I get the output that I am looking for and my output code places them in a table. What does not work is connecting to a ".css". I am unable to find the placement of the css file so that the servlet knows where it is or I have the wrong code to connect to the css file. For now the css file is placed in the same directory as the servlet yet it does not see the file. Below is the method that prints out the begging of the HTML and associates the style sheet with the servlet.

void printHTMLhead(){
out.println("<html>");
out.println("<title>Employee Servlet</title>");
out.println("<head>");
out.println("<link rel='stylesheet' type='text/css' href='EmpSearchServlet.css' />");
out.println("</head>");
out.println("<body>");
out.println("<h1> Employee Servlet</h1>");
out.println("<h2>Java Servlet using JDBC</h2>");
out.println("<h3>" + getDate() + "</h3>");
out.println("<hr>");
out.println("<p>Print this sucker out </p>");
out.flush();
}
One last note and that is I am developing with Netbeans if that makes a difference.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
I put .css in WAR_ROOT/css directory. To access them I use:

Some people do not like this approach considering servlet context name hardcoded.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

It's easy to not have to hard-code the context path, but otherwise, using a context-relative path such as dema suggests is what mnost people do.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Rouse
Ranch Hand

Joined: Apr 12, 2006
Posts: 73
if you were using an out.println() method what would the statement look like. Like this:
out.println("<link rel='stylesheet' href='/EmpSearchServlet/css/EmpSearchServlet.css' type='text/css' media='all'/>");
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

You can get the context path from the request instance.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by William Rouse:
I am unable to find the placement of the css file so that the servlet knows where it is or I have the wrong code to connect to the css file.


One thing to keep in mind.
The servlet doesn't need to find it.
Your browser does.

The browser will make requests for linked files (images, css sheets, js pages, etc) with a URL that is relative to the original page unless an absolute path is specified.
It might sound like nit picking but sometimes, understanding this makes debugging these things easier.

Dema's suggestion (make the link relative to the servlet context root) is good but,as he mentioned, hard coding the context path makes it cumbersome to change the apps's name.

As Bear mentioned, it's not difficult to read the context path at run time.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
For now the css file is placed in the same directory as the servlet

That will not work no matter what. Files inside of the WEB-INF directory can not be accessed by an outside client - the servlet container will refuse to serve those files. It must reside in a publicly accessible directory.


Ping & DNS - updated with new look and Ping home screen widget
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
I like to use the HTML "BASE" tag to establish the starting location for the browser to request resources such as CSS files.
Bill
William Rouse
Ranch Hand

Joined: Apr 12, 2006
Posts: 73
Thanks for your help. I was able to contact the css file with your help.
The following works for me:

pathVar = getServletContext().getRealPath("/") + "WEB-INF\\classes" ;
pathVar = pathVar +"\\EmpSearchServlet.css";
out.println("<link rel='stylesheet' type='text/css' href= '" + pathVar + "' />");

You folks are kind.
WBR
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

You put your css file in WEB-INF/classes?

What container are you using that let's you reference files under WEB-INF?

Also, using back-slashes limits you servlet to the Windows environment. I'd recommend not using them.
William Rouse
Ranch Hand

Joined: Apr 12, 2006
Posts: 73
By container do you mean what web server? It is tomcat.
I moved the css file out of the class dirctory and created a css directory on the same level of the web directory. It's location is now:
C:\JavaSource\EmpSearchServlet\build\web\css/EmpSearchServlet.css

I changed the directory seperator from \ to / where I could control it.
It works and I hope that this is a better pratice. The 2 lines are the following:
pathVar = getServletContext().getRealPath("/")+"css/EmpSearchServlet.css";
out.println("<link rel='stylesheet' type='text/css' href= '" + pathVar + "' />");
WBR
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847


needlessly complex, use instead.


42
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

I strongly disagree. You'll get into nothing but trouble using page-relative addressing. Use server-relative adressing with the context path pre-pended.
[ May 05, 2006: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by William Rouse:

pathVar = getServletContext().getRealPath("/")+"css/EmpSearchServlet.css";
out.println("<link rel='stylesheet' type='text/css' href= '" + pathVar + "' />");
WBR


The getRealPath method returns the location of the resource on your server's file system (I.E. "C:\Program Files\Apache Tomcat\webapps\yourapp...").
This not something you ever want to send to a browser.

Use HttpServletRequest.getContextPath which returns the context path (I.E: "/yourapp".)

If it looks like getRealPath is working for you, it's probably becuase you're testing with a browser running on the same machine as your app server. It will fail as soon as someone else tries to hit it from a remote machine.

Priyanka Kshatriya
Greenhorn

Joined: Feb 09, 2009
Posts: 1
@ ben - hey i was facing the same problem . Your solution solved my problem . thanks
 
 
subject: Servlet css file location
 
Similar Threads
servelts + css odd problems
how to use ssi in servlets using apache web server and jserv
uploading of files from html to server
Problem in file uploading
could servlet include the jsp,html files???