GeeCON Prague 2014*
The moose likes Servlets and the fly likes ServletRequest  RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletRequest  RequestDispatcher " Watch "ServletRequest  RequestDispatcher " New topic
Author

ServletRequest RequestDispatcher

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I got a servlet class and jsp page in the same package. [ Inside com.example package ]

I used RequestDispatcher in the servlet class to call the jsp page. But I am getting as page not found.


But if I place the jsp page at the root of the application and include a slash in getRequestDispatcher("/TestJspOne.jsp"); the page is called.

If I don't include the (slash) doesn't it considererd relative to the original request. and doesn't it supposed to look at the same logical location.

How to make this code work with relative path.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Do it like below.


Refer to the docs.

Doc says:
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.
[ September 26, 2005: Message edited by: Adeel Ansari ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by vishnu prakash:
I got a servlet class and jsp page in the same package. [ Inside com.example package ]


Your class files goes into
WEB-INF/classes/your-package-declaration..

Your jsp should be out-side WEB-INF. No matter at the root or under some directories.

So, don't put your jsp with your servlet.

Otherwise, you may do it like below. I have never try this, though.


[ September 26, 2005: Message edited by: Adeel Ansari ]
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Thanks Adeel Ansari.

I like to know the difference between RequestDispatcher in ServletRequest and ServletContext

In page 204 of Head First Servlet and Jsp book it says

Getting RequestDispatcher from ServletRequest

If the path does NOT start with forward slash,it is considered relative to the original request.


RequestDispatcher dispatch = request.getRequestDispatcher("TestJspOne.jsp");
dispatch.forward(request,response);

This is a relative path(because there is no forward slash) So in this case the container looks for "TestJspOne.jsp" in the same logical location the request is "in".


Doesn't this mean that I should put the jsp in the same servlet package. Kindly explain me "relative path"

How to use this RequestDispatcher without a (slash). If I use like this where should I place the JSP file.
Sachin Ramesh Vir
Greenhorn

Joined: Sep 15, 2005
Posts: 23
Hi,

I also want to know the differences detween relative path and absolute path. Please explain with some simple examples to understand the differences. And also why we are using "forward slash"(/). What is impiles if we using it?
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
Originally posted by vishnu prakash:
Thanks Adeel Ansari.

I like to know the difference between RequestDispatcher in ServletRequest and ServletContext

In page 204 of Head First Servlet and Jsp book it says

Getting RequestDispatcher from ServletRequest

If the path does NOT start with forward slash,it is considered relative to the original request.


RequestDispatcher dispatch = request.getRequestDispatcher("TestJspOne.jsp");
dispatch.forward(request,response);

This is a relative path(because there is no forward slash) So in this case the container looks for "TestJspOne.jsp" in the same logical location the request is "in".


Doesn't this mean that I should put the jsp in the same servlet package. Kindly explain me "relative path"

How to use this RequestDispatcher without a (slash). If I use like this where should I place the JSP file.


You could use a namedDispachter, but then the jsp has to have a mapping in the web.xml
I'd never used the requestDispatcher form the request. (no specific reason)


Win the opportunity to make money on the Internet<br /><a href="http://sweeps.sitesell.com/minirich.html" target="_blank" rel="nofollow">http://sweeps.sitesell.com/minirich.html</a>
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Relative path means it starts from the root of that particular web application

for example in Tomcat:
your application directorywebapp/my-app
your jsp page webapp/my-app/example.jsp
Relative path /example.jsp means my-app/example.jsp

Absolute path is like below
http://localhost:8080/my-app/example.jsp
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

Relative path example.jsp means my-app/example.jsp


There should be no forward slash for relative path I suppose.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Vishnu Prakash:
There should be no forward slash for relative path I suppose.


Dont suppose, check out the docs.

Doc says:

public RequestDispatcher getRequestDispatcher(String path)

The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.
[ October 27, 2005: Message edited by: Adeel Ansari ]
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
If there is no initial forward slash then it is a relative path.
ServletRequest.getRequestDispatcher("example.jsp");

If a slash is present then it is considered relative to the current context root.
ServletContext.getRequestDispatcher("/example.jsp");

I believe both these statements are correct.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Vishnu Prakash:
If there is no initial forward slash then it is a relative path.
ServletRequest.getRequestDispatcher("example.jsp");

If a slash is present then it is considered relative to the current context root.
ServletContext.getRequestDispatcher("/example.jsp");

I believe both these statements are correct.


Absolutely.
 
GeeCON Prague 2014
 
subject: ServletRequest RequestDispatcher