JSP

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);
	}
	

}

 

 

 

 

 

 

 

jsp

 

about author

PHRASE

Level 60  머나먼나라

우리들 성인(成人)들이 청년들에게 가르쳐야 할 처세에 대한 지식은 현실을 너희들의 이상(理想)에 살아야 하며 생활에 뺏기지 않도록 하라는 것이다. -슈바이처

댓글 ( 4)

댓글 남기기

작성