JSP第五次作业
1.教材P78-79 例4-9
<body bgcolor="#ffc0cb"> <% double price=98.78; %> <p style="font-family: 宋体;font-size: 36px; color: aquamarine"> 商品编号A10001,价格 8765 <a href="8receive.jsp?id=A10001&price=8765">购买</a><br> 商品编号A10002,价格<%=price %> <a href="8receive.jsp?id=A10002&price=<%=price %>">购买</a><br> </body>
<body bgcolor="#ffc0cb"> <p style="font-family: 宋体;font-size: 36px;color: aquamarine"> <% String id=request.getParameter("id"); String price=request.getParameter("price"); out.print("商品编号:"+id+"</br>"+"商品价格:"+price); %> </p> </body>
2.教材P97 实验2
<body BGCOLOR="#ffc0cb"> <p>请输入运算数,选择符号</p> <form action="9receive.jsp" method="post"> <input type="text" name="num1" id="num1"> <select name="sign" name="sign" id="sign"> <option value="+">加</option> <option value="-">减</option> <option value="*">乘</option> <option value="/">除</option> </select> <input type="text" name="num2" id="num2"> <br><br> <input type="submit" value="提交"> </form> </body>
<body bgcolor="#ffc0cb"> <% request.setCharacterEncoding("utf-8"); String num1 = request.getParameter("num1"); String num2 = request.getParameter("num2"); String sign = request.getParameter("sign"); if (num1 == null || num1.length() == 0) { response.sendRedirect("test9.jsp"); return; } else if (num2 == null || num2.length() == 0) { response.sendRedirect("test9.jsp"); return; } try { double n1 = Double.parseDouble(num1); double n2 = Double.parseDouble(num2); double s = 0; if (sign.equals("+")) s = n1 + n2; else if (sign.equals("-")) s = n1 - n2; else if (sign.equals("*")) s = n1 * n2; else if (sign.equals("/")) s = n1 / n2; out.print(n1 + "" + sign + "" + n2 + "=" + s); } catch (Exception e) { out.print("输入错误"); } %> </body>
3.制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
<head> <title>Title</title> <script type="text/javascript"> function login(){ if(logindo.admin.value==""){ alert("请输入账号"); return; } else if(logindo.pass.value==""){ alert("请输入密码"); return; } logindo.submit(); } </script> </head> <body bgcolor="#ffc0cb"> <form action="10receive.jsp" name ="logindo"> <p style="font-family: 宋体;font-size:16px; color:aquamarine"> 账号: <input type="text" name="admin" size=12/> 密码: <input type="password" name="pass" size=12/> <br><input type="button" value="登录" onclick="login()"/> </form> </body>
<body bgcolor="#ffc0cb"> <% String admin=request.getParameter("admin"); String pass=request.getParameter("pass"); if(admin.equals(pass)){ request.getRequestDispatcher("Yes.jsp").forward(request, response); } else{ response.sendRedirect("No.jsp"); } %> </body>
<body bgcolor="#ffc0cb"> 恭喜!登陆成功。 </body>
<body bgcolor="#ffc0cb"> 抱歉!登陆失败。 </body>
4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
<head> <title>Title</title> <script type="text/javascript"> function login(){ if(logindo.admin.value==""){ alert("请输入账号"); return; } else if(logindo.pass.value==""){ alert("请输入密码"); return; } logindo.submit(); } </script> </head> <body bgcolor="#ffc0cb"> <form action="10receive.jsp" name ="logindo"> <p style="font-family: 宋体;font-size:16px; color:aquamarine"> 账号: <input type="text" name="admin" size=12/> 密码: <input type="password" name="pass" size=12/> 是否注册会员: <input type="radio" name="vip" value="注册"/>注册 <input type="radio" name="vip" value="不注册"/>不注册 <br><input type="button" value="登录" onclick="login()"/> </form> </body>
<body> <h1>恭喜!登陆成功。</h1> <% String []vip=request.getParameterValues("vip"); for(int i=0;i<vip.length;i++){ if(vip[i].equals("注册")){ out.print("注册为会员"); } } %> </body>
5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。
<body bgcolor="#ffc0cb"> <form action="11receive.jsp"> <input type="text" name="num" /> <input type="submit" value="提交" /> </form> </body>
<body bgcolor="#ffc0cb"> <% String number=request.getParameter("num"); int i=Integer.parseInt(number); for(int j=0;j<i;j++){ out.print("欢迎"+"<br>"); } %> </body>
6.在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。
<head> <title>Title</title> <script type="text/javascript"> function login(){ if(logindo.admin.value==""){ alert("请输入账号"); return; } else if(logindo.pass.value==""){ alert("请输入密码"); return; } logindo.submit(); } </script> </head> <body bgcolor="#ffc0cb"> <form action="1.jsp" name ="logindo"> <p style="font-family: 宋体;font-size:16px; color:aquamarine"> 账号: <input type="text" name="admin" size=12/> 密码: <input type="password" name="pass" size=12/> 是否注册会员: <input type="radio" name="vip" value="注册"/>注册 <input type="radio" name="vip" value="不注册"/>不注册 <br><input type="button" value="登录" onclick="login()"/> </form> </body>
<body bgcolor="#ffc0cb"> 恭喜!登陆成功。 <hr> <% request.setCharacterEncoding("utf-8"); String zh=request.getParameter("zh"); String yh=request.getParameter("user"); out.print("用户: "+yh+" 账号: "+zh); %> </body>
<body bgcolor="#ffc0cb"> <% request.setCharacterEncoding("utf-8"); String zh=request.getParameter("admin"); String yh=request.getParameter("pass"); out.print("账号"+yh+"密码"+zh); %> <form action="Yes.jsp"> 用户名<input type="text" name="user"> <input type="submit" value="提交" > <input name="zh" type="hidden" value="<%=zh%>"> </form> </body>