a(+;-;*;/)b-----demo----bai
页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript" src="js/jquery.js">
</script>
<script>
$(document).ready(
function()
{
//1 按钮关联事件
$("#btn").click(
function ()
{
//构造ajax参数
var paras = "a="+$("#a").val()+"&b="
+$("#b").val()+"&op="+$("#op").val(); //发起ajax请求
$.ajax(
{
type:"get",
url:"CalculatorServlet",
data:paras,
dataType:"json",
cache:false,
success:function(c)
{
var info = "";
//使用增强for,遍历json对象的所有属性
if(c.a!=undefined)
{
alert(c.a+c.show_op+c.b+"="+c.result)
}
else
{
alert("错误编号:"+c.errcode+",错误信息:"+c.errmsg);
}
}
}
);
} );
} );
</script>
<body>
请输入a值:<input id="a" value="1"/>
<select id="op">
<option value="add">+</option>
<option value="sub">-</option>
<option value="multi">*</option>
<option value="div">/</option> </select>
请输入b值:<input id="b" value="2"/>
<input id="btn" type="button" value="获得结果"/>
</body>
</html>
控制器: 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 com.google.gson.Gson; public class CalculatorServlet extends HttpServlet { /**
* Constructor of the object.
*/
public CalculatorServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //1 中文处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Gson gson = new Gson();
//2 读取入参
int a = -1;
int b = -1;
try {
a = Integer.parseInt(request.getParameter("a"));
b = Integer.parseInt(request.getParameter("b"));
} catch (Exception e)
{
//创建1个错误消息对象,用json写回。方法提前终止。
Msg m = new Msg(1, "输入的数字格式有误!");
String json = gson.toJson(m);
response.getWriter().print(json);
response.getWriter().flush();
response.getWriter().close();
return;
}
String op = request.getParameter("op");
//3 创建calculator对象
Calculator c = new Calculator(a, b, op);
c.cal();//执行计算,算出结果
//4 转成json字符串返回
String json = gson.toJson(c);
response.getWriter().print(json);
response.getWriter().flush();
response.getWriter().close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
//用于提示错误信息的类
public class Msg
{
private int errcode;
private String errmsg;
public int getErrcode() {
return errcode;
}
public void setErrcode(int errcode) {
this.errcode = errcode;
}
public Msg(int errcode, String errMsg) {
super();
this.errcode = errcode;
errmsg = errMsg;
} }
public class Calculator
{
private int a;
private String op;
private int b;
private String show_op;
private int result; public Calculator(int a, int b, String op) {
super();
this.a = a;
this.b = b;
this.op = op;
}
public String getOp() {
return op;
}
public void setOp(String op) {
this.op = op;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
//执行计算,算出result
public void cal()
{
if("add".equals(op))
{
result = a+b;
show_op = "+";
}
else if("sub".equals(op))
{
result = a-b;
show_op = "-";
}
else if("multi".equals(op))
{
result = a*b;
show_op = "*";
}
else if("div".equals(op))
{
result = a/b;
show_op = "/";
}
} }
a(+;-;*;/)b-----demo----bai的更多相关文章
- 二分法(折半查找法)小demo
使用此算法,必须有一个前提,那就是数组必须是有序的. package com.ly.tcwireless.international.test; import org.junit.Test; publ ...
- 【iM_TFTRGB液晶模块】demo例程(版本1.02)发布
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- 【Springboot】实例讲解Springboot整合OpenTracing分布式链路追踪系统(Jaeger和Zipkin)
1 分布式追踪系统 随着大量公司把单体应用重构为微服务,对于运维人员的责任就更加重大了.架构更复杂.应用更多,要从中快速诊断出问题.找到性能瓶颈,并不是一件容易的事.因此,也随着诞生了一系列面向Dev ...
- 20191127 Spring Boot官方文档学习(9.1-9.3)
9."使用方法"指南 9.1.Spring Boot应用程序 9.1.1.创建自己的FailureAnalyzer FailureAnalyzer被包装在FailureAnalys ...
- django笔记(python web框架)
1.Python 下载地址:https://www.python.org/downloads/ 2.Django 下载地址:https://www.djangoproject.com/download ...
- python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2) #(上限,下限,步长) 可以实现对元素或者下标的 ...
- python 学习笔记1(序列;if/for/while;函数;类)
本系列为一个博客的学习笔记,一部分为我原创. 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 1. print 可以打印 有时需要 ...
- eclipse-SDK-3.7-win32;eclipse-java-indigo-win32;eclipse-jee-indigo-win32 区别(ZZ)
eclipse-SDK-3.7-win32:eclipse-java-indigo-win32:eclipse-jee-indigo-win32 三个都是用于win32,即windows系统的32位机 ...
- 深入剖析虚拟DOM提升性能(Vue,React);
I.原始渲染方式(直接操作DOM): 1.state数据: 2.JSX模板: 3.数据 + 模板 相结合,生成真实的DOM来显示: 4.state发生改变: 5.数据 + 模板结合,生成真实的DOM来 ...
- 信1705-2 软工作业最大重复词查询思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词)数组中。 (2)定义一个Map,key是字符串类型,保存单词;value是数字类型,保存该单词出现的次数。 (3)遍历(1)中得到的字符串数组,对于每一个单词,考察Map的key中是否出现过该单词,如果没出现过,map中增加一个元素,key为该单词,value为1(
通过学习学会了文本的访问,了解一点哈希表用途.经过网上查找做成了下面查询文章重复词的JAVA程序. 1 思 思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词 ...
随机推荐
- MySql数据库的主从配置
主服务器 192.168.7.182 Centos6.5 MYSQL5.6.10 从服务器 192.168.112.7 Centos6.5 MYSQL5.6.10 主服务器配置(192.168.7.1 ...
- 【P2964】硬币的游戏(DP+前缀和)
一道DP,思维难度真是不小. 首先对于这个题的数据,我们可以发现差不多可以支持n^2logn,但是貌似也不会有这种复杂度的线性DP(至少这个题看上去不是这样).所以我们考虑N^2做法.因为求得是价值和 ...
- QT 多页面切换之QTabWidget
//mydialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class QTabWidget; class ...
- Codeforces Round #372 (Div. 2) A ,B ,C 水,水,公式
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- numpy里*与dot与multiply
一.* , dot() multiply() 1, 对于array来说,(* 和 dot()运算不同, * 和 multiply()运算相同) *和multiply() 是每个元素对应相乘 do ...
- java:管道流(线程间管道流)
class Send implements Runnable{ PipedOutputStream pos = null; public Send() { this.pos = new PipedOu ...
- hdu 5243 Homework
Homework Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- RK30SDK系统重启源码分析
Linux系统重启的最底层函数是arch_reset,这是一个全局的函数指针变量,定义在 arch/arm/mach-rk30/include/mach/system.h中: extern void ...
- 教你们在cmd里运行打开游戏,效率很快的。喜欢吧?
第一步安装好的游戏 环境变量 变量值:把刚才复制好粘贴在里面,前面不用删除. 喜欢吧?这招非常好用.
- selenium-webdirver api-定位方式
1,8种单数定位方式 # 通过ID定位目标元素 driver.find_element_by_id('i1') # 通过className定位目标元素 driver.find_element_by_c ...