第七周作业
1.教材P78-79 例4-9
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 14 <title>My JSP 'demo4.9.jsp' starting page</title> 15 16 <meta http-equiv="pragma" content="no-cache"> 17 <meta http-equiv="cache-control" content="no-cache"> 18 <meta http-equiv="expires" content="0"> 19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 20 <meta http-equiv="description" content="This is my page"> 21 <!-- 22 <link rel="stylesheet" type="text/css" href="styles.css"> 23 --> 24 25 </head> 26 27 <body bgcolor=#ffccff> 28 <% 29 double price = 56.56; 30 %> 31 32 <p style="font-family:宋体;font-size:36;color:blue"> 33 商品编号A0132,价格99 <a href="demo4.9receive.jsp?id=A0132&price=56.56">购买</a><br> 34 商品编号A0133,价格<%=price%> 35 <a href="demo4.9receive.jsp?id=A0133&price=<%=price%> ">购买</a> 36 </p> 37 </body> 38 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'demo4.9receive.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body bgcolor=#EEEEFF> 26 <p style="font-family:宋体;font-size:36;color:blue"> 27 <% 28 String id=request.getParameter("id"); 29 String price=request.getParameter("price"); 30 %> 31 <b>商品编号:<%= id %><br> 32 商品价格:<%= price %> 33 </p> 34 <a href="demo4.9.jsp">返回购买页</a> 35 </body> 36 </html>
2.教材P97 实验2
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 4 <html> 5 <head> <!-- seven02.jsp --> 6 </head> 7 <body> 8 <form action="seven03.jsp" method=post name=form> 9 输入运算数,选择运算符:<br> 10 <input type=text name="numberOne" size=6 /> 11 <select name="op"> 12 <option selected="selected" value="+">加 13 <option value="-">减 14 <option value="*">乘 15 <option value="/">除 16 17 </select> 18 <input type=text name="numberTwo" size=6/><br> 19 <input type="submit" name="submit" value="提交"/> 20 </body> 21 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 4 <html> 5 <head> <!-- seven03.jsp --> 6 </head> 7 <body> 8 <% 9 request.setCharacterEncoding("utf-8"); 10 String numberOne=request.getParameter("numberOne"); 11 String numberTwo=request.getParameter("numberTwo"); 12 String op=request.getParameter("op"); 13 if(numberOne==null||numberOne.length()==0){ 14 response.sendRedirect("second01.jsp");/* 获取方法 */ 15 return; 16 } 17 else if(numberTwo==null||numberTwo.length()==0){ 18 response.sendRedirect("second01.jsp"); 19 return; 20 } 21 try{ 22 double a=Double.parseDouble(numberOne); 23 double b=Double.parseDouble(numberTwo); 24 double r=0; 25 if(op.equals("+")) 26 r=a+b; 27 else if(op.equals("-")) 28 r=a-b; 29 else if(op.equals("*")) 30 r=a*b; 31 else if(op.equals("/")) 32 r=a/b; 33 out.print(a+" "+op+" "+b+"-"+r); 34 } 35 catch(Exception e){ 36 out.println("请输入数字字符"); 37 } 38 %> 39 </body> 40 </html>
3.制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <html> 3 <head> 4 <title>My JSP 'index.jsp' starting page</title> 5 </head> 6 <body> 7 <script type="text/javascript"> 8 function validate() { 9 if (loginForm.uname.value == "") { 10 alert("账号不能为空!"); 11 return; 12 } 13 if (loginForm.upwd.value == "") { 14 alert("密码不能为空!"); 15 return; 16 } 17 loginForm.submit(); 18 } 19 </script> 20 <form name="loginForm" action="page02.jsp" method="post"> 21 用户名:<input type="text" name="uname"><br> 密码: <input 22 type="password" name="upwd"> <br> 23 <input type="button" value="登录" onClick="validate()"> 24 </form> 25 </body> 26 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <html> 3 <head> 4 <title>My JSP 'page02.jsp' starting page</title> 5 </head> 6 <body> 7 <% 8 request.setCharacterEncoding("utf-8"); 9 String uname = request.getParameter("uname"); 10 String upwd = request.getParameter("upwd"); 11 if (uname.equals(upwd)) 12 request.getRequestDispatcher("page03.jsp").forward(request, 13 response); 14 else 15 request.getRequestDispatcher("page04.jsp").forward(request, 16 response); 17 %> 18 </body> 19 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <html> 3 <head> 4 <title>My JSP 'page03.jsp' starting page</title> 5 </head> 6 7 <body> 8 <body bgcolor=#ffccff> 9 <p>登录成功!</p> 10 </body> 11 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <html> 3 <head> 4 <title>My JSP 'page04.jsp' starting page</title> 5 </head> 6 7 <body bgcolor=#EEEEFF> 8 <p>登录失败!</p> 9 </body> 10 </html>
5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。
1 <% 2 request.setCharacterEncoding("utf-8"); 3 String number = request.getParameter("number"); 4 int a = Integer.parseInt(number); 5 for (int i = 0; i < a; i++) { 6 out.print("欢迎" + "<br>"); 7 } 8 %>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'success.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <% 25 request.setCharacterEncoding("utf-8"); 26 String number = request.getParameter("number"); 27 int a = Integer.parseInt(number); 28 for (int i = 0; i < a; i++) { 29 out.print("欢迎" + "<br>"); 30 } 31 %> 32 </body> 33 </html>
6.在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 14 <title>My JSP 'index.jsp' starting page</title> 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 </head> 24 25 <body> 26 <script type="text/javascript"> 27 function validate() { 28 if (loginForm.account.value == "") { 29 alert("账号不能为空!"); 30 return; 31 } 32 if (loginForm.upwd.value == "") { 33 alert("密码不能为空!"); 34 return; 35 } 36 loginForm.submit(); 37 } 38 </script> 39 40 <form name="loginForm" action="page02.jsp" method="post"> 41 账号:<input type="text" name="account"><br> 密码: <input 42 type="password" name="upwd"> <br> <input type="button" 43 value="登录" onClick="validate()"> 44 45 </form> 46 </body> 47 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'success.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <p>登录成功!</p> 25 <% 26 request.setCharacterEncoding("utf-8"); 27 String account = request.getParameter("account"); 28 %> 29 <form action="page05.jsp" method="post"> 30 用户名:<input type="text" name="username"><br> 31 <br> <input type="submit" name="submit" value="提交"> <input 32 type="hidden" name="account" value="<%=account%>"> 33 </form> 34 </body> 35 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'cheng.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body bgcolor=#EEEEFF> 24 <p>登录失败!</p> 25 </body> 26 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'fail.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 <body> 23 <% 24 request.setCharacterEncoding("utf-8"); 25 String uname = request.getParameter("uname"); 26 String upwd = request.getParameter("upwd"); 27 if (uname.equals(upwd)) 28 request.getRequestDispatcher("page02.jsp").forward(request, 29 response); 30 else 31 request.getRequestDispatcher("page03.jsp").forward(request, 32 response); 33 %> 34 </body> 35 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'page05.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <% 27 request.setCharacterEncoding("utf-8"); 28 String account = request.getParameter("account"); 29 String uname = request.getParameter("username"); 30 out.print("账号:" + account + "<br>" + "<br>"+"用户名:" + uname); 31 %> 32 </body> 33 </html>