This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Classic Problem: JSP cannot find css and images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Classic Problem: JSP cannot find css and images" Watch "Classic Problem: JSP cannot find css and images" New topic
Forums: JSP Spring
Author

Classic Problem: JSP cannot find css and images

Moe Ba
Greenhorn

Joined: May 03, 2014
Posts: 5
So, I have the classical problem described in the post title. In my jsp, I am trying to look for the css using this:

<style type = "text/css">@import url(WebContent/css/adminstyle.css);
</style>

In order to help me diagnose the problem, I am trying to print on the jsp itself the value of what the above statement is resolving the url to. How can I get it
to print the value of:

@import url(WebContent/css/adminstyle.css)

I want to know what url the above statement in resulting in after it is evaluated.


[Thumbnail for ques1.png]

[Thumbnail for ques2.png]

[Thumbnail for ques3.png]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

See the JspFaq for info on properly forming resource URLs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Moe Ba
Greenhorn

Joined: May 03, 2014
Posts: 5
Ok I'll check out the FAQ. I've attached some screenshots to show my problem areas. I've tried so many different URL combinations that my head is spinning now. I even tried the JSTL c:url method. No luck getting my Jsp to see my resources(i.e. css, and images folder).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Screenshots are not useful. They are too fuzzy to read and cannot be searched. Please post code as text and UseCodeTags.

Also, please avoid editing your post after someone has responded. Continuing to do that will not make you any friends.

What would be most helpful is the code where you reference the resources, and (precisely) where the resources are in your filer hierarchy. The <c:url> tag will do the right thing if you feed it the correct path.
Moe Ba
Greenhorn

Joined: May 03, 2014
Posts: 5
Hi Bear, thanks for the advice. I will follow it. Sorry about the screenshots. Im a newbie to the forums and to Java EE in general. I was stuck on this problem for 8 hours between 2 days and googled my heart out. In the end, it was the knowledge on the JSP Faq page that led me to the solution. So, thank you thank you thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Glad it worked out. Care to post what the problem was?
Moe Ba
Greenhorn

Joined: May 03, 2014
Posts: 5
The problem was that my jsp couldn't find my css and image resources because I was routing my static resource queries to a path one directory deeper than I should have been. This is because I was trying to find the css and images from the "jsp's perspective" whereas the guidance I received on the jsp faq advised me that one should think from the "servlet's" perspective. So, I made my routing paths in the Spring configuration file shallower by one level. There was also some confusion regarding what directory the "servlet lives in". I thought the servlet behaves as if the application context "lives in" the root directory of the project, but it helped me more to think that it "lives in" the WebContent directory of the project (the Eclipse project). I don't know if any of this makes sense. I barely just learned it so I dare not try to profess it beyond what I saw. Overall like I said I had been stuck on this 8 hours between two different days, I tried to type the URLs and routing paths a million different ways. It was very educational for me. And one solved, rewarding.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Moe Ba wrote:There was also some confusion regarding what directory the "servlet lives in". I thought the servlet behaves as if the application context "lives in" the root directory of the project, but it helped me more to think that it "lives in" the WebContent directory of the project (the Eclipse project).

Actually, it's best to understand the the servlet "lives" nowhere. That's why the JspFaq states to always use server-realtive addressing which makes no assumptions about where the servlet "lives".
Moe Ba
Greenhorn

Joined: May 03, 2014
Posts: 5
Indeed, I read that in many places and it is the best approach. I haven't quite figured out the proper notation/syntax to practice this approach but I do realize that is the ultimate solution as it check application context and uses that no matter what it changes to across deployments. Im just glad I have at least one solution though perhaps not the best one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classic Problem: JSP cannot find css and images
 
Similar Threads
[SOLVED] CSS not being applied to my JSF?
Help with migrating to new server
Trying to get CSS dynamically for multi client
Debugging JSP and Java in Eclipse
SqlException accured at processing Jsp