No Widget Added

Please add some widget in Offcanvs Sidebar

Shopping cart

shape
shape

Connect Two Servlet with sendRedirect

  • Home
  • Java
  • Connect Two Servlet with sendRedirect
advanced java, sendredirect in servlet , send redirect in servlet, skills101, best java classes, java full stack development, java full course, java in hindi

Lets try to create one html page and retrieve data to a servlet and add those data and send this data to another servlet to print.

Using sendRedirect involves a slightly different approach. Instead of forwarding the request along with the attributes, the first servlet will send a redirect response to the client, including the result as a query parameter. The second servlet will then read this parameter from the URL.

Step 1: Create the HTML Form

Create an HTML file named input.html.

<!DOCTYPE html>
<html>
<head>
    <title>Number Addition</title>
</head>
<body>
    <h2>Add Two Numbers</h2>
    <form action="add" method="post">
        <label for="number1">Number 1:</label>
        <input type="text" id="number1" name="number1"><br><br>
        <label for="number2">Number 2:</label>
        <input type="text" id="number2" name="number2"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Step 2: Create the First Servlet (AddServlet)

Create a new Java class named AddServlet.java. This servlet will retrieve the numbers from the form, add them, and then send a redirect to the second servlet with the sum included as a query parameter.

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/add")
public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Retrieve data from the form
        int number1 = Integer.parseInt(request.getParameter("number1"));
        int number2 = Integer.parseInt(request.getParameter("number2"));
        
        // Calculate the sum
        int sum = number1 + number2;

        // Redirect to the ResultServlet with the sum as a query parameter
        response.sendRedirect("result?sum=" + sum);
    }
}

Step 3: Create the Second Servlet (ResultServlet)

Create a new Java class named ResultServlet.java. This servlet will read the sum from the query parameter and display it.

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/result")
public class ResultServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Retrieve the sum from the query parameter
        int sum = Integer.parseInt(request.getParameter("sum"));

        // Display the result
        response.setContentType("text/html");
        response.getWriter().println("<h1>Result</h1>");
        response.getWriter().println("<p>The sum is: " + sum + "</p>");
    }
}

Explanation

  1. HTML Form (input.html):
    • Collects two numbers from the user and submits the form data to the /add URL using a POST request.
  2. AddServlet (AddServlet.java):
    • Mapped to /add.
    • Retrieves the two numbers from the form, calculates their sum, and then redirects the client to the /result URL with the sum included as a query parameter.
  3. ResultServlet (ResultServlet.java):
    • Mapped to /result.
    • Reads the sum from the query parameter and displays it.

Step 4: Deploy Your Application

  1. Place input.html in the web directory (e.g., webapps/your_project_name if using Tomcat).
  2. Compile and deploy AddServlet.java and ResultServlet.java as part of your web application.
  3. Start your server (Tomcat) and navigate to http://localhost:8080/your_project_name/input.html in your web browser.

When you fill out the form and click “Submit,” the AddServlet will handle the request, add the numbers, and send a redirect to the ResultServlet with the sum included in the URL. The ResultServlet will then display the sum.

Leave A Comment

Your email address will not be published. Required fields are marked *