• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

requestDispatcher() & sendRedirect()

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear Bibeault,
but could you explain why the problem doesn't occur when i use sendRedirect()???
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly said Bear Bibeault, thanks for the reply. i got that fixed.

bye...
 
reply
    Bookmark Topic Watch Topic
  • New Topic