0% found this document useful (0 votes)
4 views4 pages

Request Dispatcher

The document contains an HTML form for adding two numbers, which submits data to an AddServlet. The AddServlet processes the input, calculates the sum, and forwards the request to a ResultServlet that displays the result. The web application is configured with a web.xml file defining the servlets and their mappings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Request Dispatcher

The document contains an HTML form for adding two numbers, which submits data to an AddServlet. The AddServlet processes the input, calculates the sum, and forwards the request to a ResultServlet that displays the result. The web application is configured with a web.xml file defining the servlets and their mappings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<!

DOCTYPE html>

<html>

<head>

<title>Request Dispatcher Example</title>

</head>

<body>

<h2>Enter Two Numbers to Add</h2>

<form action="addServlet" method="POST">

<label>Number 1: </label>

<input type="text" name="num1"><br><br>

<label>Number 2: </label>

<input type="text" name="num2"><br><br>

<input type="submit" value="Add Numbers">

</form>

</body>

</html>

import java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

public class AddServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {

// Get the input numbers from the form

String num1 = request.getParameter("num1");

String num2 = request.getParameter("num2");

// Convert the inputs to integers

int number1 = Integer.parseInt(num1);

int number2 = Integer.parseInt(num2);

// Calculate the sum

int sum = number1 + number2;

// Set the sum as an attribute in the request

request.setAttribute("sum", sum);

// Forward the request to the ResultServlet or a JSP

RequestDispatcher dispatcher =
request.getRequestDispatcher("/resultServlet");

dispatcher.forward(request, response);

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class ResultServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

// Get the sum from the request attributes

int sum = (Integer) request.getAttribute("sum");

// Set the content type for the response

response.setContentType("text/html");

// Print the result on the webpage

PrintWriter out = response.getWriter();

out.println("<html><body>");

out.println("<h2>The sum is: " + sum + "</h2>");

out.println("</body></html>");

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>

<servlet-name>AddServlet</servlet-name>

<servlet-class>AddServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>AddServlet</servlet-name>

<url-pattern>/addServlet</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>ResultServlet</servlet-name>

<servlet-class>ResultServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ResultServlet</servlet-name>

<url-pattern>/resultServlet</url-pattern>

</servlet-mapping>

</web-app>

You might also like