第七周
1.教材P78-79 例4-9
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index.jsp</title> </head> <body> <%double price=4.5; %> <p style="font-family:宋体;font-size: 25"> 商品编号A1001,单价:15<a href="buy.jsp?num=A1001&price=15">购买</a><br/> 商品编号A1002,单价:<%=price %> <a href="buy.jsp?num=A1002&price=<%= price%>">购买</a> </p> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <body> <p style="font-family:宋体;font-size: 25"> <% String num=request.getParameter("num"); String price=request.getParameter("price"); %> 商品编号:<%=num %><br/> 商品单价:<%=price %><br/> </p> </body> </html>
2.教材P97 实验2
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML > <html> <head> </head> <form action="index1.jsp" method="post" name="form"> <p style="font-family: 宋体;font-size: 18; color: blue"> 输入运算数,选择运算符:<br> <input type="text" name="num1" size=6 /> <select name="select"> <option selected="selected" value="+">加 <option value="-">减 <option value="*">乘 <option value="/">除 </select> <input type="text" name="num2" size=6 /> <input type="submit" value="提交"> </p> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML > <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <p style="font-family: 宋体; font-size: 16;color: black;"> <% String num1 = request.getParameter("num1"); String num2 = request.getParameter("num2"); String select = request.getParameter("select"); if (num1 == null || num1.length() == 0) { response.sendRedirect("index.jsp"); return; } else if (num2 == null || num2.length() == 0) { response.sendRedirect("index.jsp"); return; } try { double n1 = Double.parseDouble(num1); double n2 = Double.parseDouble(num2); double x = 0; if (select.equals("+")) { x = n1 + n2; } else if (select.equals("-")) { x = n1 - n2; } else if (select.equals("*")) { x = n1 * n2; } else if (select.equals("/")) { x = n1 / n2; } out.print(n1+""+select+""+n2+"="+x); } catch (Exception e) { out.print("请输入数字字符:"); } %> </p> </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 yzm=""; for(int i=0;i<4;i++){ yzm=yzm+a[(int)(Math.random()*10)]; } %> <form action="index1.jsp" method="post" name="login"> 账号:<input type="text" name="account" /><br><br> 密码:<input type="password" name="psd" /><br><br> 验证码:<input type="text" name="yanzheng" /><%=yzm %><br><br> <input type="hidden" name="yzm" value="<%=yzm %>"/> <input type="button" value="登录" style="margin-left: 100px" onclick="check()" /> <script type="text/javascript"> function check(){ 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"%> <!DOCTYPE HTML > <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); String psd=request.getParameter("psd"); String yanzheng=request.getParameter("yanzheng"); String yzm=request.getParameter("yzm"); if(account.equals("123456")&&psd.equals("123456")&&yanzheng.equals(yzm)) request.getRequestDispatcher("ok.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="index1.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="check()" /> <script type="text/javascript"> function check(){ 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="index1.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中显示用户的账号和姓名。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> </head> <body> <form action="index1.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="check()" /> <script type="text/javascript"> function check(){ 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("index2.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="index3.jsp" method="post"> 用户姓名<input type="text" name="username" /><br> <input type="submit" value="提交" /> <input type="hidden" name="src" value="<%=name%>" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String src = request.getParameter("src"); String name = request.getParameter("username"); %> 账号:<%=src%><br> 姓名:<%=name%> </body> </html>