We can get the RequestDispatcher object with the ServletContext simpley by calling getRequestDispatcher(String url) with the ServletContext object which finally returns the RequestDispatcher object. We have two possible options to dispatch the control after getting the object of RequestDispatcher by using include(req,res) and forward(req,res). 

We must mention the resource to which control has to be dispatched as a String parameter in getRequestDispather(String url) method  but should be start with " / " otherwise container throws IllegalArgumentException.

Why should be start the resource name with " / " ?
It is because we are getting RequestDispactcher object with ServletContext and which follows the context path. For any Servlet Application its context path is always web apps. So for the requested resource container always takes the path relative to the context. So its mandatory to mention " / " before writing url otherwise container will throw IllegalArgumentException.



If we are not writing " / " then container always search for the requested resource under the context path ( i.e web Apps ) which can never be possible because our all the resources are available under the web content folder.

A diagrammatic representation showing what is a context path


From the above directory structure let consider,
Current Resource - home.jsp
Requested Resource - login.jsp

Container always consider the relative path for the requested resource as context path in case of getting RequestDispatcher object with ServletContext that means dispatch must be with the context path. 
So here the context path is " /Application 1 ".

By writing " / " container understands that the requested resource is not in the current directory i.e here Application 1 , then search in the relevant directory either Web-content or src folder if request resource is a html/jsp or servlet respectively. 

To know about RequestDispatcher with Request object. 

Related Posts:

  • ServletContext Object in Servlet ServletContext is only a single object to a whole Servlet Application. It is once created and initialized by the web container at the container start-up. A Web application has many Servlet classes and for every servlet… Read More
  • What Happens at Container Start-Up in Servlet There are many task happening internally inside the Web-Server. Following are the list of task performed at the container start-up : 1. Reads the web.xml file or Annotations specified on various web components and stores al… Read More
  • Servlet Life Cycle Life Cycle of Servlet starts when a servlet application get deployed in the web server. Web container is responsible for calling all the life cycle methods. Servlet has following three life cycle methods : 1. init() 2. serv… Read More
  • What is a Web Container After developing Servlet Application we need to deploy and run our application at theWeb-Server. When a Web-server starts it internally starts a web-container which is also termed as servlet container.  What is a we… Read More
  • Introduction to Java Servlet Servlet is a web technology, which is used to develop web application. It is used to process the HTTP request by the client. Servlet was first introduced by the Sun MicroSystems with released of  version 1.0 in 1997. … Read More

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic