• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with request.getContextPath() when redirecting

 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i fixed it when i realised the contextpath is server relative, that has done the trick. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic