大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Java技巧 > 不使用BeanUtils,利用Java反射机制:表单数据自动

不使用BeanUtils,利用Java反射机制:表单数据自动(1)

关键词:BeanUtilsJava反射机制表单数据JavaB  阅读(1478) 赞(11)

[摘要]本文是对不使用BeanUtils,利用Java反射机制:表单数据自动封装到JavaBean的讲解,对学习Java编程技术有所帮助,与大家分享。

  在百度搜“java反射 将表双数据自动封装到javabean ”,第一页显示的都是一样的代码,都是运用导入第三方jar包<commons-beanutils>和<commons-logging>去完成。

开发环境:MyEclipse 10.7(亲测在MyEclipse 2014 上正常运转)

web project 版本:Java EE 6.0

JDK:1.7

Tomcat效能器版本:apache-tomcat-7.0.53

  JSP页面:

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
 <html>
 <head>
 <script type="text/javascript">
         function submitForm(){
             document.myForm.submit();
         }
     </script>
 
 </head>
   
   <body>
     <form name="myForm" action="${pageContext.request.contextPath }/regServlet" method="post"> 
     <center>
     用户名:<input type="text" name="userName" value=""><br>
     密码:<input type="password" name="password" value=""><br>
     年龄:<input type="text" name="age" value=""><br>
     工资:<input type="text" name="salary" value=""><br>
     <input type="button" value="注册" onclick="submitForm()" >
     </center>
     </form>
   </body>
 </html>

  JAVABean:

 package com.it.beans;
 
 public class Users {
     private String userName;
     private String password;
     private int age;
     private float salary;
     
     public String getUserName() {
         return userName;
     }
     public void setUserName(String userName) {
         this.userName = userName;
     }
     public String getPassword() {
         return password;
     }
     public void setPassword(String password) {
         this.password = password;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     public float getSalary() {
         return salary;
     }
     public void setSalary(float salary) {
         this.salary = salary;
     }
     
 }

  Servlet:

 package com.it.servlet;
 
 import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import com.it.beans.Users;
 
 public class RegServlet extends HttpServlet {
 
   //这里只写了doGet()和doPost()办法,其他自动生成代码没有粘贴,请留意!
    public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
 
         this.doPost(request, response);
     }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         request.setCharacterEncoding("UTF-8");
         Users user=(Users)Utils.getBean(request,"com.it.beans.Users");
 
       //这里只做后台打印演示,其他转发跳转可自行补充
         System.out.println(user.getUserName());
         System.out.println(user.getPassword());
         System.out.println(user.getAge());
         System.out.println(user.getSalary());
     }    
     
 }
«上一页12下一页»

相关Java技巧推荐

    暂时没有相关推荐



    相关评论