package cn.edu.xaut.servlet;
import cn.edu.xaut.bean.UserBean;
import cn.edu.xaut.dao.UserDao;
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.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
@WebServlet(name = "RegisterServlet", urlPatterns = {"/RegisterServlet"})
public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理字符编码
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//获取表单参数
String username = request.getParameter("username");
String email = request.getParameter("email");
String pwd = request.getParameter("pwd");
//创建JavaBean
UserBean user = new UserBean();
user.setName(username);
user.setEmail(email);
user.setPassword(pwd);
user.setRegisterDate(new Date(System.currentTimeMillis()));
//使用userDao插入数据
UserDao userDao = new UserDao();
try {
userDao.insertUser(user);
} catch (Exception e) {
throw new RuntimeException(e);
}
// 跳转到registerSuccess.jsp进行显示
request.setAttribute("userbean", user);
request.getRequestDispatcher("registerSuccess.jsp").forward(request,response);
}
}
标签:示例,request,response,user,import,servlet,javax
From: https://www.cnblogs.com/cloud-2-jane/p/18562832