GeeCON Prague 2014*
The moose likes Servlets and the fly likes requestDispatcher() & sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "requestDispatcher() & sendRedirect()" Watch "requestDispatcher() & sendRedirect()" New topic
Author

requestDispatcher() & sendRedirect()

Raj Kumar
Ranch Hand

Joined: Sep 19, 2005
Posts: 65
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

Joined: Jan 10, 2002
Posts: 61310
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Raj Kumar
Ranch Hand

Joined: Sep 19, 2005
Posts: 65
Thanks Bear Bibeault,
but could you explain why the problem doesn't occur when i use sendRedirect()???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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

Joined: Sep 19, 2005
Posts: 65
Exactly said Bear Bibeault, thanks for the reply. i got that fixed.

bye...
 
GeeCON Prague 2014
 
subject: requestDispatcher() & sendRedirect()