Calculatorbean————————————————————————————————————————————
public class Calculatorbean {
Private String firstnum;
Private String sencondnum;
Private String operator;
Private Double result=0.0;
Public Double GetResult () {
Return result;
}
public void Setresult (Double Result) {
This.result = result;
}
Public String getfirstnum () {
Return firstnum;
}
public void Setfirstnum (String Firstnum) {
This.firstnum = firstnum;
}
Public String getsencondnum () {
Return sencondnum;
}
public void Setsencondnum (String Sencondnum) {
This.sencondnum = sencondnum;
}
Public String getoperator () {
Return operator;
}
public void Setoperator (String Operator) {
This.operator = operator;
}
public void calculate () {
Double fisrt =double.valueof (firstnum);
Double Sencond =double.valueof (sencondnum);
If ("+". equals (operator)) {
this.result=fisrt+sencond;
}else if ("-". equals (operator)) {
this.result=fisrt-sencond;
}else if ("*". equals (operator)) {
this.result=fisrt*sencond;
}else if ("/". equals (operator)) {
this.result=fisrt/sencond;
}
}
}
——————————————————————————————————————————————————————
Import java.io.IOException;
Import javax.servlet.ServletException;
Import javax.servlet.annotation.WebServlet;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import com.lovo.javaBean.CalculatorBean;
/**
* Servlet Implementation Class Calculatorservlet
*/
@WebServlet ("/calculatorservlet.do")
public class Calculatorservlet extends HttpServlet {
Private static final Long Serialversionuid = 1L;
/**
* @see Httpservlet#httpservlet ()
*/
Public Calculatorservlet () {
Super ();
TODO auto-generated Constructor stub
}
/**
* @see httpservlet#doget (httpservletrequest request, httpservletresponse Response)
*/
protected void doget (httpservletrequest request, httpservletresponse Response) throws servletexception, IOException {
DoPost (request, response);
}
/**
* @see httpservlet#dopost (httpservletrequest request, httpservletresponse Response)
*/
protected void DoPost (httpservletrequest request, httpservletresponse Response) throws servletexception, IOException {
Request.setcharacterencoding ("UTF-8");
Get form data
String Num1=request.getparameter ("num1");
String Num2=request.getparameter ("num2");
String zifu= Request.getparameter ("zifu");
Calculatorbean calculatorbean= New Calculatorbean ();
Calculatorbean.setfirstnum (num1);
Calculatorbean.setsencondnum (num2);
Calculatorbean.setoperator (zifu);
Calculatorbean.calculate ();
To store a JavaBean object in a Session object
HttpSession session = Request.getsession ();
Session.setattribute ("calculatorbean", calculatorbean);
Control page Forwarding
Request.getrequestdispatcher ("newcal.jsp"). forward (request, response);
}
}
————————————————————————————————————————————————————
<% @page import= "com.lovo.javaBean.CalculatorBean"%>
<%@ page language= "java" contenttype= "text/html;charset=utf-8"%>
<meta charset= "utf-8" >
<title> Calculators </title>
<body>
<%
Calculatorbean Calculatorbean = (calculatorbean) Session.getattribute ("calculatorbean");
If (calculatorbean!=null) {
Out.println (calculatorbean.getfirstnum () +calculatorbean.getoperator () +calculatorbean.getsencondnum () + "=" + Calculatorbean.getresult ());
}
%>
<form action= "calculatorservlet.do" method= "post" >
<table border = 1 align= "center" >
<tr>
<td> operand 1:<input type= "text" name= "num1" ></td>
</tr>
<tr>
<td> characters:
<select name= "zifu" id= "" >
<option value= "+" >+</option>
<option value= "-" >-</option>
<option value= "*" >*</option>
<option value= "/" >/</option>
</select>
</td>
</tr>
<tr>
<td> operand 2:<input type= "text" name= "num2" ></td>
</tr>
<tr>
<TD colspan= "2" > <input type= "submit" value= "results" ></td>
</tr>
</table>
</form>
</body>
JSP Development Mode 2 (calculator)