正常情况下你获取 页面传过来的值 你需要这样
例子:
UserBean.java
package com.st.web.bean;
public class UserBean {
private String username;
private String password;
private String emil;
private String hbody;
private String addrass;
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 getEmil() {
return emil;
}
public void setEmil(String emil) {
this.emil = emil;
}
public String getHbody() {
return hbody;
}
public void setHbody(String hbody) {
this.hbody = hbody;
}
public String getAddrass() {
return addrass;
}
public void setAddrass(String addrass) {
this.addrass = addrass;
}
@Override
public String toString() {
return "UserBean [username=" + username + ", password=" + password + ", emil=" + emil + ", hbody=" + hbody
+ ", addrass=" + addrass + "]";
}
}
获取值应该这样写
package com.st.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.st.web.bean.UserBean;
/**
* Servlet implementation class UserServlet
*/
public class UserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String addrass=request.getParameter("addrass");
String emil=request.getParameter("emil");
String hbody=request.getParameter("hbody");
UserBean bean=new UserBean();
bean.setAddrass(addrass);
bean.setEmil(emil);
bean.setHbody(hbody);
bean.setUsername(username);
bean.setPassword(password);
System.out.println(username+password+emil+hbody+addrass);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
但是我们会发现这个 是重复写了很多相同的代码
为了 减少代码量 我们 引入了 下面的beanutils工具类
这使得代码更加简洁 高效
使用beanutils
package com.st.web.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.beanutils.ConvertUtils;
import com.st.web.bean.UserBean;
/**
* Servlet implementation class UserServlet
*/
public class UserServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
try {
Map map=request.getParameterMap();
UserBean bean=new UserBean();
BeanUtils.populate(bean, map);
System.out.println("println"+bean.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
这里使用 map集合类 把获取到的值装在一起 以便使这个成为要传入数据库的的包装成一个类.
下面的该方法 让整个map集合 都传给bean 从而方便了 很多
BeanUtils.populate(bean, map);