JSP复习整理(五)JavaBean使用表单处理数据

时间:2021-08-21 19:53:20

一、先建立用户输入的数据

usingGetparameter.html

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Users to input date</title>
 </head>
 <body>
    <form action="usingGetParameter.jsp" method="post"></form>
         <table border="2"  bgcolor="#F8DBE5">
         <tr><td bgcolor="#F9BADA">姓名:</td>
             <td><input type="text" name="name"></td></tr>
         <tr><td bgcolor="#F9BADA">电话:</td>
             <td><input type="text" name="tel"></td></tr>
         <tr><td bgcolor="#F9BADA">电子邮箱:</td>
             <td><input type="text" name="email"></td></tr>
         <tr><td colspan="2" align="center">
             <input type="submit" value="确定">
             <input type="reset"  value="重置">
             </td></tr>
         </table>
 </body>
 </html>

二、服务器获取信息

usingGetParemeter.jsp

 <%@ 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>Jsp page show dates</title>
 </head>
 <body>
  <%
       String name = request.getParameter("name");
       String email = request.getParameter("email");
       String tel = request.getParameter("tel");
  %>
  Hello~~~<%=name %>Here....<br>     <br>
  What you input are as follows:
  <p>
                     姓        名:<%=name %><br><br>
                    电子邮箱:<%=email %><br><br>
                    电        话:<%=tel %>
 </body>
 </html>

三、运行结果:

JSP复习整理(五)JavaBean使用表单处理数据JSP复习整理(五)JavaBean使用表单处理数据

上面是比较简单的介绍JavaBean处理表单的例子。。

四、JSP+JavaBean

一、用户输入信息:

showInf.jsp

 <%@ 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>Show users' information</title>
 </head>
 <body>
    <jsp:useBean id="user" scope="session" class="jsp.test.show"></jsp:useBean>
    <jsp:setProperty property="*" name="user"/>
    <%if(request.getParameter("name")==null){ %>

       <form action="" name="Example"method="post">
       <p>姓名: <input type="text" name="name" size="17" maxlength="17"></p>
       <p>密码: <input type="password" name="password" size="17" maxlength="17"></p>
       <p>性别:<input type="radio" name="sex" value="F" checked>女
              <input type="radio" name="sex" value="M">男
              </p>
       <p>年龄:
          <select name="age">
              <option value="10">10~20</option>
              <option value="20" selected>21~30</option>
              <option value="30">31~40</option>
              <option value="40">41~70</option>
          </select>
          </p>

       <p>特长:
           <input type="checkbox" name="specialty" value="Music">
                                      音乐
            <input type="checkbox" name="specialty" value="Write">
                                      写作
           <input type="checkbox" name="specialty" value="SoftWare">
                                    软件
           <input type="checkbox" name="specialty" value="Photo">
                                   摄影
          </p>
      <p>
      <input type="submit" value="传送">
      <input type="submit" value="清除">
      </p>
       </form>
     <%}else{ %>
      姓名:<%=user.getName() %><br><br>
      密码:<%=user.getPassword() %><br><br>
      性别:<%=user.getSex() %><br><br>
      年龄:<%=user.getAge() %><br><br>
      特长:<%=user.getHobby() %><br><br>
      <%} %>

 </body>
 </html>

二、服务器接收信息:

show.java

 package jsp.test;

 public class show {

     private String name;
     private String password;
     private String sex;
     private String age;
     private String hobby;
     private String[] specialty;

     public String getHobby() {
         return hobby;
     }
     public void setHobby(String hobby) {
         this.hobby = hobby;
     }
     public void setSpecialty(String[] specialty) {
         hobby="";
         for(int i = 0; i<specialty.length; i++)
         {
             if(specialty[i].equals("Music"))
             {
                 hobby += "音乐";
             }
             if(specialty[i].equals("Write"))
             {
                 hobby += "写作";
             }
             if(specialty[i].equals("SoftWare"))
             {
                 hobby += "软件";
             }
             if(specialty[i].equals("Photo"))
             {
                 hobby += "摄影";
             }
         }
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public String getPassword() {
         return password;
     }
     public void setPassword(String password) {
         this.password = password;
     }
     public String getSex() {
         return sex;
     }
     public void setSex(String sex) {
         if(sex.equals("Male")){
             this.sex="男";
         }
         else{
             this.sex="女";
         }
     }
     public String getAge() {
         return age;
     }
     public void setAge(String age) {

         int age1=Integer.parseInt(age);
         switch(age1)
         {
         case 10:
             this.age="10~20";
             break;
         case 20:
             this.age="21~30";
             break;
         case 30:
             this.age="31~40";
             break;
         case 40:
             this.age="41~70";
             break;
             default:
                 this.age="error";
                 break;
         }
     }
     public String[] getSpecialty() {
         return specialty;
     }

 }

三、运行结果:

JSP复习整理(五)JavaBean使用表单处理数据JSP复习整理(五)JavaBean使用表单处理数据

后面的显示中姓名出现乱码。。。。

暂时就到这儿了。。