Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSP and the fly likes CSS Not Rendering on JSP that is passed to from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "CSS Not Rendering on JSP that is passed to from Servlet" Watch "CSS Not Rendering on JSP that is passed to from Servlet" New topic
Author

CSS Not Rendering on JSP that is passed to from Servlet

MikeS Miller
Greenhorn

Joined: Mar 06, 2009
Posts: 6
Okay, I will try to explain this intelligently, even though I am a complete greenhorn when it comes to jsp and servlets.

I am working on a school assignment. I have a referral form that someone is supposed to fill in with a few pieces of information. This is then passed to a servlet (this is using an MVC setup), which then passes the properties to one of a few jsp pages.

The part of the assignment that I need to accomplish is done - the properties pass over correctly and the MVC set up works. However, the problem I am getting is very weird.

Here is the series:

referall.jsp submits to scoutReferral servlet. This servlet then responds with a confirm.jsp page that lists the entries for the person to confirm (I know this is not the best way, but the point here is just to get the MVC to work properly).

Well, the confirm.jsp has two css style sheets attached. However, when it displays using this method, the css does not render at all.

Now, what is weird is that if I go directly to the jsp, it not only renders the css properly, it still has the session variables there.

I have checked the source that is getting passed to the browser and it all looks perfect.

What am I missing here?

Here is the link: http://www.paradigmforward.com/pack111/referral.jsp

(Once you fill the form out and see the result using that method, the direct link to the confirm.jsp is http://www.paradigmforward.com/pack111/confirm.jsp)

If you use IE - I know the table is messed up, but I will fix that later.

Here is the code:

referral.jsp:


confirm.jsp


Thanks! Mike
Rosco Duncan
Ranch Hand

Joined: Apr 23, 2007
Posts: 41
Hi Mike,

Are you still stuck on this? You might need to check if your relative paths to the css files are correct

Cheers,
Rosco
MikeS Miller
Greenhorn

Joined: Mar 06, 2009
Posts: 6
Yes - I am still stuck on this. And I have checked and triple checked the paths and they are correct. In fact, they would have to be since the direct confirm.jsp file works right? Maybe I am missing something here.

Mike
Rosco Duncan
Ranch Hand

Joined: Apr 23, 2007
Posts: 41
Hi Mike,

Thats the interesting thing. I think the 'direct access' is using a different base context to when you are getting redirected to this JSP. Im not sure of the structure of your webapp, but you might try switching over to using explicit path references, just to check this. Ie start your path reference with '/'
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

You are using page-relative URLs. That doesn't work in non-direct access. Use server-relative URLs. See the JSP FAQ for more info.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
MikeS Miller
Greenhorn

Joined: Mar 06, 2009
Posts: 6
Bear -

Thanks for that - that is exactly my problem. Now my only question is - the FAQs seem to refer to the images or whatever linked file residing in the root folder of my webapp. However, I am sure this is the case in my webapp. I have the css sheets outside of the root folder of my webapp. For example:

public_html
-confirm.jsp
-referral.jsp
html
-index.html
-other.html
css
- style.css
scripts
flash
WEB-INF
-controller servlet


The names without dashes are directories obviously, and the dashed items are the files.

If I am reading the FAQ correctly, it is referencing the code to use as if the css was located in the WEB-INF folder? Or am I reading that wrong?

So I suppose my question is, given the above folder structure, what would I need to code in order to make this work? And where do I put that code at? In the JSP or in the servlet?

The funny thing is that I thought it might have been something like this, since there were no images getting pulled in except those that were referenced directly using an absolute URL (the W3C Validators).

Thanks again for your help!

Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

You're reading it wrong. Anything in the WEB-INF folder can't be directly served so putting css files there would make no sense.

I can't grok your file structure from what you posted (you can use code tags to preserve formatting), but let's say that your CSS files were in a folder named styles in the web root.

The proper URL to such files would be:


where /appcontext is whatever your context path is. (Don't hard-code it, get it from the EL as described in the FAQ)
MikeS Miller
Greenhorn

Joined: Mar 06, 2009
Posts: 6
Ha ha! Yeah, I was wondering if anyone would be able to figure that structure out! Anyhow, I think I understand what you are saying and I can play around with it until it works out right.

So, my only question that remains is where do I post this path? In the JSP where I have it coded already? But wait, that would be hardcoded then wouldn't it?

Just to clarify, when I get the JSP as passed to from the servlet, this is what the path is:



So, if the file is in a folder named styles, then would the following be way my path should be?



or would it be



If my web.xml is mapped like this:



I think I am getting close here

Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Why the bogus "/servlet" prefix on your mapping? It looks amateurish and doesn't add anything except complexity.

And, the FAQ entry tells you exactly what the path should be. It should start with the context path of the web application (obtained via ${pageContext.request.contextPath}) and have the full path to the file from the root of the context. Your servlet mapping has nothing at all to do with it.
MikeS Miller
Greenhorn

Joined: Mar 06, 2009
Posts: 6
Well, it would make sense that it looks amateurish as this is the first one that I have ever done for my web applciations class. I was under the assumption that had to be there, based on how my host has the server set up, but I may be misunderstanding. I am trying to learn the basics of how it works and then will fine tune using best practices. This is only for a test site at the moment, so that does not matter.

By the way, I figured out the correct path and it works great! Now the only thing is that all of my links and such in my menu, etc. all need to be updated that way. Or is there an easier way? I am sure that I am doing a lot of stuff the hard way initially, but like I said, I am learning.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Yup, all URLs should be structured like that. You might also find the <c:url> JSTL tag helpful as it automatically adds the correct context path prefix.
MikeS Miller
Greenhorn

Joined: Mar 06, 2009
Posts: 6
I will check that out. That could be helpful. Thanks again for the help I understand it a lot better now. Still a million miles away from where I want to be, but this helps a lot!

Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Sounds like you are making a good start and trying to do things the right way. Keep it up!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CSS Not Rendering on JSP that is passed to from Servlet
 
Similar Threads
how use a <li>with <h:commandLink> ?
set values in a table from other
Forwarding users from one JSP to another using a form.
Struts 2
Drop Down Menu Problem