JSP第五次作业
1.教材P78-79 例4-9
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'example4.jsp' starting page</title> 6 </head> 7 8 <body bgcolor="#ffccff"> 9 <% 10 double price = 97.78; 11 %> 12 <p style="font-family:宋体;font-size:36;color:blue"> 13 商品编号 A1001,价格 8765 <a href="receive.jsp?id=A1001&price=8765">购买</a><br> 14 商品编号A1002,价格<%=price%> 15 <a href="receive.jsp?id=A1002&price=<%=price%>">购买</a> 16 </p> 17 </body> 18 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'receive.jsp' starting page</title> 6 </head> 7 8 <body bgcolor="#EEEEEE"> 9 <p style="font-family:宋体;font-size:36;color:blue"> 10 <% 11 request.setCharacterEncoding("UTF-8"); 12 response.setCharacterEncoding("UTF-8"); 13 String id=request.getParameter("id"); 14 String price=request.getParameter("price"); 15 %> 16 商品编号:<%=id %><br> 17 商品价格:<%=price %> 18 </p> 19 </body> 20 </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 <html> 4 <head> 5 <title>My JSP 'input.jsp' starting page</title> 6 </head> 7 8 <body bgcolor="#ffccff"> 9 <form action="computer.jsp" method="post" name="form"> 10 <p style="font-family:宋体;font-size=18;color=blue"></p> 11 输入运算数,选择运算符号:<br> 12 <input type="text" name="numberOne" size="6" value=56> 13 <select name="operator"> 14 <option selected="selected" value="+">加 15 <option value="-">减 16 <option value="*">乘 17 <option value="/">除 18 </select> <input type="text" name="numberTwo" size="6" value=88 /> <br> 19 <input type="submit" name="submit" value="提交" /> 20 </form> 21 </body> 22 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'computer.jsp' starting page</title> 6 </head> 7 <body bgcolor="cyan"> 8 <p style="font-family:宋体;font-size:18;color:black"> 9 <% 10 request.setCharacterEncoding("UTF-8"); 11 response.setCharacterEncoding("UTF-8"); 12 String numberOne=request.getParameter("numberOne"); 13 String numberTwo=request.getParameter("numberTwo"); 14 String operator=request.getParameter("operator"); 15 if(numberOne==null||numberOne.length()==0){ 16 response.sendRedirect("input.jsp"); 17 return; 18 }else if(numberTwo==null||numberTwo.length()==0){ 19 response.sendRedirect("input.jsp"); 20 return; 21 } 22 try{ 23 double a=Double.parseDouble(numberOne); 24 double b=Double.parseDouble(numberTwo); 25 double r=0; 26 if(operator.equals("+")) 27 r=a+b; 28 else if(operator.equals("-")) 29 r=a-b; 30 else if(operator.equals("*")) 31 r=a*b; 32 else if(operator.equals("/")) 33 r=a/b; 34 out.print(a+""+operator+""+b+"="+r); 35 }catch(Exception e){ 36 out.print("请输入数字字符"); 37 } 38 %> 39 </body> 40 </html>
3.制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'login.jsp' starting page</title> 6 </head> 7 8 <body> 9 <% 10 int intmethod1 = (int) ((((Math.random()) * 5)) + 1); 11 int intmethod2 = (int) ((((Math.random()) * 5)) + 1); 12 int intmethod3 = (int) ((((Math.random()) * 5)) + 1); 13 int intmethod4 = (int) ((((Math.random()) * 5)) + 1); 14 String intsum = intmethod1 + "" + intmethod2 + intmethod3 15 + intmethod4; 16 %> 17 <form name="loginForm" action="target.jsp" method="post"> 18 请输入账号:<input type="text" name="account"> <br> 19 请输入密码:<input type="password" name="password"> <br> 20 验证码:<input type="text" name="validationCode" onKeyDown="if(event.keyCode==13){loginForm.submit.focus();}" size="6"> 21 <input type="button" name="validationCode1" size="1" value="<%=intsum%>" /><br> 22 是否注册会员:<input type="radio" name="sf" value="是" />是 23 <input type="radio" name="sf" value="否" />否<br> 24 <input type="button" value="登录" onClick="validate()"> 25 </form> 26 <script type="text/javascript"> 27 function validate(){ 28 if(loginForm.account.value==""){ 29 alert("账号不能为空!"); 30 return; 31 } 32 if(loginForm.password.value==""){ 33 alert("密码不能为空!"); 34 return; 35 } 36 if (loginForm.validationCode.value == "") { 37 alert("验证码不能为空,请输入验证码"); 38 loginForm.validationCode.focus(); 39 return; 40 } 41 if (loginForm.validationCode.value != loginForm.validationCode1.value) { 42 alert("请输入正确的验证码"); 43 loginForm.validationCode.focus(); 44 return; 45 } 46 loginForm.submit(); 47 } 48 </script> 49 </body> 50 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'target.jsp' starting page</title> 6 </head> 7 8 <body> 9 <% 10 request.setCharacterEncoding("utf-8"); 11 String uname = request.getParameter("account"); 12 String upwd = request.getParameter("password"); 13 if (uname.equals("admin") && upwd.equals("admin")) 14 request.getRequestDispatcher("succes.jsp").forward(request, 15 response); 16 else 17 request.getRequestDispatcher("fail.jsp").forward(request, 18 response); 19 %> 20 </body> 21 </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 'succes.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 <h1>登录成功!!!!</h1> 27 <% 28 request.setCharacterEncoding("utf-8"); 29 String sf=request.getParameter("sf"); 30 if(sf.equals("是")){ 31 out.print("欢迎您注册为会员"); 32 } 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 'fail.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 <h1>登陆失败!!!</h1> 27 </body> 28 </html>
5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'page.jsp' starting page</title> 6 </head> 7 <body> 8 <form action="page1.jsp" method="post" name="page0"> 9 输入一个数字:<input type="number" name="number1"><br> 10 <input type="submit" value="提交" > 11 <input type="reset" value="重置"> 12 </form> 13 14 </body> 15 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'page1.jsp' starting page</title> 6 </head> 7 <body> 8 <% 9 //设置编码 10 request.setCharacterEncoding("UTF-8"); 11 response.setCharacterEncoding("UTF-8"); 12 //获取数据 13 String number = request.getParameter("number1"); 14 //转换数据类型 15 int n = Integer.parseInt(number); 16 //循环 17 for (int i = 0; i < n; i++) { 18 out.print("欢迎" + "<br>"); 19 } 20 %> 21 </body> 22 </html>
6.在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。(转发)request.getRequestDispatcher.....forward (request,response)
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'reg.jsp' starting page</title> 6 </head> 7 8 <body> 9 <form action="one.jsp" method="post" name="zero"> 10 <p> 11 <span>账号:</span> 12 <input type="text" name="account" > 13 </p> 14 <p> 15 <span>密码:</span> 16 <input type="password" name="pwd" > 17 </p> 18 <p> 19 <input type="submit" value="提交"> 20 <input type="reset" value="重置"> 21 </p> 22 </form> 23 </body> 24 </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 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 7 <html> 8 <head> 9 <title>My JSP 'one.jsp' starting page</title> 10 </head> 11 <body> 12 <% 13 //设置编码 14 request.setCharacterEncoding("UTF-8"); 15 //获取数据 16 String account=request.getParameter("account"); 17 String pwd=request.getParameter("pwd"); 18 //判断账号,密码是否相同 19 if(account.equals("admin")&&pwd.equals("admin")){ 20 request.getRequestDispatcher("two.jsp").forward(request,response); 21 }else{ 22 response.sendRedirect("reg.jsp"); 23 } 24 %> 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() + "://" 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 'two.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> 28 <form action="three.jsp" method="post" name="three"> 29 请输入姓名:<input type="text" name="name"><br> 30 <input id="hidden_account" type="hidden" 31 value="<%=request.getParameter("account")%>" /> 32 <input type="submit" value="提交" onclick="doClick()"> 33 </form> 34 <script type="text/javascript"> 35 function doClick() { 36 document.forms[0].action = "six/three.jsp?account=" 37 + document.getElementById("hidden_account").value; 38 document.forms[0].submit(); 39 } 40 </script> 41 </body> 42 </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 'three.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 //设置编码 28 request.setCharacterEncoding("UTF-8"); 29 response.setCharacterEncoding("UTF-8"); 30 //获取数据 31 String name=request.getParameter("name"); 32 %> 33 用户的姓名:<%=name %><br> 34 用户的账号:<%= request.getParameter("account") %> 35 </body> 36 </html>