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; charset=UTF-8">
<title>Servlet计算器</title>
</head>
<body bgcolor="#afb4db">
<%
String s1 = "";
Object ob = session.getAttribute("jg");
if(ob != null){
s1 = ob.toString();
}
session.invalidate();
%>
<form action="TestJSQ" method="post">
<table border="1" align="center">
<tr align="center"><td colspan="4">简易计算器</td></tr>
<tr>
<td colspan="4"><textarea name="text" id="tt" cols="22" rows="1"><%= s1 %></textarea></td></tr>
<tr align="center">
<td width="40" align="center"><input type="reset" value="C" style="width: 40px;"></td>
<td width="40"><input type="button" value="off" onclick="doselect(this)"></td>
<td width="40"><input type="button" value="+" onclick="doselect(this)" style="width: 40px;"></td>
<td width="40"><input type="button" value="←" onclick="doselect(this)" style="width: 40px;"></td></tr>
<tr align="center">
<td><input type="button" value="7" onclick="doselect(this)"></td>
<td><input type="button" value="8" onclick="doselect(this)"></td>
<td><input type="button" value="9" onclick="doselect(this)"></td>
<td><input type="button" value="-" onclick="doselect(this)" style="width: 40px;"></td></tr>
<tr align="center">
<td><input type="button" value="4" onclick="doselect(this)"></td>
<td><input type="button" value="5" onclick="doselect(this)"></td>
<td><input type="button" value="6" onclick="doselect(this)"></td>
<td><input type="button" value="x" onclick="doselect(this)" style="width: 40px;"></td></tr>
<tr align="center">
<td><input type="button" value="1" onclick="doselect(this)"></td>
<td><input type="button" value="2" onclick="doselect(this)"></td>
<td><input type="button" value="3" onclick="doselect(this)"></td>
<td><input type="button" value="/" onclick="doselect(this)" style="width: 40px;"></td></tr>
<tr align="center">
<td><input type="button" value="." onclick="doselect(this)"></td>
<td><input type="button" value="0" onclick="doselect(this)"></td>
<td><input type="submit" value="=" onclick="doselect(this)"></td>
<td><input type="button" value="%" onclick="doselect(this)" style="width: 40px;"></td></tr> </table>
</form>
</body>
</html>
<script language="javascript"> function doselect(bt){
var obj = document.getElementById('tt');
var s = bt.value;
obj.innerHTML += s;
if(s == "←"){
var a = obj.value;
s = a.substring(0, a.length-2);
obj.innerHTML = s;
}
if(s == "off"){
window.close();
}
}
</script>
Servlet后台处理:
package com.sp.web; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class Test_JSQ extends HttpServlet {
private static final long serialVersionUID = 1L; public Test_JSQ() {
super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String st = request.getParameter("text").trim(); int a = 0;
if (st.contains("+")) {
a = st.indexOf("+");
} else if (st.contains("-")) {
a = st.indexOf("-");
} else if (st.contains("x")) {
a = st.indexOf("x");
} else if (st.contains("/")) {
a = st.indexOf("/");
} else if (st.contains("%")) {
a = st.indexOf("%");
}
Double n1 = Double.parseDouble(st.substring(0, a));
Double n2 = Double.parseDouble(st.substring(a + 1, st.length() - 1));
String s = st.charAt(a) + ""; String rs = "";
if (s.equals("+")) {
rs = n1 + n2 + ""; } else if (s.equals("-")) {
rs = n1 - n2 + ""; } else if (s.equals("x")) {
rs = n1 * n2 + ""; } else if (s.equals("/")) {
if (n2 != 0.0) {
rs = n1 / n2 +""; } else {
rs = "error by 0";
}
} else if (s.equals("%")) {
rs = n1 % n2 + ""; }
request.getSession().setAttribute("jg", rs);
response.sendRedirect("jisuanqi.jsp");
;
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} }
界面展示: 当除数=0 时:
关键问题在点击各个按键的时候,同时在文本域中显示想要输入的内容,当点击“←”的时候 输入的内容会自动往前退一格。
还有不知道什么原因,在计算加减乘模的时候没有问题但是当运算除法的时候总是会报数据格式错误,所以把“÷” 改成“/”之后就莫名其妙的解决了