CreateUserServlet
package com.slipp.user;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.RequestDispatcher;
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 javax.validation.ConstraintViolation;
import javax.validation.Validator;
import com.slipp.db.Database;
import com.slipp.support.MyValidatorFactory;
@WebServlet("/users/save")
public class CreateUserServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userId=request.getParameter("userId");
String password=request.getParameter("password");
String name=request.getParameter("name");
String email=request.getParameter("email");
User user=new User(userId, password, name, email);
Validator validator=MyValidatorFactory.createValidaor();
Set<ConstraintViolation<User>> constraintViolations=validator.validate(user);
if(constraintViolations.size() >0){
String errorMessage=constraintViolations.iterator().next().getMessage();
String page="/form.jsp";
System.out.println("회원 가입 에러");
forwordJSP(request, response, errorMessage, page);
return;
}else {
System.out.println("회원 가입 성공");
}
Iterator<ConstraintViolation<User>> iterator =constraintViolations.iterator();
while(iterator.hasNext()){
ConstraintViolation<User> each=iterator.next();
}
//Database.addUser(user);
UserDAO dao =new UserDAO();
dao.insert(user);
response.sendRedirect("/");
}
private void forwordJSP(HttpServletRequest request, HttpServletResponse response,
String errorMessage, String page) throws ServletException, IOException{
request.setAttribute("errorMessage", errorMessage);
RequestDispatcher rd=request.getRequestDispatcher(page);
rd.forward(request, response);
}
}
댓글 ( 4)
댓글 남기기