Simple code to display addition and product of two numbers using JSP.
1) Create a landing page in HTML.
Index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Practical Work</title> </head> <body> <form action="add_multiply.jsp"> <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>
2) Make a module in JSP that will print the sum and product of two numbers
add_multiply.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% int num1 = Integer.parseInt(request.getParameter("num1")); int num2 = Integer.parseInt(request.getParameter("num2")); int sum = num1 + num2; int product = num1 * num2; out.println("This is the sum = "+sum+"\n This is the product = "+product); %> </body>
Output:
This post was last modified on July 4, 2022