jsp5
1.教材P78-79 例4-9
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <html> 3 <head> 4 </head> 5 6 <body> 7 <% 8 double price=98.78; 9 %> 10 商品编号A1001,价格8765 11 <a href="index.jsp?id=A1001&price=8765">购买</a><br> 12 商品编号A1002,价格<%=price %> 13 <a href="index.jsp?id=A1002&price=<%=price %>">购买</a><br> 14 </body> 15 </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> </head> <body> <% String id=request.getParameter("id"); String price=request.getParameter("price"); %> <b>商品编号:<%= id %></b> 商品价格:<%= price %> </body> </html>
2.教材P97 实验2
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <% 4 request.setCharacterEncoding("UTF-8"); 5 response.setCharacterEncoding("UTF-8"); 6 %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 10 <body> 11 <form action="index.jsp"> 12 <p style="font-family: 宋体;font-size: 18; color:yellow"> 13 输入运算数,选择运算符号: 14 <input type="text" name="one" size=6/> 15 <select name="op"> 16 <option value="+">加</option> 17 <option value="-">减</option> 18 <option value="*">乘</option> 19 <option value="/">除</option> 20 </select> 21 <input type="text" name="two" size=6/> 22 <br><input type="submit" value="提交"/> 23 </form> 24 25 26 </body> 27 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <% 4 request.setCharacterEncoding("UTF-8"); 5 response.setCharacterEncoding("UTF-8"); 6 %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 10 <body> 11 <p style="font-family: 宋体;font-size: 18; color:black"> 12 <% 13 String one=request.getParameter("one"); 14 String two=request.getParameter("two"); 15 String op=request.getParameter("op"); 16 if(one==null||one.length()==0){ 17 response.sendRedirect("Copy of index.jsp"); 18 return; 19 } 20 else if(two==null||two.length()==0){ 21 response.sendRedirect("Copy of index.jsp"); 22 return; 23 } 24 double a=Double.parseDouble(one); 25 double b=Double.parseDouble(two); 26 double r=0; 27 try{ 28 if(op.equals("+")){ 29 r=a+b; 30 } 31 else if(op.equals("-")){ 32 r=a-b; 33 }else if(op.equals("*")){ 34 r=a*b; 35 }else if(op.equals("/")){ 36 r=a/b; 37 38 } 39 out.print(a+op+b+"="+r); 40 } 41 catch(Exception e){ 42 out.print("输入数字字符"); 43 } 44 %> 45 </body> 46 </html>
4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 3 <html> 4 <head> 5 </head> 6 7 <body> 8 <script type="text/javascript"> 9 function validate() { 10 if (loginForm.account.value == "") { 11 alert("账号不能为空!"); 12 return; 13 } else if (loginForm.password.value == "") { 14 alert("密码不能为空!"); 15 return; 16 } 17 loginForm.submit(); 18 } 19 </script> 20 <form action="index.jsp" name="loginForm" method="post"> 21 <p style="font-family: 宋体;font-size: 18; color:blue"> 22 输入账号:<input name="account" type="text" /><br> 23 输入密码:<input name="password" type="password" /><br> 24 是否注册会员: 25 <input type="checkbox" name="yes" value="1"/>注册 26 <input type="checkbox" name="yes" value="2"/>不注册<br> 27 <input type="button" value="登录"onClick="validate()"> 28 </form> 29 </body> 30 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 3 <html> 4 <head> 5 </head> 6 7 <body> 8 <h1>登录成功</h1> 9 <% 10 String []yes=request.getParameterValues("yes"); 11 for(int i=0;i<yes.length;i++){ 12 if(yes[i].equals("1")){ 13 out.print("注册为会员"); 14 } 15 } 16 %> 17 </body> 18 </html>
5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 7 </head> 8 9 <body> 10 <form action="MyJsp.jsp" method="post" name="form"> 11 输入任意整数N:<input type="text" name="name" > 12 <input type="submit" value="提交"> 13 14 </form> 15 </body> 16 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <html> 5 <head> 6 <title>My JSP 'index.jsp' starting page</title> 7 </head> 8 <body> 9 <% 10 request.setCharacterEncoding("UTF-8"); 11 String name=request.getParameter("name"); 12 int n=Integer.parseInt(name); 13 for(int i=0;i<n;i++){ 14 out.print("欢迎"+"<br>"); 15 } 16 17 %> 18 </body> 19 </html>
6.在页面1中输入账号和密码,进行登录,
如果账号和密码相同,则认为成功登录到页面2,
在页面2中显示一个文本框输人用户姓名,输人之后提交,
在页面3中显示用户的账号和姓名。
(转发)request.getRequestDispacher.....forward
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 </head> 6 <body> 7 <script> 8 9 function lg() { 10 if (form.user.value == "") { 11 alert("用户名不能为空!"); 12 form.user.focus(); 13 return; 14 } 15 if (form.password.value == "") { 16 alert("密码不能为空!"); 17 form.password.focus(); 18 return; 19 } 20 } 21 </script> 22 <form action="index.jsp" method="post" name="form"> 23 账号:<input type="text" name="user"/><br> 24 密码:<input type="password" name="password"/><br> 25 <input type="submit" value="登录"/> 26 </form> 27 28 </body> 29 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 </head> 6 <body> 7 <% 8 request.setCharacterEncoding("UTF-8"); 9 String user=request.getParameter("user"); 10 String password=request.getParameter("password"); 11 12 if(user.equals(password)){ 13 request.getRequestDispatcher("index1.jsp").forward(request, response); 14 15 } 16 else{ 17 request.getRequestDispatcher("index2.jsp").forward(request, response); 18 } 19 %> 20 </body> 21 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 </head> 6 <body> 7 <% 8 request.setCharacterEncoding("UTF-8"); 9 String user=request.getParameter("user"); 10 %> 11 12 <h1> 登陆成功!</h1> 13 <form action="index3.jsp" method="post" name="form"> 14 用户姓名:<input type="text" name="username"/><br> 15 <input type="submit" value="提交"/><br> 16 <input type="hidden" name="user" value="<%= user %>"/> 17 </form> 18 </body> 19 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 </head> 11 <body> 12 <% 13 request.setCharacterEncoding("UTF-8"); 14 15 %> 16 <h1> 登陆失败!</h1> 17 </body> 18 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 </head> 6 <body> 7 <% 8 request.setCharacterEncoding("UTF-8"); 9 String user=request.getParameter("user"); 10 String username=request.getParameter("username"); 11 out.print("账号:"+user+"</br>"+"用户姓名:"+"   "+username); 12 %> 13 </body> 14 </html>