JSP第七周练习
1.教材P78-79 例4-9
1 <%@ page contentType="text/html" %> 2 <%@ page pageEncoding = "utf-8" %> 3 <HTML><body bgcolor = #000000> 4 <% 5 double price = 98.78; 6 %> 7 <p style="font-family:宋体;font-size:36;color:red"> 8 商品编号A1001,价格8765 9 <a href ="Test.jsp?id=A1001&price=8765">购买</a><br> 10 商品编号A1002,价格<%= price%> 11 <a href ="Test.jsp?id=A1002&price=<%= price%>">购买</a> 12 </p></body></HTML>
1 <%@ page contentType="text/html" %> 2 <%@ page pageEncoding = "utf-8" %> 3 <HTML><body bgcolor = #cccccc> 4 <p style="font-family:宋体;font-size:36;color:blue"> 5 <% String id = request.getParameter("id"); 6 String price = request.getParameter("price"); 7 %> 8 <b>商品编号:<%= id %><br> 9 商品价格:<%= price %> 10 </p></body></HTML>
2.教材P97 实验2
1 <%@ page contentType="text/html" %> 2 <%@ page pageEncoding = "utf-8" %> 3 <HTML><body bgcolor = #A9F5F2> 4 <form action="JsqTest.jsp" method=post name=form> 5 <p style="font-family:宋体;font-size:50;color:blue"> 6 输入运算数、选择运算符号:<br> 7 <input type=text name="numberOne" size=6/> 8 <select name="operator" > 9 <option selected="selected" value="+">加 10 <option value="-">减 11 <option value="*">乘 12 <option value="/">除 13 </select> 14 <input type=text name="numberTwo" size=6 /> 15 <br><input type="submit" name="submit" value="提交" size="10"/> 16 </form> 17 </p></body></HTML>
1 <%@ page contentType="text/html" %> 2 <%@ page pageEncoding = "utf-8" %> 3 <HTML><body bgcolor = #9bfc9b> 4 <p style="font-family:宋体;font-size:30;color:black"> 5 <% 6 String numberOne=request.getParameter("numberOne"); 7 String numberTwo=request.getParameter("numberTwo"); 8 String operator=request.getParameter("operator"); 9 if(numberOne==null||numberOne.length()==0) { 10 response.sendRedirect("input.jsp"); 11 return; 12 } 13 else if(numberTwo==null||numberTwo.length()==0) { 14 response.sendRedirect("input.jsp"); 15 return; 16 } 17 try{ 18 double a=Double.parseDouble(numberOne); 19 double b=Double.parseDouble(numberTwo); 20 double r=0; 21 if(operator.equals("+")) 22 r=a+b; 23 else if(operator.equals("-")) 24 r=a-b; 25 else if(operator.equals("*")) 26 r=a*b; 27 else if(operator.equals("/")) 28 r=a/b; 29 out.print(a+""+operator+""+b+"="+r); 30 } 31 catch(Exception e){ 32 out.println("请输入数字字符"); 33 } 34 %> 35 </body></HTML>
3.制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
1 <%@ page contentType="text/html"%> 2 <%@ page pageEncoding="utf-8"%> 3 <HTML> 4 <body> 5 <% 6 int intmethod1 = (int) ((((Math.random()) * 5)) + 1); 7 int intmethod2 = (int) ((((Math.random()) * 5)) + 1); 8 int intmethod3 = (int) ((((Math.random()) * 5)) + 1); 9 int intmethod4 = (int) ((((Math.random()) * 5)) + 1); 10 String intsum = intmethod1 + "" + intmethod2 + intmethod3 11 + intmethod4; 12 %> 13 <form name="loginForm" action="ShiXian.jsp" method="post"> 14 请输入账号:<input type="text" name="account"> 15 <br> 请输入密码: 16 <input type="password" name="password"> 17 <br> 验证码:<input type="text" name="validationCode" 18 onKeyDown="if(event.keyCode==13){loginForm.submit.focus();}" size="6"> 19 <input type="button" name="validationCode1" size="1" value="<%=intsum%>" /> 20 <br> 是否注册会员:<input type="radio" name="sf" value="是" />是 21 <input type="radio" name="sf" value="否" />否 22 <br> <input type="button" value="登录" onClick="validate()"> 23 </form> 24 <script type="text/javascript"> 25 function validate() { 26 if (loginForm.account.value == "") { 27 alert("账号不能为空!"); 28 return; 29 } 30 if (loginForm.password.value == "") { 31 alert("密码不能为空!"); 32 return; 33 } 34 if (loginForm.validationCode.value == "") { 35 alert("验证码不能为空,请输入验证码"); 36 loginForm.validationCode.focus(); 37 return; 38 } 39 if (loginForm.validationCode.value != loginForm.validationCode1.value) { 40 alert("请输入正确的验证码"); 41 loginForm.validationCode.focus(); 42 return; 43 } 44 loginForm.submit(); 45 } 46 </script> 47 </body> 48 </HTML>
1 <%@ page contentType="text/html"%> 2 <%@ page pageEncoding="utf-8"%> 3 <HTML> 4 <body> 5 <% 6 request.setCharacterEncoding("utf-8"); 7 String uname = request.getParameter("account"); 8 String upwd = request.getParameter("password"); 9 if (uname.equals(upwd)) 10 request.getRequestDispatcher("Cg.jsp").forward(request, 11 response); 12 else 13 request.getRequestDispatcher("Sb.jsp").forward(request, 14 response); 15 %> 16 </body> 17 </HTML>
1 <%@ page contentType="text/html" %> 2 <%@ page pageEncoding = "utf-8" %> 3 <HTML> 4 <body> 5 <h1>登陆失败!!!</h1> 6 </body></HTML>
1 <%@ page contentType="text/html" %> 2 <%@ page pageEncoding = "utf-8" %> 3 <HTML> 4 <body> 5 <h1>登录成功!!!!</h1> 6 <% 7 request.setCharacterEncoding("utf-8"); 8 String sf=request.getParameter("sf"); 9 if(sf.equals("是")){ 10 out.print("欢迎您注册为会员"); 11 } 12 %> 13 </body></HTML>
5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。
1 <%@ page contentType="text/html"%> 2 <%@ page pageEncoding="utf-8"%> 3 <html> 4 <head> 5 6 <title>My JSP 'index.jsp' starting page</title> 7 </head> 8 9 <body> 10 <form action="Hello.jsp" method="post"> 11 <p style="font-family:宋体;font-size:18;color:black"> 12 请输入数字:<input type="text" name="number" size=10><br> 13 <br> 14 <br> <input type="submit" name="submit" value="提交"> 15 </form> 16 </body> 17 </html>
1 <%@ page contentType="text/html"%> 2 <%@ page pageEncoding="utf-8"%> 3 <html> 4 <head> 5 6 <title>My JSP 'welcome.jsp' starting page</title> 7 8 </head> 9 10 <body> 11 <% 12 request.setCharacterEncoding("utf-8"); 13 String number = request.getParameter("number"); 14 int a = Integer.parseInt(number); 15 for (int i = 0; i < a; i++) { 16 out.print((i+1)+"欢迎" + "<br>"); 17 } 18 %> 19 </body> 20 </html>
6.在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。
1 <%@ page contentType="text/html"%> 2 <%@ page pageEncoding="utf-8"%> 3 <HTML> 4 <body> 5 6 <form action="MyJsp0.jsp" method="post" name="login"> 7 账号:<input type="text" name="user" /> 8 <br> 密码:<input type="password" name="password" /> 9 <br> <input type="button" value="登录" style="margin-left: 100px" onclick="panduan()" /> 10 <script type="text/javascript"> 11 function panduan() { 12 if (login.id.value == "") { 13 alert("账号不能为空"); 14 return; 15 } 16 if (login.password.value == "") { 17 alert("密码不能为空"); 18 return; 19 } 20 login.submit(); 21 } 22 </script> 23 24 </form> 25 </body> 26 </HTML>
1 <%@ page contentType="text/html" %> 2 <%@ page pageEncoding = "utf-8" %> 3 <HTML> 4 <body> 5 6 <% 7 request.setCharacterEncoding("utf-8"); 8 String id=request.getParameter("user"); 9 String password=request.getParameter("password"); 10 if(id.equals(password)){ 11 request.getRequestDispatcher("MyJsp2.jsp").forward(request, response); 12 }else{ 13 out.print("登陆失败"); 14 } 15 %> 16 17 18 19 </body></HTML>
1 <%@ page contentType="text/html"%> 2 <%@ page pageEncoding="utf-8"%> 3 <HTML> 4 <body> 5 <% 6 request.setCharacterEncoding("utf-8"); 7 String name = request.getParameter("user"); 8 %> 9 登录成功到页面2!!! 10 <form action="MyJsp3.jsp" method="post"> 11 用户姓名<input type="text" name="uname" /><br> <input type="submit" 12 value="提交" /> 13 <input type="hidden" name="src" value="<%=name%>" /> 14 </form> 15 </body> 16 </HTML>
1 <%@ page contentType="text/html"%> 2 <%@ page pageEncoding="utf-8"%> 3 <HTML> 4 <body> 5 <% 6 request.setCharacterEncoding("utf-8"); 7 String src = request.getParameter("src"); 8 String name = request.getParameter("uname"); 9 %> 10 账号:<%=src%><br> 姓名:<%=name%> 11 </body> 12 </HTML>