本帖最后由 CrazyProgram 于 2013-4-27 22:38 编辑
- package zhiwei.deng.web.bbs.control;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Enumeration;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import zhiwei.deng.web.bbs.domain.User;
- import zhiwei.deng.web.bbs.form.RegisterForm;
- import zhiwei.deng.web.bbs.service.UserService;
- import com.sun.org.apache.commons.beanutils.BeanUtils;
- public class RegisterServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
- doPost(request,response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- Enumeration<String> enums = request.getParameterNames();
- User user = new User();
- while(enums.hasMoreElements()){
- String name = enums.nextElement();
- String[] values = request.getParameterValues(name);
- System.out.println(name);//username的参数
- System.out.println(request.getParameter(name));//这个显示乱码了é????????
- try {
- BeanUtils.setProperty(user, name, values);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- //进入数据格式校验
- RegisterForm registerForm = new RegisterForm();
- boolean flag = registerForm.validate(user);
- if(flag){
- //校验通过
- try {
- UserService userService = new UserService();
- userService.addUser();
- request.setAttribute("message", "用户注册成功");
- request.getRequestDispatcher("/WEB-INF/jsp/message.jsp").forward(request, response);
- }catch (Exception e) {
- e.printStackTrace();
- request.setAttribute("message", "用户注册失败");
- request.getRequestDispatcher("/WEB-INF/jsp/message.jsp").forward(request, response);
- }
- request.getRequestDispatcher("/WEB-INF/jsp/message.jsp").forward(request, response);
- }else{
- request.setAttribute("registerForm", registerForm);
- request.getRequestDispatcher("/WEB-INF/jsp/register.jsp").forward(request, response);
- }
- }
- }
复制代码 |