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)后,存储到一个字符串(单词 ...
随机推荐
- 【转】服务器.htaccess 详解以及 .htaccess 参数说明
htaccess文件(或者”分布式配置文件”)提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到限 ...
- COUNT(DISTINCT a.TransportOrderID)的用法
DECLARE @StartDate DATETIME= '2017-12-20 00:00:00';DECLARE @EndDate DATETIME= '2017-12-26 00:00:00'; ...
- Qt中使用setStyleSheet对QPushButton按钮进行外观设置
Qt中使用setStyleSheet对按钮进行外观设置 字体颜色的设置一般时以下两种方案: (1)属于QWidget子类的一些控件 可以直接使用样式表,例如label->setStyleShee ...
- QT 使用QPainter 绘制图形 和 世界变换 world transform
1. 绘制椭圆 饼状型 贝塞尔曲线 绘制图像重写方法 void paintEvent(QPaintEvent *event)即可. void Widget::paintEvent(QPaintEve ...
- review23
文件的创建与删除 当使用File类创建一个文件对象后,例如 File file = new File("C:\\myletter", "letter.txt") ...
- 解决:创建Android模拟器时提示“No system images installed for target”
今天在Eclipse上创建安卓模拟器,但发现CPU/ABI一项显示为“No system images installed for target”: 在网上搜索答案,在叶超Luka的博客中找到了答案, ...
- c++primer 第l六章编程练习答案
6.11.1 #include<iostream> #include<cctype> int main() { using namespace std; char ch; ci ...
- java学习笔记 --- 网络编程(套接字)
1.Socket通信原理 Socket套接字概述: 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字. 通信的两端都有Socket. 网络通信其实就是Socket间的通信 ...
- scrapy入门实践1
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 这就是整个Scrapy的架构图了: 各部件职能: Scrapy ...
- Docker 容器相关技术
Docker 依赖的Linux内核特性 Namespaces 命名空间 Control groups (cgroups) 控制组 理解这两个特性,能够更好的帮助我们理解docker的资源分配和管理 N ...