aspose file tools*
The moose likes Servlets and the fly likes Attaching Style Sheets to Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Attaching Style Sheets to Servlets" Watch "Attaching Style Sheets to Servlets" New topic
Author

Attaching Style Sheets to Servlets

Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
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

Joined: Jan 10, 2002
Posts: 60056
    
  65

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Are you talking about "inlining" the stylesheet as a style HTML element?


James Carman, President<br />Carman Consulting, Inc.
Brian Mulvany
Greenhorn

Joined: Oct 19, 2004
Posts: 28
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

Joined: Oct 19, 2004
Posts: 28
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

Joined: Dec 11, 2004
Posts: 13410

What URL are you using to access your servlet?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Feb 20, 2001
Posts: 580
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

Joined: Jan 10, 2002
Posts: 60056
    
  65

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

Joined: Jan 10, 2002
Posts: 60056
    
  65

Follow on dumb question: why aren't you forwarding to a JSP for the view?
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

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


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attaching Style Sheets to Servlets
 
Similar Threads
bean:write query
Is it possible to write SCJP 6.0 by using SCJP 5.0's coupon
invoking java function with java script Onclick event
Can one action class have more than one Form class
button