유효성 검사 대작전

주소API

회원가입을 시켜보자

JoinFormAction.java

public class JoinFormAction implements Action{

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("views/user/joinForm.jsp");
		// 경로 왜 views 부터 시작하나요?
		// -> sendRedirect 얘는 jsp 파일을 찾는다 -> 파일 찾을 때 디폴트(webapp)로 바로감
	}

}

joinForm.jsp

(부트스트랩 forms)

required="required" → 페이지에서 입력안하면 못 넘어감

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../layout/header.jsp"%>

<div class="container">

	<form action="${cp}/user?cmd=join" method="post">
		<div class="form-group">
			<input type="text" class="form-control" placeholder="Enter username" name="username" required="required"/>
		</div>
		<div class="form-group">
			<input type="password" class="form-control" placeholder="Enter password" name="password" required="required"/>
		</div>
		<div class="form-group">
			<input type="email" class="form-control" placeholder="Enter email" name="email" required="required"/>
		</div>
		<div class="form-group">
			<input type="text" class="form-control" placeholder="Enter address" name="address" required="required"/>
		</div>
		<button type="submit" class="btn btn-primary">회원가입</button>
	</form>

</div>

<%@ include file="../layout/footer.jsp"%>

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/461f40b2-2097-475c-9719-5ce63ac46a96/Untitled.png

joinAction.java

public class JoinAction implements Action {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1. 유효성 검사
		// ?username=ssar, ?username=, ? -> postman은 가능하므로 검증해야함
		if (request.getParameter("username") == null || request.getParameter("username").equals("")) {
			return; // void 함수도 return 가능
		} else if (request.getParameter("password") == null || request.getParameter("password").equals("")) {
			return;
		} else if (request.getParameter("email") == null || request.getParameter("email").equals("")) {
			return;
		} else if (request.getParameter("address") == null || request.getParameter("address").equals("")) {
			return;
		}
		
		// 2. http body 데이터 변수로 받아야 됨
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		
		User user = new User(null, username, password, email, address, null);
		// 완성해서 하나씩 집어넣어야하는데 시퀀스는 못 넣잖아?
		// 그래서 null 넣고 싶은데 id 타입 int 거든
		// 그래서 래퍼타입인 Integer로 바꿔줘
		
		// 3. DAO 연결해서 save() 하기
		// 4. result 받기
		UserDAO userDAO = new UserDAO();
		int result = userDAO.save(user);
		
		// 5. 1일때, 1아닐때를 분기해서  1-> 로그인페이지(sendR) / !1-> joinForm페이지로 보내기
		if (result == 1) {
			System.out.println("회원가입 성공 DB 확인");
		} else {
			System.out.println("회원가입 실패 콘솔에서 오류를 확인하세요");
		}
	}
	
}

UserDAO.java

// post
	public int save(User user) {

		String sql = "INSERT INTO users(id, username, password, email, address, created) "
											+ "VALUES(users_seq.nextval,?,?,?,?,SYSDATE)";

		try {
			Connection conn = DBConn.dbConn();
			
			PreparedStatement pstmt = conn.prepareStatement(sql); // 프로토콜이 적용된 버퍼
			
			pstmt.setString(1, user.getUsername());
			pstmt.setString(2, user.getPassword());
			pstmt.setString(3, user.getEmail());
			pstmt.setString(4, user.getAddress());
			

			return pstmt.executeUpdate(); // 변경된 행의 갯수 리턴
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;

	}