wood burning stoves 2.0*
The moose likes Servlets and the fly likes Problems with request.getContextPath() when redirecting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problems with request.getContextPath() when redirecting" Watch "Problems with request.getContextPath() when redirecting" New topic
Author

Problems with request.getContextPath() when redirecting

Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Hi,

I am having problems when i redirect a user to a page in another folder, basically all relative links dependant on request get broken.

For example this is my css link:


This page and this line work fine ordinarily, but when someone is redirected to this page as opposed to clicking a link / direct access the css doesnt work and other relative links on this page based on request dont work.

This is how i redirect from another page:



How do i fix this? Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Firstly, why on earth is that line of HTML in a an out.println()?

Are you writing HTML inside Java inside a JSP? Really? The whole purpose of a JSP is to be able to put static markup in template text. Not embedded in strings in code.

Back up and start writing the JSP correctly and you will be amazed how many problems disappear. No Java code. No convolutions. Straight and simple using the JSTL and EL. Otherwise, you're just digging yourself into problems that don't need to exist in the first place.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

In template text the markup would simply be:

I can see no reason why a redirect would cause any issues with that. You'll need to provide more information.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Stevie Shorey wrote:This is how i redirect from another page:

That is a forward, not a redirect. They are not the same thing.

I'm still not seeing any problems. The server-reltative URL of the CSS will be independent of the URL to the page. So forwards (or redirects) should have no effect.

Does the HTML sent to the browser look correct?
Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Bear Bibeault wrote:Firstly, why on earth is that line of HTML in a an out.println()?

Are you writing HTML inside Java inside a JSP? Really? The whole purpose of a JSP is to be able to put static markup in template text. Not embedded in strings in code.

Back up and start writing the JSP correctly and you will be amazed how many problems disappear. No Java code. No convolutions. Straight and simple using the JSTL and EL. Otherwise, you're just digging yourself into problems that don't need to exist in the first place.


I can see it looks bad but that is a line from a servlet not a JSP
Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Here is the extended code if this helps debug the problem:

index.jsp (if user isnt allowed to be here, redirect to LogoutServlet )




And this is LogoutServlet (which is the redirection address above)
Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

if i link to the logoutServlet it works fine and the css displays fine. The problem occurs when i forward using the requestDispatcher,

When i click on View-Source for this css-less logoutServlet, it is linking from the previous directory for some reason.
This is the basic structure:

logoutServlet
home/style.css
admin/index.jsp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Stevie Shorey wrote:I can see it looks bad but that is a line from a servlet not a JSP


That's not much better. Why are you emitting HTML from a servlet instead of using a JSP?

When i click on View-Source for this css-less logoutServlet, it is linking from the previous directory for some reason.

I don't know what you mean by that.
Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Bear Bibeault wrote:
Stevie Shorey wrote:I can see it looks bad but that is a line from a servlet not a JSP


That's not much better. Why are you emitting HTML from a servlet instead of using a JSP?

Some pages have a lot of java code in them for logic, so i didnt want to put them in a scriptlet inside a jsp, hence this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Stevie Shorey wrote:
Some pages have a lot of java code in them for logic, so i didnt want to put them in a scriptlet inside a jsp, hence this.


Mixing logic with the view is a poor approach regardless of whether you do it in a JSP or a servlet. It's still a violation of Separation of Concerns.

Your logic should be preformed in the model or controller (depending upon whether it's business or control logic), and then the JSP (without scriptlets) is used to create the view.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 316
    
    2

I think the problem is with the context path



Take a look at what the resulting html looks like.

What I think you want is 'href="/home/style.css' which would be relative to your website or perhaps 'http://myreallycoolsite.gov/home/style.css'

I'm not as dead set against emitting html from Java code as Bear is. I think his position is more accepted by the community but I have sites that do everything programatically. They certainly don't use the MVC pattern, but the design is pretty clean and they have been fairly easy to maintain and the output html of all pages validates without error.

One of these days we can each get a 6-pack of Moose Piss Beer (If I can find it in California) and argue about it in a thread.

Joe


It's not what your program can do, it's what your users do with the program.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Joe Areeda wrote:What I think you want is 'href="/home/style.css' which would be relative to your website or perhaps 'http://myreallycoolsite.gov/home/style.css'

Nope. The URLs need to be server-relative. How would the container know which web app is being addressed without the context path?

If it's the root application, the context path is "/", but it still needs to be included.

Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 316
    
    2

Bear Bibeault wrote:
Joe Areeda wrote:What I think you want is 'href="/home/style.css' which would be relative to your website or perhaps 'http://myreallycoolsite.gov/home/style.css'

Nope. The URLs need to be server-relative. How would the container know which web app is being addressed without the context path?

If it's the root application, the context path is "/", but it still needs to be included.


You're right. Problem with starting to type before I check what I'm going to say.

Joe
Stevie Shorey
Ranch Hand

Joined: Dec 10, 2012
Posts: 45

Yeah i fixed it when i realised the contextpath is server relative, that has done the trick. Thanks
 
Consider Paul's rocket mass heater.
 
subject: Problems with request.getContextPath() when redirecting