Thursday, November 24, 2011

What is Dispatcher-Servlet in Spring

When you work with Spring framework, you will find an xml file called dispatcher-servlet is created when we create a new project. If we use Maven to build the project we can define the name for this servlet instead of the default name "dispatcher-servlet.xml". Normally we give the project name as the prefix & keep the servlet part as it is. For example, if the project name is Navigator, then dispatcher servlet can be "navigater-servlet.xml". 
the purpose of the dispatcher is to receive the requests & map those requests to the correct resources such as models & views. that means when a request is received it should be handled to meet with the correct resources & output the results. therefore we define all the request mapping codes within the dispatcher servlet. It is an xml file which contains java beans. 
when we define the beans tag, we must include all relevant bean URLs within it in order to map the resources. A sample beans tag definition is as follows.
After defining the beans, we can define the resource mapping tags. For example, we can define view resolvers, annotation URLs or any other DAO resource URLs within the xml.
In order to map the requests the request mapping code must be included within the java controller pages. such mapping code can be shown as follows.
@RequestMapping("/enterdetails")
This @RequestMapping denotes that the current page is mapped with the dispatcher servlet. so if the mapping is correct, the request will be directed to this controller & controller will do the rest if the things.
Now you may have a clear idea on the dispatcher servlet. Stay tuned for the next post.




No comments:

Post a Comment