JSP第七次作业
1.教材P78-79 例4-9
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body bgcolor=#ffccff> <% double price=98.78; %> <p style="font-family:宋体;font-size:36;color:blue"> 商品编号 A1001,价格 8765 <a href="ShangTest.jsp?id=A1001&price=8765">购买</a><br> 商品编号A1002,价格<%= price %> <a href="ShangTest.jsp?id=A1002&price=<%= price %>">购买</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body bgcolor=#EEEEFF> <p style="font-family:宋体;font-size:36;color:blue"> <% String id=request.getParameter("id"); String price=request.getParameter("price"); %> <b>商品编号:<%=id %><br> 商品价格:<%=price %> </p> </body> </html>
2.教材P97 实验2
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="computer.jsp" method=post name=form> <p style="font-family:宋体;font-size:18;color:blue"> 输入运算数,选择运算符号:<br> <input type=text name="numberOne" size=6 /> <select name="operator"> <option selected="selected" value="+">加 <option value="-">减</option> <option value="*">乘</option> <option value="/">除</option> </select> <input type=text name="numberTwo" size=6 /> <br><input type="submit" name="submit" value="提交"> </form> </p> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <p style="font-family:宋体;font-size:18;color:black"> <% String numberOne=request.getParameter("numberOne"); String numberTwo=request.getParameter("numberTwo"); String operator=request.getParameter("operator"); if(numberOne==null||numberOne.length()==0){ response.sendRedirect("input.jsp"); return; } else if(numberTwo==null||numberTwo.length()==0){ response.sendRedirect("input.jsp"); return; } try{ double a=Double.parseDouble(numberOne); double b=Double.parseDouble(numberTwo); double r=0; if(operator.equals("+")) r=a+b; else if(operator.equals("-")) r=a-b; else if(operator.equals("*")) r=a*b; else if(operator.equals("/")) r=a/b; out.print(a+""+operator+""+b+"="+r); } catch(Exception e){ out.print("请输入数字字符"); } %> </body> </html>
3.制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form name="login" action="loginzhong.jsp" method="post"> 账号:<input name="account" type="text"><br> 密码:<input name="password" type="password"><br> 是否注册成为会员:<input type="checkbox" name="sign" value="shi" checked="checked"/>是 <input type="checkbox" name="sign" value="fou" />否<br> 验证码:<input name="userid" type="text" maxlength="20" /> 验证码: <% char[] c = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M' }; int n; StringBuffer sb = new StringBuffer(); for (int i = 0; i < 4; i++) { n = (int) (Math.random() * 36); %> <%=c[n]%> <% sb.append(c[n]); } request.getSession().setAttribute("radon", sb.toString()); %> <br/> <input type="submit" name="submit" value="登录"> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); String password=request.getParameter("password"); String userid=request.getParameter("userid"); String sign=request.getParameter("sign"); String randon=(String) request.getSession().getAttribute("radon"); if(account.equals(password)){ if(userid.equals(randon)){ if(sign.equals("shi")){ request.getRequestDispatcher("loginHui.jsp").forward(request,response); }else{ request.getRequestDispatcher("loginSuccess.jsp").forward(request,response); } }else{ request.getRequestDispatcher("loginYan.jsp").forward(request,response); } }else{ request.getRequestDispatcher("loginFail.jsp").forward(request,response); } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 验证码不一致 </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 登录成功<br> 欢迎您,您是普通会员! </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 登录成功<br> 欢迎您注册为会员 </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 登录失败 </body> </html>
5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body bgcolor="pink"> <form action="ShuNTest.jsp" method=post name=form> <input type="text"value=""name="string"><br> <input type="submit"value="提交"name="submit"> <input type="reset"value="重置"> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body bgcolor="pick"> <% String string=request.getParameter("string"); int x=Integer.parseInt(string); for(int i=0;i<=x;i++){ out.print("欢迎!"); } %> <br><a href="ShuN.jsp">返回</a> </body> </html>
6.在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form name="loginForm" action="Ye04.jsp" method="post"> 账号:<input name="account" type="text"><br> 密码:<input name="password" type ="password"><br> <input type="submit" name="submit" value="登录"> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String account=request.getParameter("account"); String password=request.getParameter("password"); if(!account.equals(password)){ out.print("用户名密码不相同,输入错误"); }else{ request.getRequestDispatcher("Ye02.jsp").forward(request,response); } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); String str=request.getParameter("account"); %> <form action="Ye03.jsp" method="post"> 用户姓名:<input name="name" type="text"><br> <input type="submit" value="提交"> <input name="account1" type="hidden" value="<%= str %>"> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); String str1=request.getParameter("account1"); String str2=request.getParameter("name"); out.println("用户账号:"+str1+"<br>"+"用户姓名:"+str2); %> </body> </html>