Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaServlet – Fetching Result

Servlet – Fetching Result

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 

HTML




<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Form</title>
</head>
<body>
<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>
</form>
 
</body>
</html>


Output:

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. 

Example: GFGServlet1.java 

Java




// Java Program to illustrate Fetching Result From a
// Servlets
 
// Importing required classes
import java.io.IOException;
import java.io.PrintWriter;
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
@WebServlet("/GFGServletFetchResult")
 
// 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("<html>");
        out.println("<head>");
        out.println("<title>Result Servlet</title>");
        out.println("<body>");
        out.println("<h1>Welcome " + name + "!</h1>");
        out.println("</body>");
        out.println("</head>");
        out.println("</html>");
    }
 
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
        throws ServletException, IOException
    {
 
        doGet(request, response);
    }
}


Output:

Output of GFGServletFetchResult.java

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments