标签:
一、先建立用户输入的数据
usingGetparameter.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Users to input date</title> 6 </head> 7 <body> 8 <form action="usingGetParameter.jsp" method="post"></form> 9 <table border="2" bgcolor="#F8DBE5"> 10 <tr><td bgcolor="#F9BADA">姓名:</td> 11 <td><input type="text" name="name"></td></tr> 12 <tr><td bgcolor="#F9BADA">电话:</td> 13 <td><input type="text" name="tel"></td></tr> 14 <tr><td bgcolor="#F9BADA">电子邮箱:</td> 15 <td><input type="text" name="email"></td></tr> 16 <tr><td colspan="2" align="center"> 17 <input type="submit" value="确定"> 18 <input type="reset" value="重置"> 19 </td></tr> 20 </table> 21 </body> 22 </html>
二、服务器获取信息
usingGetParemeter.jsp
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Jsp page show dates</title> 8 </head> 9 <body> 10 <% 11 String name = request.getParameter("name"); 12 String email = request.getParameter("email"); 13 String tel = request.getParameter("tel"); 14 %> 15 Hello~~~<%=name %>Here....<br> <br> 16 What you input are as follows: 17 <p> 18 姓 名:<%=name %><br><br> 19 电子邮箱:<%=email %><br><br> 20 电 话:<%=tel %> 21 </body> 22 </html>
三、运行结果:
上面是比较简单的介绍JavaBean处理表单的例子。。
四、JSP+JavaBean
一、用户输入信息:
showInf.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Show users‘ information</title> 8 </head> 9 <body> 10 <jsp:useBean id="user" scope="session" class="jsp.test.show"></jsp:useBean> 11 <jsp:setProperty property="*" name="user"/> 12 <%if(request.getParameter("name")==null){ %> 13 14 <form action="" name="Example"method="post"> 15 <p>姓名: <input type="text" name="name" size="17" maxlength="17"></p> 16 <p>密码: <input type="password" name="password" size="17" maxlength="17"></p> 17 <p>性别:<input type="radio" name="sex" value="F" checked>女 18 <input type="radio" name="sex" value="M">男 19 </p> 20 <p>年龄: 21 <select name="age"> 22 <option value="10">10~20</option> 23 <option value="20" selected>21~30</option> 24 <option value="30">31~40</option> 25 <option value="40">41~70</option> 26 </select> 27 </p> 28 29 <p>特长: 30 <input type="checkbox" name="specialty" value="Music"> 31 音乐 32 <input type="checkbox" name="specialty" value="Write"> 33 写作 34 <input type="checkbox" name="specialty" value="SoftWare"> 35 软件 36 <input type="checkbox" name="specialty" value="Photo"> 37 摄影 38 </p> 39 <p> 40 <input type="submit" value="传送"> 41 <input type="submit" value="清除"> 42 </p> 43 </form> 44 <%}else{ %> 45 姓名:<%=user.getName() %><br><br> 46 密码:<%=user.getPassword() %><br><br> 47 性别:<%=user.getSex() %><br><br> 48 年龄:<%=user.getAge() %><br><br> 49 特长:<%=user.getHobby() %><br><br> 50 <%} %> 51 52 </body> 53 </html>
二、服务器接收信息:
show.java
1 package jsp.test; 2 3 public class show { 4 5 private String name; 6 private String password; 7 private String sex; 8 private String age; 9 private String hobby; 10 private String[] specialty; 11 12 13 public String getHobby() { 14 return hobby; 15 } 16 public void setHobby(String hobby) { 17 this.hobby = hobby; 18 } 19 public void setSpecialty(String[] specialty) { 20 hobby=""; 21 for(int i = 0; i<specialty.length; i++) 22 { 23 if(specialty[i].equals("Music")) 24 { 25 hobby += "音乐"; 26 } 27 if(specialty[i].equals("Write")) 28 { 29 hobby += "写作"; 30 } 31 if(specialty[i].equals("SoftWare")) 32 { 33 hobby += "软件"; 34 } 35 if(specialty[i].equals("Photo")) 36 { 37 hobby += "摄影"; 38 } 39 } 40 } 41 public String getName() { 42 return name; 43 } 44 public void setName(String name) { 45 this.name = name; 46 } 47 public String getPassword() { 48 return password; 49 } 50 public void setPassword(String password) { 51 this.password = password; 52 } 53 public String getSex() { 54 return sex; 55 } 56 public void setSex(String sex) { 57 if(sex.equals("Male")){ 58 this.sex="男"; 59 } 60 else{ 61 this.sex="女"; 62 } 63 } 64 public String getAge() { 65 return age; 66 } 67 public void setAge(String age) { 68 69 int age1=Integer.parseInt(age); 70 switch(age1) 71 { 72 case 10: 73 this.age="10~20"; 74 break; 75 case 20: 76 this.age="21~30"; 77 break; 78 case 30: 79 this.age="31~40"; 80 break; 81 case 40: 82 this.age="41~70"; 83 break; 84 default: 85 this.age="error"; 86 break; 87 } 88 } 89 public String[] getSpecialty() { 90 return specialty; 91 } 92 93 94 }
三、运行结果:
后面的显示中姓名出现乱码。。。。
暂时就到这儿了。。
标签:
原文地址:http://www.cnblogs.com/haixiaomei/p/OUC1125.html