javaEE BeanUtils工具类, 将用户参数封装成JavaBean实体对象, 参数乱码问题, UUID

BeanUtils工具包下载(Jar包):https://pan.baidu.com/s/1h9Yi4pbNhYbRnLaF0Y8BtQ  密码:6kth

commons-beanutils-1.8.3.jar 和 commons-logging-1.1.1.jar包----Build Path----Add To Build Path


RegisterServlet:

package com.xxx.register;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;

import com.xxx.utils.DataSourceUtils;

public class RegisterServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		//设置request的编码---只适合post方式
		request.setCharacterEncoding("UTF-8");
		
		//get方式乱码解决
		//String username = request.getParameter("username");  //乱码参数
		//username = new String(username.getBytes("iso8859-1"),"UTF-8");

		//使用BeanUtils自动将用户参数(Map集合)映射封装成JavaBean实体对象
		//map中的key与实体的属性保持对应
		//只要key与实体的属性一样,就自动封装到实体中
		Map<String, String[]> properties = request.getParameterMap();
		User user = new User();
		try {
			BeanUtils.populate(user, properties); // 将map参数封装成user对象。
		} catch (Exception e) {
			e.printStackTrace();
		}

		//可以手动封装uid(注册)----uuid---随机不重复的字符串32位--java代码生成的是36位(多4个"-")
		user.setUid(UUID.randomUUID().toString());

		//........
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doGet(request, response);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值