项目的包结构:
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/mydb1
root
root
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
用户注册
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);
}
}