JSP Development Mode 2 (calculator)

Source: Internet
Author: User

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>
&LT;TD colspan= "2" > <input type= "submit" value= "results" ></td>
</tr>
</table>
</form>
</body>

JSP Development Mode 2 (calculator)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.