본문 바로가기
IT/JSP Practice

[JSP Practice] - JSP 게시판 CRUD(글쓰기, 읽기, 수정, 삭제) Model 1 방식

by 차이나는 개발자 2020. 7. 24.
728x90
반응형

※JSP 게시판 CRUD(글쓰기, 읽기, 수정, 삭제) Model 1 방식

-data => application 

 

 

-글작성 화면입니다.

 

 

-글목록 화면입니다.

 

 

-상세보기 화면입니다.

 

 

#write.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>write.jsp</title>
</head>
<body>
	<form action="addArticle.jsp" method="post">
		제목 : <input type="text" name="title" placeholder="제목" />
		<br/>
		글쓴이 : <input type="text" name="writer" placeholder="작성자"  />
		<br/>
		비밀번호 : <input type="password" name="pw" placeholder="비밀번호" />
		<br/>
		내용
		<br/>
		<textarea name="contents" rows="5" cols="50" ></textarea>
		<br/>
		<input type="submit" />&nbsp;
		<input type="reset" />
	</form>
</body>
</html>

 

 

#addArticle.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ page import="kr.ac.green.Article" %>
    <%@ page import="java.util.Vector" %>
<%
	request.setCharacterEncoding("euc_kr");

	String title = request.getParameter("title");
	String writer = request.getParameter("writer");
	String contents = request.getParameter("contents");
	String pw = request.getParameter("pw");
	
	Article article = new Article(title, writer, contents, pw);
	
	Vector<Article> list = (Vector<Article>)application.getAttribute("list");
	if(list == null){
		list = new Vector<Article>();
		application.setAttribute("list", list);
	}
	
	list.add(article);
	
	response.sendRedirect("list.jsp");

%>

 

 

#list.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ page import="java.util.Vector" %>
    <%@ page import="kr.ac.green.Article" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>list.jsp</title>
</head>
<body>
<%
	// 기본 메시지
	String msg = "어서오세요";
	// delete에서 세션으로 받아옴
	Object o = session.getAttribute("msg");
	// 있으면 실패 메시지
	if(o != null){
		msg = o.toString();
	}
	// 기본 세션으로 받아온 메시지 제거
	session.removeAttribute("msg");
%>
<div>message : <%= msg %></div>
<a href="write.jsp">글 등록</a>
<hr>
<table>
	<tr>
		<th>번호</th>
		<th>제목</th>
		<th>글쓴이</th>
		<th>작성일</th>
	</tr>
	<%
		Vector<Article> list = (Vector<Article>)application.getAttribute("list");
	
		if(list == null || list.size() == 0){
			
	%>
	<tr>
		<th colspan="4">등록된 글이 없습니다.</th>
	</tr>
	<%
		} else {
			for(int i = list.size()-1; i>=0; i--){
				Article temp = list.get(i);
	%>
	<!-- onclick 이줄을 선택하면 -->
	<tr onclick="select(<%=temp.getNum()%>)">
		<td><%= temp.getNum() %></td>
		<td><%= temp.getTitle() %></td>
		<td><%= temp.getWriter() %></td>
		<td><%= temp.getDateString() %></td>
	</tr>
	<%
			} 
		}
	%>
</table>
<form id="hiddenForm" name="hiddenForm" action="readArticle.jsp">
	<input type="hidden" name="num" />
</form>
<script>

// 	function select(num){
// 		var myForm = document.getElementById("hiddenForm");
// 		myForm.num.value = num;
// 		myForm.submit();
// 	}

	function select(num){
// 		var myForm = document.forms["hiddenForm"]
		var myForm = document.hiddenForm;
		myForm.num.value = num;
		myForm.submit();
	}	

// 	alert("1" == 1);
// 	alert("2" === 2);

	// 매개변수가 없다고 해서 없는것이 아님(자바스크립트는 선택사항)
// 	function select(num){
// 		location.href="readArticle.jsp?num=" + num;
// 	}	
</script>
</body>
</html>

 

 

#readArticle.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ page import="java.util.Vector" %>
    <%@ page import="kr.ac.green.Article" %>
    <%
    	int num = Integer.parseInt(request.getParameter("num"));
    
    	Vector<Article> list = (Vector<Article>)application.getAttribute("list");
    	Article article = list.get(list.indexOf(new Article(num)));
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>readArticle.jsp</title>
</head>
<body>
		<form id="modifyForm" method="post">
		번호 : <input type="text" name="num" value="<%= article.getNum() %>" readonly />
		<br/>
		제목 : <input type="text" name="title" value="<%= article.getTitle() %>"/>
		<br/>
		글쓴이 : <input type="text" name="writer" value="<%= article.getWriter() %>"  />
		<br/>
		작성일 : <input type="text" name="date" value="<%= article.getDateString() %>" /> 
		<br/>
		비밀번호 : <input type="password" name="pw"  />
		<br/>
		내용
		<br/>
		<textarea name="contents" rows="5" cols="50" ><%= article.getContents() %></textarea>
		<br/>
		<input type="button" value="목록보기" onclick="goList()"/>&nbsp;
		<input type="button" value="수정" onclick="todo('update')"/>&nbsp;
		<input type="button" value="삭제" onclick="todo('delete')"/>&nbsp;
		<input type="reset" />
	</form>
	<script>
		function goList(){
			location.href = "list.jsp";
		}
		
		function todo(){
			var what = arguments[0];
			
			// modifyForm을 가진 id값을 가져와서 action속성을 >>
			var modifyForm = document.getElementById("modifyForm");
			modifyForm.action = what + ".jsp";
			
			modifyForm.submit();
		}
	</script>
</body>
</html>

 

 

#update.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<%@ page import="java.util.*"%>
<%@ page import="kr.ac.green.Article"%>
<%
	request.setCharacterEncoding("euc_kr");

	int num = Integer.parseInt(request.getParameter("num"));
	String title = request.getParameter("title");
	String contents = request.getParameter("contents");
	String pw = request.getParameter("pw");

	Vector<Article> list = (Vector<Article>) application.getAttribute("list");

	Article original = list.get(list.indexOf(new Article(num)));

	String msg = "수정이 완료되었습니다.";
	if (pw.equals(original.getPw())) {
		original.setTitle(title);
		original.setContents(contents);
	} else {
		msg = "비밀번호가 일치하지 않습니다.";
	}

	session.setAttribute("msg", msg);
	response.sendRedirect("list.jsp");
%>

 

 

#delete.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ page import="java.util.*" %>
    <%@ page import="kr.ac.green.Article" %>
<%
	int num = Integer.parseInt(request.getParameter("num"));

	String pw = request.getParameter("pw");

	Vector<Article> list = (Vector<Article>) application.getAttribute("list");
	
	Article temp = new Article(num);
	
	Article original = list.get(list.indexOf(temp));

	String msg = "삭제가 완료되었습니다.";
	if(pw.equals(original.getPw())){
		list.remove(temp);
	}else{
		msg ="비밀번호가 일치하지 않습니다.";
	}
	// 세션으로 보냄
	session.setAttribute("msg", msg);
	response.sendRedirect("list.jsp");
%>		
	
	
	

 

#Article.java

import java.text.SimpleDateFormat;
import java.util.Date;

public class Article {
	private static int count = 0;
	
	private int num;
	private String title;
	private String writer;
	private String contents;
	private String pw;
	private Date date;
	 
	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy.MM.dd");
	
	public Article(int num){
		this.num = num;
	}
	
	// 글 작성시 사용
	public Article(String title, String writer, String contents, String pw) {
		super();
		this.title = title;
		this.writer = writer;
		this.contents = contents;
		this.pw = pw;
		this.num = ++count;
		this.date = new Date();
	}

	public String getDateString(){
		return sdf.format(date);
	}
	
	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getWriter() {
		return writer;
	}

	public void setWriter(String writer) {
		this.writer = writer;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	
	// 식별자를 num으로 지정
	@Override
	public boolean equals(Object o){
		if(o == null || !(o instanceof Article)){
			return false;
		}
		Article other = (Article)o;
		return num == other.getNum();
	}
	
}

 

 

 

728x90
반응형

댓글