The following example shows how to Find Sum of Two Numbers in JSP. Basically, here we use HTML input fields to accept the numbers from the user. Therefore, in the code, first we check whether the input fields have values or not. In case, user has entered the values and clicks on the Find Sum button, we call the find_sum() method to compute the sum and display it on the same page.

Program to Find Sum of Two Numbers in JSP

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="f1" method="post" action="2.jsp">
	Enter First Number: <input type="text" name="n1"/><br/>
	Enter Second Number: <input type="text" name="n2"/><br/>
	
	<br/>
	<input type ="submit" value="Find Sum"/>
</form>

<%!
	int find_sum(int p, int q)
	{
		return p+q;
	}
%>
<%
	String inp1=request.getParameter("n1");
	String inp2=request.getParameter("n2");

	if((inp1!=null) && (inp2!=null)
)
	{
		int px=Integer.parseInt(inp1);
		int py=Integer.parseInt(inp2);

		int sum=find_sum(px, py);
		out.println("<br/>Sum = " + sum);
	}
%>
</body>
</html>

Output

Output of the Program to Find Sum of Two Numbers in JSP
Output of the Program to Find Sum of Two Numbers in JSP

Once you click on the Find Sum button, you will get following result.

Find Sum in JSP
Find Sum in JSP