0

Actually when I run a particular JSP page it run perfectly(with external style sheet) but when servlet return back the control through RequestDispatcher, the page is not displaying properly means it don't took the css file path. In my program the stylesheet is present inside css directory and the page is present inside another directory named JSP.

The code which include the css in the program is-

 <link rel="stylesheet" href="../css/stylesheet.css" type="text/css"/>
sujoy
  • 299
  • 1
  • 2
  • 8
  • What path does the browser end up seeing for the CSS? – Thilo Nov 28 '11 at 04:39
  • @thilo : sorry i don't understand ur question..The adress shown by browser is the path of that servlet – sujoy Nov 28 '11 at 04:42
  • 1
    What URL does the browser try to load the stylesheet from? Is the path correct? The relative path in the tag must match the URL (as seen by the browser) of the page that includes it. – Thilo Nov 28 '11 at 04:45
  • @thilo: Yes the path is correct. – sujoy Nov 28 '11 at 04:47
  • So the browser could load the CSS? Not a 404 File Not Found? – Thilo Nov 28 '11 at 04:48
  • @thilo: when I run the page directly I ,the page load properly,but it create problem when i try to run it through servlet using RequestDispatcher – sujoy Nov 28 '11 at 04:53
  • I think you're misunderstanding @Thilo's question, sujoy - not, does it load the JSP page, but does the JSP page (when rendered as HTML) load the CSS page successfully? You should be able to find this out using a browser inspector (Opera Dragonfly, Chrome inspector, Firebug, or similar). If you don't know how to use one of those, try putting in an easily testable style (such as red background) and see whether the page's style changes. Or look at the page source in a browser and check that the stylesheet's URL relative to the page's URL is correct (verify by retrieving it directly). – Amos M. Carpenter Nov 28 '11 at 13:42
  • @aaamos: Thanks for your kind advice. Actually I checked the page and I found that it can't locate css file. But now I am able to fix the problem. Thank you all for your kind advice. – sujoy Nov 29 '11 at 06:30
  • Glad you got it working, sujoy, I think that's what Thilo was getting at. For things like CSS and JS files, I'd recommend using an absolute path from the web root rather than a relative URL as you had it. That way, you can move your JSP files around without breaking references. – Amos M. Carpenter Nov 29 '11 at 08:14

4 Answers4

2

Try URL-encoding your CSS file location. If you use taglibs, this might look something like:

<link href="<c:url value="/path/to/stylesheet.css"/>" rel="stylesheet"
    type="text/css" />

where you define the path to the stylesheet from your app's web root.

If you want to use multiple stylesheets, you do something like:

<c:set var="stylePath" value="/path/to/cssFolder" />
...
<link href="<c:url value="${stylePath}/stylesheet.css"/>" rel="stylesheet"
    type="text/css" />
Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
1

Can you access the css url in the browser? You can create the full path using context path in jsp.(You can get the context path using request.getContextPath() in JSP)

mjlowky
  • 1,183
  • 12
  • 19
0

May be you have to use <base/> html tag to resolve the relative path.

<head>
<base href="/testfolder/" />  
<link rel="stylesheet" type="text/css" href="../css/stylesheet.css"/>
Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0
<link rel="stylesheet" href="<%=cssFilePath%>" type="text/css" />

Try this by declaring a String cssFilePath = "YOUR_CSS_FILE_PATH" in your jsp.

Sachin Karjatkar
  • 313
  • 3
  • 13