JSP

[211118] jsp servlet mvc 흐름, 방명록 fin, 회원가입(수정전)

감자탈출기 2021. 11. 19. 16:44

[15:50~]

 

 

방명록 목록 띄우기

5번 검색 요청으로 (/guest 요청) -> 5번~11번 흐름으로 작성

                          ㄴ데이터 수정, 추가, 삭제 : post 요청

                          ㄴ단순 검색 요청, 일반 페이지(url 주소) 요청 : get 요청 (pick!)

 

 

 

모델 선정 (service, dao, dto)

DTO(전달객체) : db의 테이블 구조와 유사
Service(로직) : Controller로부터 전달받은 데이터로 동작하게 됨 

DAO(연결) : 실제 db에 저장할 수 있게

 

DTO, Service (pick!)

         ㄴ 로직 짤 때 DAO 연결 (수정 삭제 등 해야 하니까)

 

방명록 --

 

 


 

회원가입

 

 

/webapp/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Random" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>메인 페이지</title>
</head>
<body>
	<h1>메인 페이지 입니다.</h1>
	<ul>
		<li><a href="/lotto">로또번호 생성 페이지</a></li>
		<li><a href="/guest">방명록 작성 페이지</a></li>
		<li><a href="/join">회원가입 페이지</a></li>
	</ul><!-- index 파일 알아서 찾아서 실행하므로 폴더만 지정해준 것 -->
	<!-- 이름 다르게 지정한 경우 /lotto/sample.jsp 이런 식으로 작성해야 함 -->
	<%-- 주석(comment) : 스크립트 태그의 주석을 설정하기 위해 사용-> 
	스크립트 태그 -> <% %>
	         스크립틀릿(Scriptlet) : 자바 로직코드를 작성하는데 사용 -> <% %>
	         표현식(expression) : 변수, 계산식, 메서드 호출 결과를 출력(HTML)할 때 사용 -> <%= %>
	         선언문(declaration) : 멤버변수 또는 메서드를 정의하는데 사용-> <%! %>
	         지시문(directive) : JSP 페이지의 인코딩 설정, 동작언어 설정 또는 자바 라이브러리를 추가(ex.import)하기 위해 사용-> <%@ %>
	--%>
	<%!
		String name = "Java";
		Random rand = new Random();
	%>
	
	<h2><%=name %></h2>
	
	<%
		int num = rand.nextInt(10); //0~9 까지 정수 랜덤 발생
		for(int i = 0; i <= num; i++){ //i = 0~(0~9)까지 Java 찍힘(최대 10번)
			out.print(name);
			out.print("<br>");
		}	
	%>
	
	<hr>
	
	<%
		out.print(num);
	%>
</body>
</html>

 

src/webapp/WEB-INF/jsp/account/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*"
         import="com.web.account.model.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입</title>
<script type="text/javascript" src="/static/js/join.js"></script>
<link type="text/css" rel="stylesheet" href="/static/css/join.css">
</head>
<body>
	<h2>회원가입 하기</h2>
	<form id="join_form" action="./join" method="post">
		<div>
			<label>계정명</label>
			<input type="text" name="username" placeholder="계정 이름" value= "<%=request.getParameter("username") %>" required>
		</div>	
		<div>
			<label>패스워드</label>
			<input type="password" name="password" placeholder="패스워드" value = "<%=request.getParameter("password") %>" required>
		</div>
		<div>
			<label>패스워드 확인</label>
			<input type="password" name="password_check" placeholder="패스워드 확인" value = "<%=request.getParameter("password_check") %>" required>
		</div>
		<input type="email" name="email" placeholder="이메일 주소" value = "<%=request.getParameter("password_check") %>" required>
		<button type="submit">전송</button>
	</form>
</body>
</html>

 

 

src/main/java/com.web.account.controller - JoinController.java

package com.web.account.controller;

import java.io.IOException;
import java.util.List;

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 com.web.account.model.AccountService;
import com.web.guest.model.GuestDTO;
import com.web.guest.model.GuestService;
import com.web.account.model.AccountDTO;

@WebServlet("/join")
public class JoinController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String view = "/WEB-INF/jsp/account/index.jsp";
		RequestDispatcher rp = request.getRequestDispatcher(view);
		rp.forward(request, response);
			
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		//System.out.println("동작 확인");//콘솔로 보기
		String username = request.getParameter("username");  //사용자입력값을 가져오는 메서드
		String password = request.getParameter("password");
		String password_check = request.getParameter("password_check");
		String email = request.getParameter("email");
//		System.out.println("username : " + username); //확인용
//		System.out.println("password : " + password);
//		System.out.println("password_check : " + password_check);
//		System.out.println("email : " + email);
		
		AccountDTO dto = new AccountDTO(username, password, email);
		AccountService service = new AccountService();
		if(service.isValid()) {
			//유효성 검사 이상 없음
		} else {
			//유효성 검사 이상 있음
		}
	}
}

 

src/main/java/com.web.account.model - AccountDTO.java

package com.web.account.model;

import java.sql.Date; //sql로 임포트

public class AccountDTO { //테이블 구조와 유사하게 작성 (편의를 위해 id)
	private int id;             //Column Name : ID
	private String username;    //Column Name : USERNAME
	private String password;    //Column Name : PASSWORD
	private String email;       //Column Name : EMAIL
	private Date JoinDate;      //Column Name : JOINDATE
	
	public AccountDTO() {}
	
	public AccountDTO(String username, String password, String email) {
		this.username = username;
		this.password = password;
		this.email = email;
	}
	
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public Date getJoinDate() {
		return JoinDate;
	}
	
	public void setJoinDate(Date joinDate) {
		JoinDate = joinDate;
	}
	
}

src/main/java/com.web.account.model - AccountService.java

package com.web.account.model;

public class AccountService {

}