Skip to content
Related Articles

Related Articles

Servlet – Fetching Result

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 17 Feb, 2022

Servlet is a simple java program that runs on the server and is capable to handle requests from the client and generate dynamic responses for the client.

How to Fetch a Result in Servlet?

 It is depicted below stepwise as shown below as follows:

  1. You can fetch a result of an HTML form inside a Servlet using request object‘s getParameter() method.
  2. request.getParameter() takes the name of the corresponding input field of HTML form (whose value we want to fetch) as the parameter. This method will then fetch/retrieve the value passed in that input field.
  3. Assign the value fetched from request.getParameter to a String variable because this method returns a String value.
  4. Note: If you submit an integer or float value through the HTML form and want to retrieve it in the same datatype, then you will have to typecast the value while/after retrieving it through request.getParameter(), as this method returns only a String value.

In order to display the result through a Servlet, print the String variable which has stored the value retrieved from request.getParameter()

The steps are as follows: 

  1. Create an HTML form with an appropriate method. Be mindful of the method you use in the HTML form, whether get or post.
  2. Create a Servlet.
  3. Inside the Servlet, write the logic inside doGet() or doPost() method accordingly.

Example: index.html 


<!DOCTYPE html>
<meta charset="ISO-8859-1">
<form action="GFGServletFetchResult" style="font-size: 25px;">
Enter your name: <input type="text" name="name" placeholder="Enter your name here"><br>
<button type="submit">Submit and go to Servlet</button>


Output of index.html

Note: In the following code, logic is written in doGet() method because, in the (above) HTML form, the method is “get” by default as the method is not explicitly mentioned as the post in the HTML form. 



// Java Program to illustrate Fetching Result From a
// Servlets
// Importing required classes
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Annotation
// Main class
// Extending HttpServlet class
public class GFGServletFetchResult extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        out.println("<!DOCTYPE html>");
        out.println("<title>Result Servlet</title>");
        out.println("<h1>Welcome " + name + "!</h1>");
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
        throws ServletException, IOException
        doGet(request, response);


Output of

Note: Kindly learn how to create a Servlet before moving on to fetching results.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!