第七周作业
1.教材P78-79 例4-9
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <%String id=request.getParameter("id"); String price=request.getParameter("price"); %> 商品编号:<%=id %><br> 商品价格:<%=price %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <%double price=98.78; %> 商品编号 A001,价格 8765 <a href="4-9-1.jsp?id=A1001&price=8765">购买</a><br> 商品编号 A1002,价格<%=price %> <a href="4-9-1.jsp?id=A1002&price=<%=price%>">购买</a><br> </body> </html>
2、教材P97 实验2
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <form action="counter.jsp" method="post"> 输入运算数,选择运输符号:<br> <input type="text" name="num1" size="5"/> <select name="sel"> <option selected="selected" value="+">加</option> <option value="-">减</option> <option value="*">乘</option> <option value="/">除</option> </select> <input type="text" name="num2" size="5"/><br> <input type="submit" name="submit" value="提交"/> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String num1 = request.getParameter("num1"); String num2 = request.getParameter("num2"); String sel = request.getParameter("sel"); if (num1 == null || num1.length() == 0) { response.sendRedirect("input.jsp"); return; } else if (num2 == null || num2.length() == 0) { response.sendRedirect("input.jsp"); return; } try { double n1 = Double.parseDouble(num1); double n2 = Double.parseDouble(num2); double s = 0; if (sel.equals("+")) s = n1 + n2; else if (sel.equals("-")) s = n1 - n2; else if (sel.equals("*")) s = n1 * n2; else if (sel.equals("/")) s = n1 / n2; out.print(n1 + "" + sel + "" + n2 + "=" + s); } catch (Exception e) { out.print("输入错误"); } %> </body> </html>
3、制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <%char a[] ={'0','1','2','3','4','5','6','7','8','9'}; String yan=""; for(int i=0;i<4;i++){ yan=yan+a[(int)(Math.random()*10)]; } %> <form action="domain.jsp" method="post" name="login"> 账号:<input type="text" name="account" /><br> 密码:<input type="password" name="psd" /><br> 验证码:<input type="text" name="yanzheng" /><%=yan %><br> <input type="hidden" name="yan" value="<%=yan %>"/> <input type="button" value="登录" style="margin-left: 100px" onclick="panduan()" /> <script type="text/javascript"> function panduan(){ if(login.account.value==""||login.account.value==null&&login.psd.value==""||login.psd.value==null){ alert("账号或密码不能为空!!!"); return ; } login.submit(); } </script> </form> </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 'domain.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> <% request.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); String psd=request.getParameter("psd"); String yanzheng=request.getParameter("yanzheng"); String yan=request.getParameter("yan"); if(account.equals("123456")&&psd.equals("123456")&&yanzheng.equals(yan)) request.getRequestDispatcher("win.jsp").forward(request, response); else request.getRequestDispatcher("fail.jsp").forward(request, response); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> 登录失败!!! </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> 登录成功!!! </body> </html>
4、在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <form action="domain2.jsp" method="post" name="login"> 账号:<input type="text" name="account" /><br> 密码:<input type="password" name="psd" /><br> 是否注册为会员:<input type="checkbox" name="checkbox" /> <br> <input type="submit" value="登录" style="margin-left: 100px" onclick="panduan()" /> <script type="text/javascript"> function panduan(){ if(login.account.value==""||login.account.value==null&&login.psd.value==""||login.psd.value==null){ alert("账号或密码不能为空!!!"); return ; } login.submit(); } </script> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); String psd=request.getParameter("psd"); String checkbox=request.getParameter("checkbox"); if(account.equals("123456")&&psd.equals("123456")){ if(checkbox!=null) out.print("欢迎注册为会员"+"<br>"); out.print("登录成功!!!"); } else{ out.print("登录失败!!!"); } %> </body> </html>
5、在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<form action="two.jsp" method="post">
<input type="text" name="text"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <% String text = request.getParameter("text"); int n = Integer.parseInt(text); for (int i = 0; i < n; i++) { out.print("欢迎" + "<br>"); } %> </body> </html>
6、在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。(转发)request.getRequestDispacher.....forward
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <form action="dopage1.jsp" method="post" name="login"> 账号:<input type="text" name="id" /><br> 密码:<input type="password" name="password" /><br> <input type="button" value="登录" style="margin-left: 100px" onclick="panduan()" /> <script type="text/javascript"> function panduan(){ if(login.id.value==""){ alert("账号不能为空"); return; } if(login.password.value==""){ alert("密码不能为空"); return; } login.submit(); } </script> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String id=request.getParameter("id"); String password=request.getParameter("password"); if(id.equals(password)){ request.getRequestDispatcher("page2.jsp").forward(request, response); }else{ out.print("登陆失败"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("id"); %> 登录成功到页面2!!! <form action="page3.jsp" method="post"> 用户姓名<input type="text" name="uname" /><br> <input type="submit" value="提交" /> <input type="hidden" name="src" value="<%=name%>" /> </form> </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 'page3.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> <% request.setCharacterEncoding("utf-8"); String src = request.getParameter("src"); String name = request.getParameter("uname"); %> 账号:<%=src%><br> 姓名:<%=name%> </body> </html>