Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

CSS stylesheet will not work with JSP files  RSS feed

 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know why I can't get an external style sheet work work when using JSP files? Putting a link in the doc head like I would for an HTML or PHP file does not work. The style sheet is in the exact same directory as the JSP files and I cannot get it to work. I think I have tried about everything that has been suggested on the web with no luck.

If I create an HTML doc in the same directory as the JSP files, and add the below line in the document head, it works fine. Doing the same thing with a JSP file does nothing.

 
Ron McLeod
Saloon Keeper
Posts: 1424
206
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you put your style sheet?

Sorry - I missed that you said it was with the JSP. Where did you put the JSP?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the JspFaq on referencing resource URLs. You should be using a server-relative URL that starts with the context path.

(And make sure it's not under WEB-INF)
 
Akhil Pratap Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try closing the link tag <link rel="stylesheet" type="text/css" href="style.css"/>
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<link href="${pageContext.request.contextPath}/common/styles/mystyles.css" rel="stylesheet" type="text/css" >

You must provide a complete path to the page including the context. Using the style above will insure that it works even on different servers with different contexts. Of course your path will vary from my example; that's up to you to sort out.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether the link tag is closed or not depends on whether one is using old-fashioned XHTML or HTML. It has nothing to do with anything else.

The fix is to follow my and J. Kevin's advice to use server-relative URLs.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Whether the link tag is closed or not depends on whether one is using old-fashioned XHTML or HTML. It has nothing to do with anything else.

The fix is to follow my and J. Kevin's advice to use server-relative URLs.


As I said, I have tried about everything I have found on the web, and that includes the below code. I have also tried the @import method. This is driving me nuts.

Could it have anything to do with the location of the files?

The JSP files are in the WebContent directory and that is also where the CSS file sheet is as well.


 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this and it worked although I thought I tried it last night also.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Good grief! Why would you do that?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Gillespie wrote:
The JSP files are in the WebContent directory and that is also where the CSS file sheet is as well.


You're running inside an IDE aren't you? That complicates things. It's been suggested elsewhere that running outside the IDE is the best way to make sure things work without the cruft and complications that an IDE adds (like a bogus WebContent folder).

In any case, you need to find out what the folder structure the IDE expects and to add that.

For now just use the address bar of the browser to find the right URL rather than screwing around inside the JSP. When you find the URL that lets the browser display the CSS file as text, you'll know you have the right URL.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!