Servlet call from URL.

One of the most simple and yet having a number of configurations thing is a scratch Servlet call from a request URL or from REST or through a web service.  Sure, we all can copy-paste from existing code, such configurations whenever required, but knowing saves half of your time.

This blog cum tutorial will show how to call a servlet from Request URL.

Pre-requisites :

  1. Basics of JAVA
  2. Basics of Spring MVC.
  3. Knowledge of  Request URL, REST CLient, Web service.

Knowing this , i am going to explain this call with a very simple example.

Use case scenario of example : If my Request URL has parameter as “true”, Servlet will print a message in output that “I have hit for TRUE” and if it has parameter “false” , it will print “I have hit for FALSE”

Reason for taking such a simple use case is: I do not intend that the reader should take the pain to understand the use case when the intention of this tutorial is to understand “HOW ” a successful call will be made.

Let us begin. The following steps are divided majorly on the basis of the files which are created / modified.

Step 1 :  THE Servlet :Capture

@RequestMapping has been used to map various variables. These will be used while creating URL in step 4. @ResponseBody is being used to send response back. Here, we are sending back a DTO named “TestDTOForController” which looks like this :


NOTE : No bean definition in spring.xml is required of this controller.

Step 2: dispatcher-servlet.xml:

ADD the following file as dispatcher-servlet.xml in your web/webroot/WEB-INF directory :


This file mainly tells which base package needs to be scanned for annotated controllers.

Step 3: web.xml code changes

Add the following code snippet in the web.xml in web/webroot/WEB-INF folder :


This mainly tells where to find the dispatcher we created in step 2, what will be the URL pattern. As you can see we have kept the URL pattern to be the curent working folder of servlet file.

Step 4 : URL creation :

The URL that you will use to hit this servlet depends on following factors :

  1. What environment you are hitting : Enter the URL of the environment you wnat to hit. For local, you may use : localhost:9001
  2. The extension which has the servlet : The extension/the base package in which you have the entire folder structure . In our case we created it in commercecore.
  3. The URL pattern which we added in web.xml . In our case /rest
  4. The mapping which we use to identify which controller to call . In our case : /testMapping
  5. The parameters you want to pass : In our case either true or false .

This makes our URL as :


And below is how the Output will look like :


You may create a POST call as well by changing the request method in Servlet and passing some data as input in request body .

Hope this will help in creating a servlet call next time onwards.


EDIT 1 : The example shown above is using Spring style controller call. This style comes from spring, one of the many competing frameworks which have been made above java. In pure java. A servlet is called by extending the HttpServlet class and overriding doGet, doPost etc. Frameworks like spring just make this call easier adn with more enhancements like ability to use annotation mapping, ability to use autowired services , just to name a few.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s