• Post Reply Bookmark Topic Watch Topic
  • New Topic

servelts + css odd problems  RSS feed

Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While trying to use css's with servlets, I can not make the served
page read in and apply the css that is indicated via "link" (of course
the static version of the page (copied and pasted from the served
page) and embedding the stylesheet within the html works)
I have also noticed that when jsp:forward'ing the welcome page
requests for static pages are being sent to the servlet as well!?!?!
1._ I am forwarding the very first hit on my "home page" to a servlet
"WelcomeCSSImages.java" with the
"<TOMCAT_HOME>\webapps\ROOT\welcome.jsp" jsp like:
<jsp:forward page="/WelcomeCSSImages" />
2._ my webapps\ROOT\WEB-INF\web.xml configuration file looks like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

3._ Here is the code/css example:
// - - - - - - - - - - - - - - CODE - - - - - - - - -
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
public class WelcomeCSSImages extends HttpServlet{
private String azLnSep = System.getProperty("line.separator");
public void doGet(HttpServletRequest Rq, HttpServletResponse Rs)
throws IOException, ServletException{
PrintWriter out = Rs.getWriter();
out.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>");
type=\"text/css\" ?>");
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\"
xml:lang=\"en\" lang=\"en\">");
out.println(" <head>");
out.println(" <title>Weird Servlet/CSS example</title>");
out.println(" <meta http-equiv=\"Content-Type\"
content=\"text/html; charset=ISO-8859-1\" />");
String azCSSRef;
azCSSRef = "./ie/en.css"; // <-- Does not work as served page, but
as a static one it does!
// azCSSRef = "http://localhost/ie/en.css"; // <-- Does not work
out.println(" <link rel=\"StyleSheet\" type=\"text/css\" href=\"" +
azCSSRef + "\" />");
out.println("<hr /><h2>All headers:</h2><br/>");
Enumeration e = Rq.getHeaderNames();
while (e.hasMoreElements()){
String name = (String)e.nextElement();
String value = Rq.getHeader(name);
out.println("<tr><td>" + name + "</td><td>" + value +
// __
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never seen XML and HTML mixed like that before. Is the DOCTYPE declaration valid after the start of a XML document?
At any rate, the XHTML is not well formed. There is no </HEAD> and no <BODY> tag in your code, which is going to cause a problem no matter what. You may want to try removing the xml altogether, and see if the XHTML it returns works first. If so, there's a XHTML/XML conflict.
Hope that helps.
Albretch Mueller
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did check XHTML compliance of the static page using the validator at
prior to print out the page with a servlet:
I do not think at all it is a validation thing. It looks to me like some sort of bug in TOMCAT.
Try the following:
1.) use a jsp:forward declaration in the index.jsp to forward the very first hit to a servlet
2.) Inside of the servlet do something that depends on the request header, let's say we are checking the Language-Accept header to read in a static file (containign style sheets, images, javascript read from a central location) depending on the language read(located in the respective directory (like "./en/..." for English, "./de/..." for German)) the actual text is the only thing that changes
Of course this req. header directory "stringing" must only be done for the actual page, but absolutely all requests (even for images, css, javascript code, ...) are being forwarded to this servlet.
I think Tomcat/Catalina should be smart enough to serve the images directly right?
I could imagine, I don't know all the details of TOMCAT, I am overseeing some very subtle bug, or it is some sort of bug in TOMCAT.
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch! Please adjust your display name to meet
the JavaRanch Naming Policy. You can change it here.
and welcome to the JavaRanch!
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using this method.
Place the css file in dir right under webapps.
like c:/tocathome/webapps/somedirname
For your css line in the servlet code use the following line:
out.println(" <link rel=\"StyleSheet\" type=\"text/css\" href=\"/somedirname/en.css\">");
instead of using the string variable: azCSSRef
And in your stylesheet make sure all of your stylesheet properties are not in double quotes.
for example use:
font-family: arial;
instead of:
font-family: "arial";
For whatever reason tomcat does not process the css files unless you go through these steps.

Albretch Mueller
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But wait, isn't the whole idea about style sheets about using kind of a directory structure that 'cascades' with more particular style sheets for deeper directory levels, ...
If you need to put a stylesheet in each webapp directory (and I could imagine it is the same for each javascript, image, etc) you will be littering the whole webserver directory with the same files for each webapp
I need the same style sheet to work for all pages. I mean wasn't that the idea behind style sheets to begin with?
There you have the whole 'web contexts' idea doing some pretty nasty harm. Isn't it?
Please, let me know, how do you use the same style sheet accross all web contexts. This is really puzzling me now.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!