首页 新闻 搜索 专区 学院

做web项目,注册页面时,点击提交出现异常,检查几遍都没有发现异常出现在什么地方,想让大牛帮帮我看下

0
[已解决问题] 解决于 2018-08-28 14:51

做网上商城,在点击用户注册时候,提交,出现异常,怎么都找不出异常在什么地方

一下是小项目的代码

package com.itheima.web.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

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

/**
* servlet的生命周期中,service方法贯穿整个servlet的生命周期,所以创建BaseServlet要首先继承httpServlet,重写HttpServlet中的service方法
* 在service方法中完成通用的servlet。
*
*
*
* 通用的Servlet
*/
public class BaseServlet extends HttpServlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 1.获取子类 创建子类或者调用子类的时候 this代表的是子类对象
@SuppressWarnings("rawtypes")
Class clazz = this.getClass();
//System.out.println(this);

// 2.获取请求的方法
String m = request.getParameter("method");
if(m==null){
m="index";
}
//System.out.println(m);

// 3.获取方法对象
Method method = clazz.getMethod(m, HttpServletRequest.class, HttpServletResponse.class);

// 4.让方法执行 返回值为请求转发的路径
String s=(String) method.invoke(this, request,response);//相当于 userservlet.add(request,response)

// 5.判断s是否为空
if(s!=null){
request.getRequestDispatcher(s).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}

}


public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return null;
}

}

上面的这段代码是BaseServlet,使它继承httpServlet,重写service方法

 

 

user类代码

package com.itheima.domain;

import java.util.Date;
/**
* 用户实体
* @author Administrator
*
*/
public class User {

private String uid;
private String username;
private String password;

private String name;
private String email;
private String telephone;

private Date birthday;
private String sex;
private Integer state=0;//激活状态 1:激活 0:未激活
private String code;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}




}

 

 

创建UserServlet 继承 BaseServlet

package com.itheima.web.servlet;

import java.io.IOException;
import java.sql.Date;

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

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

import com.itheima.domain.User;
import com.itheima.myconventer.MyConventer;
import com.itheima.service.iUserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.utils.UUIDUtils;

/**
* 和用户相关的方法
*/
public class UserServlet extends BaseServlet {

public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("userservlet的add方法执行了");
return null;
}
/**
* 跳转到注册页面
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String registerUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

return "jsp/register.jsp";
}
/**
* 用户注册
* @param request
* @param response
* @return
* @throws Exception
*/
public String regist(HttpServletRequest request, HttpServletResponse response) throws Exception {
//1.封装数据
User user = new User();

//注册自定义转化器
ConvertUtils.register(new MyConventer(), Date.class);
BeanUtils.populate(user, request.getParameterMap());

//1.1 设置用户id
user.setUid(UUIDUtils.getId());

//1.2 设置激活码
user.setCode(UUIDUtils.getCode());

//1.3加密密码
//user.setPassword(MD5Utils.md5(user.getPassword()));

//2.调用service完成注册
iUserService s=new UserServiceImpl();
s.regist(user);

//3.页面请求转发
request.setAttribute("msg", "用户注册已成功,请去邮箱激活~~");

return "/jsp/msg.jsp";
}

}

 

 

package com.itheima.service.impl;

import com.itheima.dao.iUserDao;
import com.itheima.dao.impl.UserDaoImpl;
import com.itheima.domain.User;
import com.itheima.service.iUserService;

/*
* 接口实现类
*/
public class UserServiceImpl implements iUserService{
/**
* 用户注册
*/
@Override
public void regist(User user) throws Exception{
iUserDao dao=new UserDaoImpl();
dao.add(user);
//发送激活邮件
}
}

 

 

dao层代码

package com.itheima.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import com.itheima.dao.iUserDao;
import com.itheima.domain.User;
import com.itheima.utils.DataSourceUtils;

/*
* 接口实现类
*/
public class UserDaoImpl implements iUserDao {
/**
* 添加用户
* @throws SQLException
*/
@Override
public void add(User user) throws SQLException {

//创建queryrunner对象
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());

//编写sql语句
String sql="insert into user values(?,?,?,?,?,?,?,?,?,?);";

//执行sql语句
qr.update(sql, user.getUid(),user.getUsername(),user.getPassword(),
user.getName(),user.getEmail(),user.getTelephone(),
user.getBirthday(),user.getSex(),user.getState(),user.getCode());
}

}

以上是这个注册这一阶段的全部代码,当我启动项目,在注册页面输入完信息后,点击提交时候出现三个异常提示,检查好久,怎么都发现不了问题所在   知道博客园的大牛比较多,希望能认识大牛,加上qq,以后方便指导,小弟感激不尽。。。。

IT小小笨猪的主页 IT小小笨猪 | 初学一级 | 园豆:194
提问于:2018-02-05 17:10

建议给代码加上高亮

dudu 2年前
< >
分享
最佳答案
0

//注册自定义转化器
ConvertUtils.register(new MyConventer(), Date.class); 这段代码出的问题

奖励园豆:5
魂心 | 初学一级 |园豆:147 | 2018-02-05 17:26

用这个 ConvertUtils.register(new DateLocaleConverter(), Date.class);  

魂心 | 园豆:147 (初学一级) | 2018-02-05 17:28

你好!很感谢你热心,帮 我解答,我刚刚按照你帮我改的代码添加上去,发现继续出现一样的异常,如果你方便的话,希望你能加我扣扣,帮我远程看下,着急死了。。。。。。我扣扣是1078553774

IT小小笨猪 | 园豆:194 (初学一级) | 2018-02-05 17:34

@IT小小笨猪: 看一下这个博客,https://www.cnblogs.com/fujilong/p/5562480.html

魂心 | 园豆:147 (初学一级) | 2018-02-05 18:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册