请告诉我如何获得输出。我无法获得所需的结果。
我想在点击提交按钮时只需要一次操作。
告诉我,我只想使用servlet创建一个简单的caclculator。
<!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>
<form action="MyServlet" method="get">
<p>Select Operation:</p>
<p>
<select name="expression">
<option value=1>Multiply
<option value=2>Divide
<option value=3>Add
<option value=4>Subtract
</select>
</p>
<p>First number:</p>
<p><input name="firstNumber" size=10></p>
<p>Second number:</p>
<p><input name="secondNumber" size=10></p>
<p><input type="submit" nvalue="submit"></p>
</form>
</body>
</html>
Myservlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html"); // informing the client that which format of data/response will be send
PrintWriter out = response.getWriter();
int a1 = Integer.parseInt(request.getParameter("firstNumber"));
int a2 = Integer.parseInt(request.getParameter("secondNumber"));
int expr1 = Integer.parseInt(request.getParameter("expression"));
//Invoke the relevant method of the enterprise bean
switch (expr1) {
case 1:
out.println("<h3> Multiply= </h3>" + (a1 * a2));
break;
case 2:
out.println("<h3> Divide= </h3>" + (a1 / a2));
break;
case 3:
out.println("<h3> Addition= </h3>" + (a1 + a2));
break;
case 4:
out.println("<h3> Subtraction= </h3>" + (a1 - a2));
break;
}
}
}