使用BeanUtils接收前台表单数据(简化Servlet)

BeanUtils的作用:可以快速的将前台接收的数据封装到对象中。

一、写一个简单的Servlet程序获取表单数据

这部分可以参考这篇博客:

JavaWeb:用java做一个servlet程序获取表单数据(新手向)_ShareFive的博客-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/weixin_74250204/article/details/132865499?spm=1001.2014.3001.5502

二、BeanUtils代码部分

首先,要使用BeanUtils,要先将其jar包导入java库

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
    @Override
    public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");
        User user = new User();
        /*ConvertUtils.register(new Converter() {//这是针对获取表单日期转换的转换器,用不到就可以不用写
            @Override
            public Object convert(Class aClass, Object value) {
                if (value == null) {
                    return null;
                }
                if (!(value instanceof String)) {
                    throw new ConversionException("conversion error");
                }
                String star = (String) value;
                if (star.trim().equals("")) {
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd");
                try {
                    if (star.contains("-")){
                        return sdf.parse(star);
                    }
                    if (star.contains("/")){
                        return sdf1.parse(star);
                    }
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                return null;
            }
        },Date.class);*/
        try {
            BeanUtils.populate(user, req.getParameterMap());
            System.out.println(user);
        } catch (IllegalAccessException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

三、创建User类

import java.util.Arrays;
import java.util.Date;

public class User {
    private String username;
    private String password;
    private String gender;
    //private String age;
    //private Date date;
    private String[] hobbies;
    private String address;
    private String remark;


    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 String getGender() {
        return gender;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String[] getHobbies() {
        return hobbies;
    }

    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                //", age='" + age + '\'' +
                //", date='" + date + '\'' +
                ", hobbies=" + Arrays.toString(hobbies) +
                ", address='" + address + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值