Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

requestDispatcher() & sendRedirect()

 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Following is my webapp structure.

Control Flow :
1)Once I access my webapplication through a browser it goes to the index.jsp
2)I had just forwarded that using sendRedirect to file1.jsp.
3)In file1.jsp i will enter username,password & click the submit button.
4)which will transfer the control to the servlet serv.java

Upto this step everything works fine.

Now inside my servlet I will generate a dynamic html page and save that to a folder named "output"
please consider file2.html is one such html file which uses the "prop.css" & "validation.js" for broswer display & script validation

Now when i use either of the follwoing two lines inside my servlet

res.sendRedirect("output/file1.html");
=> i could able to see the style sheet i has used in my
css & also the java script.
BUT when i use
objServletContext.getRequestDispatcher("/output/file1.html").forward(req,res);
=> I had just recived an html page on my screen without
any javascript / i could not see any style sheet on my broswer.

Pls. note i had check all the path verification when i included the .css & .js file in my html AND
another point to mention here is when i open the html file manually using the browser it displays everything perfectly.


I had not set any headers to my HttpResponse....

Let me know what basic knowledge i'am missing out here.

Thanks in advance

[BSouther: Added UBB Code tags to directory tree]
[ December 07, 2006: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably using page-relative addressing in the URLs for your images and style sheets. This is problematic in web applications. You should be using server-relative addressing that begins with the context path.
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault,
but could you explain why the problem doesn't occur when i use sendRedirect()???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because a redirect comes from the browser and the URL known to the browser is the HTML page. In a forward, the URL known to the browser is the servlet URL and it has no knowledge that you've forwarded to an HTML page.

It's best to just always use server-relative addressing for image, CSS and script files. Such references will always work regardless of how the page was displayed.
 
Raj Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly said Bear Bibeault, thanks for the reply. i got that fixed.

bye...
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic