• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

damn the requestDispatcher

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have read lots of threads that try to clarify the paths associated with a requestdispatcher.
but the damn thing isnt yet clear. i have probelms even while running some sample applications of my own, which usee the requestdispatcher

i have a servlet that says

RequestDispatcher rd = getServletContext().getRequestDispatcher("/result.jsp");

but when i run my app, i see a blank page, or in other words, contents from the result.jsp arent displyed on the browser
where should i place the result.jsp ?? Please mention propery the exact directory structure

Now if i make the servlet to say -

1) RequestDispatcher rd = request.getRequestDispatcher("/jsp/result.jsp");

where do i keep the "jsp" folder ? should i keep it as
myApp/WEB-INF/jsp/reult.jsp OR
myApp/jsp/result.jsp ??

2) RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
again the same question-- where should i keep the result.jsp ?

3) RequestDispatcher rd = request.getRequestDispatcher("/result.jsp");
where should i place the result.jsp ??


4)RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/result.jsp");

where do i keep the "jsp" folder ? should i keep it as

myApp/WEB-INF/jsp/reult.jsp OR
myApp/jsp/result.jsp ??

When you reply to my query.please dont bluff the typical lines from the spec- "relative to the context root" or "relative to current request

i want a dirctory structure for all the POSSIBLE paths that we can use in getRequestDispatcher(String path)
i would be grateful if you give a explanation lik - "if the path is "...../..../....jsp" then place the jsp in "..../.../..." where .... is directory name.

the whole thing is f****ing me a lot, and i want to get a solid understaning of it in my mind.

"EVEN IF I AM TAKING CARE OF THE DIRECTORY STRUCTURE AND THE PATHS, WHAT COULB B THE REASON THAT I M NOT
GETTING A BLANK PAGE AS TEH OUTPUT??"

please help out
niranjan
 
Kenny Yan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) If the path inside the getRequestDispatcher(...) starts with a '/', it should be relative to the context root[your folder under webapps], which is "myApp/" for ur case, and the the path for the jsp is "myApp/jsp/result.jsp"

2) If the path inside the getRequestDispatcher(...) doesn't start with a '/', it is relative the CURRENT request[hard to explain without saying it...I would include an example]. Say now you are accessing ur app thru
http://localhost:8080/myApp/redirect.do, the result page then should be contained in "myApp/jsp/result.jsp" [which is the same as Q1]
However, if u are accessing it with
http://localhost:8080/myApp/test1/redirect.do [assume u have another servlet mapping on another path], the container will then try to search if there is result.jsp under "myApp/test1/jsp/result.jsp"

3) For "/result.jsp", it should reside on "myApp/result.jsp"

4) Same as Q1

Hope u understand
 
sarang bharambe
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Niranjan,

There is no need to be frusted so much about this.

Here are the answers to your queries.

1) RequestDispatcher rd = request.getRequestDispatcher("/jsp/result.jsp");
The directory structure is :
myApp/jsp/result.jsp

2) RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
myApp/result.jsp

3) RequestDispatcher rd = request.getRequestDispatcher("/result.jsp");
myApp/result.jsp

4)RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/result.jsp");

myApp/jsp/result.jsp

I think the simplest way to remember it is that
Consider "/" as "myApp" Directory. So you can easily resolve the paths.
Also remember that View components are always put under the context root of your web app(or in a directory under the context root) and not under WEB-INF.

I hope this will help

regards,
Sarang Bharambe
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you call:

rd.forward(request, response);

???

If you do not call it you will get a blank page because nothing gets sent back to the browser.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

thnks very much to sarang for replying to mr queries. i will try the solutions provided here.

hey, marc, i think i m doing rd.forward(request,response);
thnks to u too
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m still seeing a blank page
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2) RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
myApp/result.jsp


Sarang,

As Kenny said, if there is no "/", the path where there the container would search for the jsp depends on the initial request. I couldnt think of a better example than what he gave.


Cheers,
Arvind
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the method in your servlet is even getting called.

Add a System.out.println("METHOD CALLED"); to verify if it is being called.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic