1.p78-p79的例4-9
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body bgcolor=#9370DB > <%double price=98.78; %> <p style="font-family:宋體;font-size:36;color:Thistle"> 商品編號A1001,價格8765 <a href="two.jsp?id=A1001&price=8765">購買</a><br> 商品編號A1002,價格<%=price %> <a href="two.jsp?id=A1002&price=<%=price %>">購買</a> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'two.jsp' starting page</title> </head> <body bgcolor=#FFB6C1> <p style="font-family:宋體;font-size:36;color:blue"> <% String id=request.getParameter("id"); String price=request.getParameter("price"); %> <b>商品編號:<%=id %></b><br> 商品價格:<%=price %> </p> </body> </html>
2.p97實驗2計算器
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>p79實驗2計算器</title> </head> <body> <form action="three.jsp" method="post"> 輸入運算數,選擇運算子號<br> <br> <input type="text" name="d1" value="4"> <select name="fu"> <option>+</option> <option>-</option> <option>*</option> <option>/</option> </select> <input type="text" name="d2" value="23"><br> <br> <input type="submit" name="ti" value="提交"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>計算器</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String d1 = request.getParameter("d1"); String d2 = request.getParameter("d2"); String fu = request.getParameter("fu"); if (d1 == null || d1.length() == 0) { response.sendRedirect("index.jsp"); return; } else if (d2 == null || d2.length() == 0) { response.sendRedirect("index.jsp"); return; } try { double a = Double.parseDouble(d1); double b = Double.parseDouble(d2); double r = 0; if (fu.equals("+")) r = a + b; else if (fu.equals("-")) r = a - b; else if (fu.equals("*")) r = a * b; else if (fu.equals("/")) r = a / b; out.print(a + "" + fu + "" + b + "=" + r); } catch (Exception e) { out.println("請輸入數字字元"); } %> </body> </html>
3.製作一個登陸表單,輸入賬號和密碼,如果賬號密碼相同,跳轉到“登入成功”頁面,否則跳轉到“登入失敗”頁面。(加上JS非空驗證)(選做,加驗證碼)
4.在上題的表單中增加一個checkbox,讓使用者選擇“是否註冊為會員",如果註冊為會員,則在顯示時增加文字“歡迎您註冊為會員”。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登入表單</title> </head> <body> <form action="four.jsp" method="post" name="form1"> 賬號:<input type="text" name="name"><br> <br> 密碼:<input type="password" name="password"><br> <br><input type="checkbox" name="vip" value="歡迎您註冊為會員">是否註冊為會員<br> <br> <input type="button" name="ti" value="提交" onclick="fu()"> </form> <script type="text/javascript"> function fu() { if (form1.name.value == "") { alert("賬號不能為空"); return; } if (form1.password.value == "") { alert("密碼不能為空"); return; } form1.submit(); } </script> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>第3題</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("password"); String[] vip = request.getParameterValues("vip"); if (name.equals(password)) { out.println("登入成功"+"<br>"); if(vip!=null){ out.println("歡迎註冊為會員"); } } else { out.println("登入失敗"); } %> </body> </html>
5.在頁面1的表單內輸人一個數字N,提交,能夠在另一個頁面列印N個“歡迎”字串。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>歡迎</title> </head> <body> <form action="five.jsp" method="post"> 請輸入一個數字:<br> <br> <input type="text" name="shu"><br> <br> <input type="submit" name="ti" value="提交"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>歡迎</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String shu = request.getParameter("shu"); double a = Double.parseDouble(shu); for(int i=0;i<a;i++){ out.println("歡迎"+"</br>"); } %> </body> </html>
6.在頁面1中輸入賬號和密碼,進行登入,如果賬號和密碼相同,則認為成功登入到頁面2,在頁面2中顯示一個文字框輸人使用者姓名,輸人之後提交,在頁面3中顯示使用者的賬號和姓名。
<%@ page language="java" import="java.util.Enumeration" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>6</title> </head> <body> <form action="one6-1.jsp" method="post"> 賬號:<input type="text" name="name1" /><br> <br> 密碼:<input type="password" name="password" /><br> <br> <input type="submit" value="登入" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>第6題</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name1=request.getParameter("name1"); String password=request.getParameter("password"); if(name1.equals(password)){ request.getRequestDispatcher("two6-1.jsp").forward(request, response); } else{ request.getRequestDispatcher("one6-2.jsp").forward(request, response); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>第6題</title> </head> <body> 登入失敗 </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>第6題</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name1 = request.getParameter("name1"); %> <form action="two6-2.jsp" method="post"> 使用者姓名:<input type="text" name="name2"><br> <br> <input type="submit" value="提交"><br> <br> <input type="hidden" name="name1" value="<%= name1 %>" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>第6題</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name1 = request.getParameter("name1"); String name2 = request.getParameter("name2"); out.print("賬號:" + name1 + "</br>" + "使用者姓名:" + name2); %> </body> </html>