servlet+mysql实现用户注册

项目的包结构:

servlet+mysql实现用户注册_第1张图片

User.java:用于封装数据库中的字段的实体,本例中会使用beanutils来完成bean的封装操作.
UserDao.java:用于操作数据库,本例中会使用dbutils来完成mysql的操作
UserService.java:用于处理相关的业务逻辑
RegistServlet.java:完成用户注册功能的servlet
MsgServlet.java:显示注册成功与失败的servlet
c3p0-config.xml:c3p0连接池的配置文件

使用到的相关jar包:
servlet+mysql实现用户注册_第2张图片

c3p0连接池配置文件:

  
    com.mysql.jdbc.Driver
    jdbc:mysql://127.0.0.1:3306/mydb1
    root
    root
  

mysql的user表

servlet+mysql实现用户注册_第3张图片

web.xml文件


	servlet_regist
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	
	
		
		RegistServlet
		RegistServlet
		blog.csdn.net.web.servlet.RegistServlet
	
	
		RegistServlet
		/regist
	
	
		
		MsgServlet
		MsgServlet
		blog.csdn.net.web.servlet.MsgServlet
	
	
		MsgServlet
		/msg
	
index.html文件




用户注册



用户名:
密码:

各模块java文件分别如下:
package blog.csdn.net.domain;

public class User {
	public int id;
	public String username;
	public String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	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;
	}
	
}
package blog.csdn.net.web.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import blog.csdn.net.domain.User;

public class UserDao {

	QueryRunner mQueryRunner = new QueryRunner(new ComboPooledDataSource());
	
	public boolean addUser(User user) throws SQLException {
		String sql = "insert into user(username,password) values(?,?)";
		int update = mQueryRunner.update(sql,user.username,user.password);
		return update==1;
	}
}
package blog.csdn.net.web.service;

import java.sql.SQLException;

import blog.csdn.net.domain.User;
import blog.csdn.net.web.dao.UserDao;

public class UserService {
	public boolean regist(User user) throws SQLException {
		return new UserDao().addUser(user);
	}
}
package blog.csdn.net.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

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 blog.csdn.net.domain.User;
import blog.csdn.net.web.service.UserService;

/**
 * 用户注册
 */
public class RegistServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取所有的请求参数
		Map params = request.getParameterMap();
		User user = new User();
		try {
			//通过BeanUtils封装参数生成bean
			BeanUtils.populate(user, params);
			
			//注册用户
			UserService service = new UserService();
			boolean success= service.regist(user);
			
			//通过request域传递信息
			request.setAttribute("result", success?"注册成功":"注册失败");
			//转发到另一个页面展示结果
			request.getRequestDispatcher("/msg").forward(request, response);
		} catch (Exception e) {
			if(e.getMessage().contains("Duplicate entry")) {
				request.setAttribute("result","用户已存在,注册失败");
				request.getRequestDispatcher("/msg").forward(request, response);
			}
			
		}
	}

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

}
package blog.csdn.net.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MsgServlet
 */
public class MsgServlet extends HttpServlet {
	

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String msg = (String) request.getAttribute("result");
		response.getWriter().println(msg);
	}

	/**
	 * @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);
	}

}










你可能感兴趣的:(Servlet)