Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet css file location

 
William Rouse
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Rouse
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the context path from the request instance.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


needlessly complex, use instead.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ ben - hey i was facing the same problem . Your solution solved my problem . thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic