Wednesday, May 8, 2013

RequestDispatcher and it's working with Relative,Absolute path

A RequestDispatcher is an object which receives the request from the client and sends (forward or include) the request to any resource ( a servlet, jsp or html).

RequestDispatcher is an interface available as part of javax.servlet package. A servlet container will creates the RequestDispatcher object.

As RequestDispatcher is an interface, it can't directly create the object . Container will creates the object to it's (RequestDispatcher) implementation class. 

The class name will differ from server to server as every server should implement servlet api they chose proprietay names to the implementation classes.

Incase of Tomcat the implementation class to RequestDispatcher is ApplicationDispatcher . you can get more information from here.

Once we got RequestDispatcher object we can perform two operations.

forward (request, response) and
include (request, response).

Getting RequestDispatcher object:

There are mainly three ways to get the RequestDispatcher object created by the servlet container.

1. Using ServletContext.getRequestDispatcher("/path"):

Once we have ServletContext object we can get the RequestDispatcher object by using context.getrequestDispatcher("/path"). where path is the absolute path of the resource to which we want to send the request.

Here is the example.

RequestDServlet is the one which will send request to another resource.


in above servlet,' /requestR ' is the absolute path of the resource (RequestRServlet) to which request has to be sent.

forward(request, response) will through ServletException,IOException. That's why we have placed that code in try catch block.

RequestRServlet is the servlet which will receive request from RequestDServlet with the help of RequestDispatcher.


Configure both servlets in web.xml


If we run the application , we will get output showing text "Request Dispatcher Dispacthed the REQUEST" as the request is forwarded to RequestRServlet.

We can perform include() operation too , will be discussed in next way of getting RequestDispatcher.

2. ServletContext.getNamedDispatcher("name"):

This is another way of getting RequestDispatcher if we have ServletContext object.

The only change we need to do is in RequestDServlet .


getNamedDispatcher("name") will accept name of resource to which request has to be sent.

include(request, response) will include the contents of both resources and display to the client.

3. ServletRequest.getRequestDispatcher("path"):

If we have a request object, we can get the RequestDispatcher object from
request.getRequestDispatcher("path"), where path refers to absolute or relative path of the resource to which request has to be sent.

Again, the change only we need to do in RequestDServlet.


Here, two types of paths we can give to the request.getRequestDispatcher().
If we give '/path' it will be considered as absolute path and if we mention "path" it will be treated as relative path.

Absolute path and Relative path :

Absolute path is the exact path showing the location of resource where it is available.

example:' /ServletApp/requestR ' or ' /requestR'

Server will get's the context path (/ServletApp) and appends the path (/requestR) to it.

Relative path is the path relative to the current path of the resource.

example: ' requestR '

Server will get's the current path of the resource executing, i.e,(/ServletApp/requestD) and get's the context path from that (/ServletApp/) by removing resource path (requestD) . Finally it will append the resource to which request has to be sent (requestR).

Note:

We can use any resource (html, jsp, servlet) to forward or include the request with RequestDispatcher.



16 comments:


  1. After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
    Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in



    selenium training in Bangalore
    selenium training in Marathahalli
    selenium training in Btm layout
    selenium training in Jaya nagar
    selenium training in Electronic city
    selenium training in Kalyan nagar



    ReplyDelete
  2. This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot. 
    Python Online training
    python Course institute in Chennai
    Python Course institute in Bangalore

    ReplyDelete
  3. Wonderful to visit your websites, This sites are fully filled with full of Information's. I perceived a lots of knowledge from this Articles.

    For more to learn about Python Visit below
    python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

    ReplyDelete