servlet+mysql实现用户注册

项目的包结构:


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

使用到的相关jar包:


c3p0连接池配置文件:
<c3p0-config>
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb1</property>
    <property name="user">root</property>
    <property name="password">root</property>
  </default-config>
</c3p0-config>

mysql的user表

web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>servlet_regist</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<description></description>
		<display-name>RegistServlet</display-name>
		<servlet-name>RegistServlet</servlet-name>
		<servlet-class>blog.csdn.net.web.servlet.RegistServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>RegistServlet</servlet-name>
		<url-pattern>/regist</url-pattern>
	</servlet-mapping>
	<servlet>
		<description></description>
		<display-name>MsgServlet</display-name>
		<servlet-name>MsgServlet</servlet-name>
		<servlet-class>blog.csdn.net.web.servlet.MsgServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>MsgServlet</servlet-name>
		<url-pattern>/msg</url-pattern>
	</servlet-mapping>
</web-app>
index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body>

<form action="/servlet_regist/regist" method="post">
	用户名:<input type="text" name="username"/><br/>
	密码:<input type="password" name="password"/><br/>
	<input type="submit" value="注册"/><br/>
</form>

</body>
</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<String, String[]> 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);
	}

}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值