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
posted
0
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>
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'/>");
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.
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.
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
posted
0
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
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 ]
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
posted
0
@ ben - hey i was facing the same problem . Your solution solved my problem . thanks
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.