본문 바로가기

jsp 프로젝트 V2 post

JSP게시판 만들기 6강 - 회원가입

1. include/nav.jsp만들기

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<nav>
	<c:choose>
		<c:when test="${empty sessionScope.principal}">	
			<a href="/apple/member?cmd=loginForm">로그인</a>
			<a href="/apple/member?cmd=joinForm">회원가입</a>
		</c:when>
		<c:otherwise>
			<a href="/apple/member?cmd=updateForm">회원정보</a>
			<a href="/apple/member?cmd=logout">로그아웃</a>		
		</c:otherwise>
	</c:choose>
</nav>

 

2. 모든 jsp(view) 페이지 수정

<nav></nav> 부분에 아래 문구 삽입하기

<%@include file="../include/nav.jsp" %>

 

3. MemberDao.java

package com.cos.apple.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.cos.apple.db.DBConn;

public class MemberDao {
	
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	public int 회원가입(String username, String password, String email) {
		try {
			String sql = "INSERT INTO member(id, username, password, email, createDate) VALUES(member_seq.nextval, ?,?,?, sysdate)";
			conn = DBConn.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, password);
			pstmt.setString(3, email);
			return pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
}

 

 

4. MemberJoinProc.java

package com.cos.apple.action.member;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cos.apple.action.Action;
import com.cos.apple.dao.MemberDao;

public class MemberJoinProcAction implements Action{
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		
		MemberDao memberDao = new MemberDao();
		int result = memberDao.회원가입(username, password, email);
		
		if(result == 1) {
			response.sendRedirect("member/loginForm.jsp");
		}else {
			response.sendRedirect("member/joinForm.jsp");
		}
	}
}