answer – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 14 Dec 2020 15:44:22 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg answer – Programmerbay https://programmerbay.com 32 32 Create an application to add and multiply two numbers to the servlet to the user in apache tomcat server https://programmerbay.com/create-an-application-to-add-and-multiply-two-numbers-to-the-servlet-to-the-user-in-apache-tomcat-server/ https://programmerbay.com/create-an-application-to-add-and-multiply-two-numbers-to-the-servlet-to-the-user-in-apache-tomcat-server/#respond Sat, 07 Nov 2020 14:34:36 +0000 https://programmerbay.com/?p=5656 We are assuming that you have already downloaded and setup the Eclipse Environment and Tomcat server.

Here are the steps and the code to add and multiply two numbers to servlet in apache tomcat server

1) On Eclipse, create a new project and select Dynamic Web Project by ticking on producing deployment descriptor file.

2. Firstly hover on your newly created project, right-click on it and tab on new and select HTML file. ( or anything you want).

Index.html

<!DOCTYPE html>
<html>
<head>
<title>Practical Work</title>
</head>
<body>
<form action="add_me">
<label>First number </label> <input type="text" name="num1"/> <br/><br/>
<label>Second number </label> <input type="text" name="num2"/> <br/><br/>
<button type="submit" name="calculate">Product and Sum </button><br/>
</form>
</body>
</html>

3. Now, again hover on the project , right click on it and select new and tab on class that would create a java file.

Create a servlet class.

Add_Numbers.java

package com.programmerbay;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.*;
public class Add_Numbers extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException
{
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
int sum = num1 + num2;
int product = num1 * num2;
PrintWriter output = response.getWriter();
output.println("The Answer :"+sum +"\n The product :"+product);
}
}

4. Configure the Web.xml file which you find in Web Content folder.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

<servlet>
<servlet-name>Add</servlet-name>
<servlet-class>com.programmerbay.Add_Numbers</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Add</servlet-name>
<url-pattern>/add_me</url-pattern>

</servlet-mapping>
</web-app>

5. Open the previously created index.html and run it by selecting the “restart server”.

Output:

Servlet java code to add two numbers

Servlet java code to add two numbers 2

]]>
https://programmerbay.com/create-an-application-to-add-and-multiply-two-numbers-to-the-servlet-to-the-user-in-apache-tomcat-server/feed/ 0