本文通过servlet和jsp分别实现计算器的加减乘除运算,下面直接演示代码:
1、servlet实现简单的计算器
1)、CalBean.java 计算器运算的bean封装类
package example.bean.learn;
public class CalBean {
private double firstParam;
private double secondParam;
private String operator;
private double result;
public double getFirstParam() {
return firstParam;
}
public void setFirstParam(double firstParam) {
this.firstParam = firstParam;
}
public double getSecondParam() {
return secondParam;
}
public void setSecondParam(double secondParam) {
this.secondParam = secondParam;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public double calculate() {
result = 0;
try {
if ("+".equals(operator))
result = firstParam + secondParam;
if ("-".equals(operator))
result = firstParam - secondParam;
if ("*".equals(operator))
result = firstParam * secondParam;
if ("/".equals(operator) && (0 != secondParam))
result = firstParam / secondParam;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//判断输入的是不是数字
public boolean isNum(String str) {
return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
}
}
package example.calculate.learn;
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;
import example.bean.learn.CalBean;
/**
* Servlet implementation class Calculate
*/
public class Calculate extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @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");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String firstParam = request.getParameter("firstParam");
String secondParam = request.getParameter("secondParam");
String operator = request.getParameter("operator");
CalBean cal = new CalBean();
if (firstParam != null && cal.isNum(firstParam) && secondParam != null
&& cal.isNum(secondParam)) {
if (!firstParam.isEmpty()) {
cal.setFirstParam(Double.valueOf(firstParam));
}
if (!secondParam.isEmpty()) {
cal.setSecondParam(Double.valueOf(secondParam));
}
cal.setOperator(operator);
if (cal.getOperator().equals("/") && cal.getSecondParam() == 0.0) {
out.println("<html><head>");
out.println("<title>" + "简易计算器" + "</title>");
out.println("</head>");
out.println("<body>");
out.println("输入有误,除数不能为零" + "<br>");
out.println("<a href='index.html'>返回</a>");
out.println("</body>");
out.println("</html>");
} else {
double result = cal.calculate();
// request.setAttribute("result", result);
// request.setAttribute("firstParam", firstParam);
// request.setAttribute("secondParam", secondParam);
// request.setAttribute("operator", operator);
//
// RequestDispatcher dispatcher = request
// .getRequestDispatcher("index.jsp");
// dispatcher.forward(request, response);
out.println("<html><head>");
out.println("<title>" + "简易计算器" + "</title>");
out.println("</head>");
out.println("<body>");
out.println(firstParam + " " + operator + " " + secondParam
+ "= " + result + "<br>");
out.println("<a href='index.html'>返回</a>");
out.println("</body>");
out.println("</html>");
}
} else {
out.println("<html><head>");
out.println("<title>" + "简易计算器" + "</title>");
out.println("</head>");
out.println("<body>");
out.println("输入有误,无法进行计算,请输入数字且除数不能为零" + "<br>");
out.println("<a href='index.html'>返回</a>");
out.println("</body>");
out.println("</html>");
}
}
}
3)、index.html 计算器界面
<!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=utf-8">
<title>简易计算器</title>
</head>
<body>
<form action="Calculate" method="post">
<input type="text" name="firstParam" value="">
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select> <input type="text" name="secondParam" value="">=? <input
type="submit" value="计算">
</form>
</body>
</html>
4)、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>10.1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Calculate</servlet-name>
<servlet-class>example.calculate.learn.Calculate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Calculate</servlet-name>
<url-pattern>/Calculate</url-pattern>
</servlet-mapping>
</web-app>
1)、cal.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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; utf-8">
<title>简单加速器</title>
</head>
<body>
<jsp:useBean id="calBean" class="example.bean.learn.CalBean"></jsp:useBean>
<jsp:setProperty property="*" name="calBean"/>
<%
calBean.calculate();
%>
<form action="cal.jsp" method="post">
<input type="text" name="firstParam" >
<select
name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="secondParam" />
=<button type="submit">计算</button>
</form><br>
计算结果是:
<jsp:getProperty property="firstParam" name="calBean"/>
<jsp:getProperty property="operator" name="calBean"/>
<jsp:getProperty property="secondParam" name="calBean"/>
=<jsp:getProperty property="result" name="calBean"/>
</body>
</html>