[go: up one dir, main page]

0% found this document useful (0 votes)
6 views2 pages

Java p1

The document contains an HTML form for a simple calculator that allows users to input two numbers and select an arithmetic operation (addition, subtraction, multiplication, or division). The form submits data to a Java servlet (CalculatorServlet) that processes the input and returns the result of the calculation. The servlet handles the HTTP GET request, performs the selected operation, and displays the result on a new HTML page.

Uploaded by

igryukop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java p1

The document contains an HTML form for a simple calculator that allows users to input two numbers and select an arithmetic operation (addition, subtraction, multiplication, or division). The form submits data to a Java servlet (CalculatorServlet) that processes the input and returns the result of the calculation. The servlet handles the HTTP GET request, performs the selected operation, and displays the result on a new HTML page.

Uploaded by

igryukop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

practical no 1

index.html

<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="CalculatorServlet">
Enter First Number <input type="text" name ="txtN1" ><br>
Enter Second Number <input type="text" name ="txtN2" ><br>
Select Operation
<input type="radio" name="opr" value="+">ADDTION
<input type="radio" name="opr" value="-">SUBSTRACTION
<input type="radio" name="opr" value="*">MULTIPLY
<input type="radio" name="opr" value="/">DIVIDE <br>
<input type="reset">
<input type="submit" value="Calculate">
</form>
</body>
</html>

CalculatorServlet.java

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

public class NewServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
double n1 = Double.parseDouble(request.getParameter("txtN1"));
double n2 = Double.parseDouble(request.getParameter("txtN2"));
double result = 0;
String opr=request.getParameter("opr");
if(opr.equals("+")) result=n1+n2; if(opr.equals("-")) result=n1-n2;
if(opr.equals("*")) result=n1*n2;if(opr.equals("/")) result=n1/n2;
out.println("<h1> Result = "+result);
out.println("<h1>Servlet CalculateServlet at " +
request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

You might also like