BeanUtils(bean,name,value) 一调用这个方法就抛空指针异常

as245498513 2014-05-07 05:31:08
就是想把jsp提交过来的数据用BeanUtils(bean,name,value) 方法来封装到一个formbean里面,但是在servlet一调用这个方法就抛空指针异常,而用Junit 在java程序里面调用,居然又可以封装。下面是异常信息,求大神解救我这个菜鸟啊,需要用到的JSP,servlet和formbean所有代码都贴在下面了,谢谢!

SEVERE: Servlet.service() for servlet [junit.test.test1] in context with path [/Login] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.commons.beanutils.BeanUtils
————————————————————————————————————————————
这个是JSP提交数据后转到的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 java.lang.reflect.InvocationTargetException;
import junit.test.BeanUtilsTest;
import org.apache.commons.beanutils.BeanUtils;
import utils.WebUtils;
import formbean.RegisterForm;

@WebServlet("/RequestServlet")
public class RequestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//RegisterForm就是用来封装提交信息的formbean
RegisterForm form=WebUtils.request2bean(request, RegisterForm.class);

}

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

}
------------------------------------------------------------------------------------------------------------------------------------------------------------
这个是上面servlet调用方法的类
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import formbean.RegisterForm;
public class WebUtils {
public static <T>T request2bean(HttpServletRequest request,Class<T> beanClass){
try {
//创建要保存的bean实例
T bean=beanClass.newInstance();
//把request的数据整到bean里面
Enumeration en=request.getParameterNames();
while(en.hasMoreElements()){
String name=(String) en.nextElement();
String value=request.getParameter(name);
BeanUtils.setProperty(bean, name, value);
}
return bean;
} catch (Exception e) {
System.out.println(e.toString());
throw new RuntimeException(e);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
这个就是用来封装信息的FormBean
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
public class RegisterForm {
private String username;
private String password;
private String password2;
private String email;
private String birthday;
private String nickname;
private String checknum;

private Map errors = new HashMap();

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 getPassword2() {
return password2;
}

public void setPassword2(String password2) {
this.password2 = password2;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public String getChecknum() {
return checknum;
}

public void setChecknum(String checknum) {
this.checknum = checknum;
}

public Map getErrors() {
return errors;
}

public void setErrors(Map errors) {
this.errors = errors;
}
}
}
}

--------------------------------------------------------------------------------------------------------------------
这个是JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册界面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/RequestServlet" method="post">
<table>
<tr>
<td>登陆账号:</td>
<td><input type="text" name="username" /> <span>${form.errors.username }</span>
</td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="text" name="password2" /> <span>${form.errors.password2 }</span>
</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="email" /> <span>${form.errors.email }</span></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="text" name="birthday" /></td>
</tr>
<tr>
<td>昵称:</td>
<td><input type="text" name="nickname" /> <span>${form.errors.nickname }</span>
</td>
</tr>
<tr>
<td>认证码:</td>
<td><input type="text" name="checknum" /> <span>认证码不正确!</span>
</td>
</tr>
<tr>
<td><input type="submit" value="注册" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
...全文
1330 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
servlet测试要放到web容器内才行。
teemai 2014-05-08
  • 打赏
  • 举报
回复
java.lang.ClassNotFoundException: org.apache.commons.beanutils.BeanUtils 这不是空指针啊
wyx100 2014-05-08
  • 打赏
  • 举报
回复
引用 1 楼 huxiweng 的回复:
java.lang.ClassNotFoundException: org.apache.commons.beanutils.BeanUtils 这不是空指针啊
DwGoing 2014-05-08
  • 打赏
  • 举报
回复
好像是类缺失的问题啊

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧