实验名称
Request内置对象的应用
实验内容
1、用request对象设计实现一个简单的计算加减乘除运算的网页程序。
2、编制一个具有时间显示的页面,利用response对象定时刷新页面,体会运行结果。
实验目的
掌握form表单提交信息;掌握常用内置对象的用途和使用方法;掌握在jsp页面申明使用方法。
实验要求
1、四则运算要求先登陆后计算,并利用session对象存储用户姓名并显示。
2、时间实现页面要求每隔1秒刷新一次。
实验步骤(代码、运行结果)
代码
加减乘除运算
<%@ 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 'counts.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>
<body bgcolor =pink >
<%-- 用session对象存储用户姓名并显示 --%>
<%
String name=request.getParameter("name");
if(name==null)
name="";
session.setAttribute("name",name);
String name_final=(String)session.getAttribute("name");
%>
<%-- 网页内容 --%>
<div class="top" align="center">
<h1>欢迎<%=name_final %>使用我设计的专属计算机!</h1>
</div>
<div align="center" style="margin-top: 100px; border: 10px; border-color: #ffccff">
<%--
提供一一个表单,要求表单中提供两个text输人框,供用户输入数字;提
供一个下拉列表,该下拉列表有加、减、乘、除四个选项,供用户选择运算符号。
用户在表单中输人的数字、选择运算符号提交给本页面
--%>
<form action ="counts/countsError.jsp" method =post name =form >
<h3>请输入数字与计算方式:</h3>
<input type =text name ="numberOne" size =10 >
<select name ="operator" >
<Option value ="+" >加
<Option value ="-" >减
<Option value ="*" >乘
<Option value ="/" >除
</select >
<input type =text name ="numberTwo" size =6 ><br ><br >
<input type ="submit" value =" 提交" name ="submit" ><br >
</form >
</div>
</body >
</html>
加减乘除运算—结果及报错
<%@ 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 'countsError.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>
<body alink="center" bgcolor="pink">
<div align="center" style="font-size: 40px;">
<%
String numberOne=request.getParameter( "numberOne" );
String numberTwo=request.getParameter( "numberTwo" );
String operator=request.getParameter( "operator" );
if (numberOne== null ){
numberOne= "0" ;
}
if (numberTwo== null ){
numberTwo= "0" ;
}
try {
double a=Double.parseDouble(numberOne);
double b=Double.parseDouble(numberTwo);
double r=0;
if (operator.equals( "+" ))
r=a+b;
else if (operator.equals( "-" ))
r=a-b;
else if (operator.equals( "*" ))
r=a*b;
else if (operator.equals( "/" ))
r=a/b;
out.println(a+ "" +operator+ "" +b+ "=" +r);
}
catch (Exception e){
out.println( " 请重新输入! " );
}
%>
</div>
</body>
</html>
时间显示页面
<%@ 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 'time.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>
<body bgcolor =pink >
<div align="center" >
<h1>
<%=new Date().toLocaleString() %>
<%
response.setHeader("refresh","1");//设置每1秒刷新一次
%>
</h1>
</div>
</body>
</html>
登录界面
<%@ 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 'login.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>
<body bgcolor =pink>
<div class="top">
<h1 align="center">欢迎来到我设计的计算机!</h1>
</div>
<div class="center" align="center" style="margin-top: 100px; border: 10px; border-color: #ffccff">
<form action="counts/counts.jsp" method=post name=form>
请输入名字登录:
<input type=text name="name" size=6><br><br>
<input type="submit" value=" 提交" name="submit">
</form>
</div>
</body>
</html>
运行结果
加减乘除界面
初始界面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6IQQU6e4-1722337860031)(https://i-blog.csdnimg.cn/blog_migrate/90d9d67e1c227d2c968d0e72690ee8d0.png)]
输入名字
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BwEykyfh-1722337860033)(https://i-blog.csdnimg.cn/blog_migrate/9cb8659304b26e8c7d3b21be3eb80de3.png)]
进入计算器
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tkKc9dNh-1722337860033)(https://i-blog.csdnimg.cn/blog_migrate/cebf2690e77dde97a3997e144159cbab.png)]
实现加法运算
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iet5IBw7-1722337860034)(https://i-blog.csdnimg.cn/blog_migrate/80b09b2a8d758e286c63dfcb40b75b04.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mEUBkeAh-1722337860035)(https://i-blog.csdnimg.cn/blog_migrate/1941709cea50e0bddb84c9efc70bb46c.png)]
实现减法运算
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h6ZCtCSK-1722337860035)(https://i-blog.csdnimg.cn/blog_migrate/a3697105672f37953553c65b9bcd4a98.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C3tWet6C-1722337860036)(https://i-blog.csdnimg.cn/blog_migrate/b1e948a0dd19463fc046632ef28737a2.png)]
实现乘法运算
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fQ2hQac5-1722337860036)(https://i-blog.csdnimg.cn/blog_migrate/8bdd42a369308fb3314b01def531d407.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vLLMq2B5-1722337860037)(https://i-blog.csdnimg.cn/blog_migrate/e1f4f44523d994551b0e425baa1e4ac5.png)]
实现除法运算
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2EyfOWJo-1722337860037)(https://i-blog.csdnimg.cn/blog_migrate/98e79d380fc8c3b2364fd82cefbe972c.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xmZDR57I-1722337860038)(https://i-blog.csdnimg.cn/blog_migrate/8b31dda76c775a8ec1cfc4a55ff6f099.png)]
时间实现效果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FC1aBaFi-1722337860038)(https://i-blog.csdnimg.cn/blog_migrate/b067329f4096e4c45ffd6e5db5c181fe.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S2iQmWK8-1722337860039)(https://i-blog.csdnimg.cn/blog_migrate/a06fc5bd1d0e5f7d3a35c97f4502c29e.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ddef7pbp-1722337860039)(https://i-blog.csdnimg.cn/blog_migrate/45cd9061dad610a54d11863fd36d6b87.png)]