使用jsp+javaBean写一个网页计算器

在学习jsp时写的练手的代码。

运行效果:

calculator.jsp代码:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'calculator.jsp' starting page</title>
   
   <style>
   
	   body{
		text-align : center;
	   }
		body table tr td input{
			width:100%;
			height : 100%;
		}
		.container{
			align:center;
		}
   </style>
   
  </head>
  
  <body >
  	<div>
	  	<jsp:useBean id="calculatorBean" class = "cn.Lionel.domain.CalculatorBean"></jsp:useBean>
	  	
	  	<jsp:setProperty property="*" name="calculatorBean"/>
	  	
	  	<%
	  		calculatorBean.calculate();
	  	 %>
	  	<hr>
	  		計算結果是:
	  		<jsp:getProperty property="firstNum" name="calculatorBean"/>
	  		<jsp:getProperty property="oper" name="calculatorBean"/>
	  		<jsp:getProperty property="secondNum" name="calculatorBean"/>
	  		=
	  		<jsp:getProperty property="result" name="calculatorBean"/>
	  	<hr>
		  <form action = "/day09/calculator.jsp" method = "get">
		    <table style="margin-right: auto;margin-left: auto;" width = "40%" border="1">
		    	
		    	<tr>
		    		<td colspan="2">简单计算器</td>
		    	</tr>
		    
		    	<tr>
		    		<td>第一个参数:</td>
		    		<td><input type="text" name = "firstNum"> </td>
		    	</tr>
		    	
		    	<tr>
		    		<td>运算符</td>
		    		<td>
		    			<select name = "oper">
		    				<option value = "+">+</option>
		    				<option value = "-">-</option>
		    				<option value = "*">*</option>
		    				<option value = "/">/</option>
		    			</select>
		    		</td>
		    	</tr>
		    	
		    	<tr>
		    		<td>第二个参数</td>
		    		<td><input type="text" name = "secondNum"></td>
		    		
		    	</tr>
		    	
		    	<tr>
		    		<td colspan = 2 ><input type= "submit" value = "提交" name = "submit" style = "text-align:center"></td>
		    	</tr>
		    </table>
		   </form>
	  </div>
  </body>
</html>


CalculatorBean.java代码:

package cn.Lionel.domain;

import java.math.BigDecimal;

//封装计算器数据的Bean 	
public class CalculatorBean {

	private String firstNum = "0" ;
	private String secondNum = "0";
	private char oper = '+';
	private String result = "0";
	public String getFirstNum() {
		return firstNum;
	}
	public void setFirstNum(String firstNum) {
		this.firstNum = firstNum;
	}
	public String getSecondNum() {
		return secondNum;
	}
	public void setSecondNum(String secondNum) {
		this.secondNum = secondNum;
	}
	public char getOper() {
		return oper;
	}
	public void setOper(char oper) {
		this.oper = oper;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
	public void calculate(){
		
		BigDecimal first = new BigDecimal(this.firstNum);
		BigDecimal second = new BigDecimal(this.secondNum) ;
		
		switch(this.oper){
		case '+':
			this.result = first.add(second).toString();
			break;
		case '-':
			this.result = first.subtract(second).toString();
			break;
		case '*':
			this.result = first.multiply(second).toString();
			break;
		case '/':
			this.result = first.divide(second,20,BigDecimal.ROUND_HALF_UP).toString();
			break;
		default : break;
		}
		
	}
	
}


没有什么好讲解的,都是一些入门的知识。

你可能感兴趣的:(html,jsp,代码,javabean)