Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletRequest RequestDispatcher

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


There should be no forward slash for relative path I suppose.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic