Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attaching Style Sheets to Servlets

 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Im wondering is it possible to import css stylesheets into a servlet. If its possible, could somebody tell me how to do it.
Thanks
Brian
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your servlet (I am assuming you are contructing output directly in the servlet rather than forwarding to a JSP?) is constructng HTML to send to the client. You would merely include the appropriate HTML (a <link> tag) to include the stylesheet just like any other HTML page.

Or am I missing something?
[ April 14, 2005: Message edited by: Bear Bibeault ]
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about "inlining" the stylesheet as a style HTML element?
 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Im still having a bit of trouble importing the style sheet.
The line of code I tried in my servlet was the following.
out.println("<link href="servletstyle.css" rel="stylesheet" type="text/css">");

Here is a larger segment showing you exactly where I put it inside the servlet.
Any help is greatly appreciated
Thanks
Brian
 
Brian Mulvany
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to paste in the larger segment

out.println("<html>");
out.println("<head><title>Servlet1</title>");
out.println("<link href="servletstyle.css" rel="stylesheet" type="text/css">");
out.println("</head>");
out.println("<body>");
out.println("<p>You searched for cds by ");
out.println(request.getParameter("target"));
out.println("<p>");
out.println("Here are the results");


There it is
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What URL are you using to access your servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using a page-relative reference to your CSS file. That won't work in a servlet.

Change the URL to be relative to your web app context. For example, if it's in a folder named styles at the web app level: "/styles/stylesheet.css".
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

Change the URL to be relative to your web app context. For example, if it's in a folder named styles at the web app level: "/styles/stylesheet.css".


That won't work.
The preceeding "/" will cause the browser to omit the context path.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Mulvany:
I forgot to paste in the larger segment

out.println("<html>");
out.println("<head><title>Servlet1</title>");
out.println("<link href="servletstyle.css" rel="stylesheet" type="text/css">");
out.println("</head>");
out.println("<body>");
out.println("<p>You searched for cds by ");
out.println(request.getParameter("target"));
out.println("<p>");
out.println("Here are the results");


There it is



Does this code compile? Are you really using the unescaped " character within a String literal? If so, it won't work. That'd be a compile error.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The preceeding "/" will cause the browser to omit the context path.


Doh! I forgot that. Yes, you need to preceed my example with the context path.

Thanks for the field Ben!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow on dumb question: why aren't you forwarding to a JSP for the view?
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ...

we can incorporate Cascading style sheets in html / jsp / asp or evne in servlet...

in servlet we usally wont use " ".
we will use instead \" right...
Pls do check the .css wW R T O context path..
Thx and Regards
G R Ganesh Gowtham
grgowtham_cse@yahoo.com
ganesh.gowtham@gmail.com
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Carman:



Does this code compile? Are you really using the unescaped " character within a String literal? If so, it won't work. That'd be a compile error.


AS James said u cant able to complie the servlet w.o errors coz u used
unescape'd charcater " instead use "\
Ok....

Thx and Regards
G R Ganesh Gowtham
grgowtham_cse@yahoo.com
Capricornsys inc, Hyderabad

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic