首先创建一个calculate.jsp

这是用Javascript代码来验证,代码如下:

<script type="text/javascript">

function check(){
if(form.n1.value==""&&form.value.n2==""){
window.alert("值不能为空");
return false;
}
if(isNaN(form.n1.value)&&isNaN(form.n2.vavlue)){
window.alert("请确认输入的是否是数字!");
return false;
}
if(form.operator.value=="/"&&form.n2.value=="0"){
window.alert("除数不能为0,请重新输入! ");
return false;
}
}
</script>

使用Java代码来计算,代码如下:

<%
String operator=request.getParameter("operator");
int n1=Integer.parseInt(request.getParameter("n1"));
int n2=Integer.parseInt(request.getParameter("n2"));
int result=0;
if("+".equals(operator)){
result= n1+n2;
}
else if("-".equals(operator)){
result=n1-n2;
}
else if("*".equals(operator)){
result=n1*n2;
}
else {
result=n1/n2;
}
request.setAttribute("n1", n1);
request.setAttribute("n2", n2);
request.setAttribute("result", result); //往request作用域里放一个值
request.getRequestDispatcher("calculate.jsp").forward(request,response);
%>

form表单来写一个简单的页面,代码如下:

<form name="form" action="calculate_do.jsp" method="post" >
<input name="n1" value="<%= request.getAttribute("n1")==null?"":request.getAttribute("n1") %>">
<select name="operator">
<option value="+" <%= "+".equals(operator)?"selected":"" %>>+</option>
<option value="-" <%= "-".equals(operator)?"selected":"" %>>-</option>
<option value="*" <%= "*".equals(operator)?"selected":"" %>>*</option>
<option value="/" <%= "/".equals(operator)?"selected":"" %>>/</option>

</select>
<input name="n2" value="<%= request.getAttribute("n2")==null?"":request.getAttribute("n2") %>">
<input type="submit" value="=" onclick="return check();" >
<input name="result" value="<%= request.getAttribute("result")==null?"":request.getAttribute("result") %>">
</form>

源代码:

<%@ 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 'calaulate.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">
<script type="text/javascript">

function check(){
if(form.n1.value==""&&form.value.n2==""){
window.alert("值不能为空");
return false;
}
if(isNaN(form.n1.value)&&isNaN(form.n2.vavlue)){
window.alert("请确认输入的是否是数字!");
return false;
}
if(form.operator.value=="/"&&form.n2.value=="0"){
window.alert("除数不能为0,请重新输入! ");
return false;
}
}
</script>

</head>

<body>
<%
String operator=request.getParameter("operator");
int n1=Integer.parseInt(request.getParameter("n1"));
int n2=Integer.parseInt(request.getParameter("n2"));
int result=0;
if("+".equals(operator)){
result= n1+n2;
}
else if("-".equals(operator)){
result=n1-n2;
}
else if("*".equals(operator)){
result=n1*n2;
}
else {
result=n1/n2;
}
request.setAttribute("n1", n1);
request.setAttribute("n2", n2);
request.setAttribute("result", result); //往request作用域里放一个值
request.getRequestDispatcher("calculate.jsp").forward(request,response);
%>

<form name="form" action="calculate_do.jsp" method="post" >
<input name="n1" value="<%= request.getAttribute("n1")==null?"":request.getAttribute("n1") %>">
<select name="operator">
<option value="+" <%= "+".equals(operator)?"selected":"" %>>+</option>
<option value="-" <%= "-".equals(operator)?"selected":"" %>>-</option>
<option value="*" <%= "*".equals(operator)?"selected":"" %>>*</option>
<option value="/" <%= "/".equals(operator)?"selected":"" %>>/</option>

</select>
<input name="n2" value="<%= request.getAttribute("n2")==null?"":request.getAttribute("n2") %>">
<input type="submit" value="=" onclick="return check();" >
<input name="result" value="<%= request.getAttribute("result")==null?"":request.getAttribute("result") %>">
</form>

</body>
</html>

在一个jsp页面内实现简单计算器的更多相关文章

  1. 分别应用include指令和include动作标识在一个jsp页面中包含一个文件。

    分别应用include指令和include动作标识在一个jsp页面中包含一个文件. hello.jsp <%@ page language="java" import=&qu ...

  2. 在一个jsp页面中引用另一个jsp文件的路径的问题

    为了提高代码的重用性,经常需要在一个jsp页面中引用另一个jsp文件的代码.关于引用时的路径的问题.下面只介绍动态引用的,我觉得静态引用的应该也一样. 相对路径: 一.在同一个文件夹下,a.jsp中引 ...

  3. el: 在jsp页面内使用函数判断子字符串

    e.g. <c:forEach items="${datas}" var="data"> <c:if test="${not fn: ...

  4. 在一个JSP页面中包含另一个JSP页面的三种方式

    转载自://http://blog.163.com/neu_lxb/blog/static/179417010201121343132918/ (1)include指令          includ ...

  5. jsp页面元素和内置对象

    java server pages其根本是一个简化的servlet设计.实现了在java当中使用html标签.javaEE标准 一.页面元素 1.静态内容 html.js.css相关标签元素. 2.指 ...

  6. JSP页面以及简单的指令

    —JSP(Java Server Pages)是指: —在HTML中嵌入Java脚本语言 —由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本语言命令 —然后将生成的整个页面信息返回给客户端 页 ...

  7. JSP页面以及JSP九大隐式对象

    €JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. €JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比 ...

  8. 在JSP页面中调用另一个JSP页面中的变量

    在jsp学习中,经常需要在一个jsp页面中调用另一个jsp页面中的变量,下面就这几天的学习,总结一下. jsp页面之间的变量调用有多种方法: 1.通过jsp的内置对象—request对象获取参数: ( ...

  9. jsp页面九大内置对象

    资源转载自网上,不可用于商用,学习可以.内置对象又叫隐式对象/隐含对象是由WEB容器加载的一组类的实例,不需要预先声明就可以在脚本代码和表达式中随意使用的对象. 这九大隐式对象可以按照期作用分类为: ...

随机推荐

  1. node调试工具--nodemon使用简介

    这个工具和node-supervisor基本上是一致的,但是其功能比较强大,个人觉得在开发环境还是用 nodemon,因为配置比较方便,文档也很清晰.所以这里先主要讲 nodemon. nodemon ...

  2. 【题解】【合并序列(水题)P1628】

    原题链接 这道题目如果连字符串的基本操作都没学建议不要做. 学了的很简单就可以切,所以感觉没什么难度- 主要讲一下在AC基础上的优化(可能算不上剪枝) 很明显,这道题我们要找的是前缀,那么在字符串数组 ...

  3. SpringBoot学习笔记3

    十六:自定义拦截器 参考文档 16.1 编写拦截器类 extends WebMvcConfigurerAdapter 并重写WebMvcConfigurerAdapter,如下: package co ...

  4. Android 异常 UncaughtException detected: java.lang.RuntimeException: Parcelable encountered IOExcepti

    异常信息: UncaughtException detected: java.lang.RuntimeException: Parcelable encountered IOException wri ...

  5. C语言入门9-1-分类函数

    分类函数 ASCII字符可以分为英文字母.数字.控制字符.空白字符.大小写字母以及标点符号,分类是指对字符进行属性判定,判断字符属于哪些范畴,这些属性的判定在程序中非常常见,尤其是通信协议的字符处理部 ...

  6. C++里long的字节数

    标准规定long的大小不小于int也就是说sizeof(long)>=sizeof(int). Numerical type sizes in C (bits) Platforms  \   T ...

  7. Java用户程序

    Java的用户程序分为两类:Java Application和Java Applet. 这两类程序在程序结构和执行机制上有一定的差异. Java Application是完整的程序,需要独立的Java ...

  8. php 排序和置顶功能实现

    (1)排序操作思路 一般来说都是按照发布时间排序.时间戳大的靠前,所以用倒序desc,而不是asc (2)置顶操作思路: 点击置顶时,修改数据库addtime字段值为当前时间即可.因为排序是按照时间戳 ...

  9. [POJ2823] Sliding Window 「单调队列」

    我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求:   f(i) = max{ a(i-k+1),a(i-k+2),..., a(i) },i ...

  10. 动态规划_Cow Bowling_POJ-3176

    The cows don't use actual bowling balls when they go bowling. They each take a number (in the range ...