1. 日期类型的使用转化
package com.heima;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BeanUtileTest
{
public static void main(String[] args) throws ParseException
{
SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String a1=dateformat1.format(new Date());
System.out.println("时间2:"+a1);
SimpleDateFormat dateformat2=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
String a2=dateformat2.format(new Date());
System.out.println("时间2:"+a2);
}
}
2. 在Javaweb中数据加密
import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
class TestMd5AndBase64
{
public static void main(String[] args)throws Exception
{
MessageDigest md = MessageDigest.getInstance("md5");
String name = "hello";
byte[] result = md.digest(name.getBytes());
BASE64Encoder base = new BASE64Encoder();
String b = new BASE64Encoder().encode(result);
System.out.println(b);
}
}
3. 将一个所有属性为String的bean转换为相应的属性的bean
package com.yueting.bean;
import java.util.Date;
public class UserFormBean
{
private String name = "zhangsan";
private String password = "helloworld";
private String age = "20";
private String birthday = "2012-12-12";
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 getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
public String getBirthday()
{
return birthday;
}
public void setBirthday(String birthday)
{
this.birthday = birthday;
}
public static void main(String[] args)
{
System.out.println(new Date());
}
}
2.2 User.java
package com.yueting.bean;
import java.util.Date;
public class User
{
private String name ;
private String password;
private int age ;
private Date birthday;
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 int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
@Override
public String toString()
{
return "User [name=" + name + ", password=" + password + ", age=" + age
+ ", birthday=" + birthday + "]";
}
}
3. 将一个请求中的所有数据封装到一个javabean中
package com.yueying.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.yueting.bean.*;
import com.yueting.util.WebUtil;
public class RegisterServlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Person person = null;
try{
Person bean = new Person();
/*第一个种方式直接使用BeanUtile类中的populate方法把请求中的数据直接封装到一个javabean中*/
// BeanUtils.populate(bean, req.getParameterMap());
//第二种方式将BeanUtile类中的populate方法提取出来,作为一个工具类来使用
bean = WebUtil.fillBean(req, Person.class);
System.out.println(bean.toString());
}catch (Exception e) {
e.printStackTrace();
}
}
}
2.4 工具类
package com.yueting.util;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
public class WebUtil
{
public static <T>T fillBean(HttpServletRequest req, Class<T> clazz)
{
T bean ;
try{
//获取clazz的实例
bean = clazz.newInstance();
//将req中的数据映射到bean中
BeanUtils.populate(bean, req.getParameterMap());
return bean;
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}