![]() |
VOOZH | about |
A Servlet is a server-side Java program used to handle client requests and generate dynamic responses. Fetching Result in Servlet refers to retrieving form data from the client using request.getParameter() and processing it on the server. The processed data is then sent back as an HTTP response, usually in HTML format.
Follow the given steps to fetch a result from Html form to your browser.
HTML form is used to take input from the user and send it to the servlet.
index.html
On form submission, data is passed in the request URL.
/GFGServletFetchResult?name=GeeksForGeeks
Servlet handles the request using doGet() or doPost() method.
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// working code}
Data is retrieved from the request object using getParameter().
String name = request.getParameter("name");
If numeric input is sent, convert it from String.
Servlet prepares output using PrintWriter.
PrintWriter out = response.getWriter();
out.println("<h1>Welcome " + name + "</h1>");
Final processed result is displayed in the client browser.
Output:
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.
GFGServlet1.java
Output:
Note: Kindly learn how to create a Servlet before moving on to fetching results.