728x90
반응형
2. JSP구조
글쓰기(insert), 수정(update), 삭제(delete), 전체목록(list), 선택게시글 내용(select), 목록으로 돌아가기 등
게시판이 갖는 기본적인 기능을 구조적으로 표현하면 아래와 같다.
1) index
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="mylist.jsp">list...</a>
</body>
</html>
2) mylist.jsp
<%@page import="com.my.dao.MyBoardDao"%>
<%@page import="com.my.dto.MyBoardDto"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- encoding: 암호화 decoding: 복호화 -->
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!-- text로 되어있지만 html로 받을 거고 charset을 UTF-8로 하겠다. -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
tr:nth-child(even){background: skyblue;}
tr:last-child{background: white;}
</style>
</head>
<body>
<%
//java 영역(script leap?)
MyBoardDao dao = new MyBoardDao();
List<MyBoardDto> list = dao.selectList();
%>
<h1>List</h1>
<table style="border: 0.5px solid gray">
<col width="50px"/>
<col width="100px"/>
<col width="300px"/>
<col width="100px"/>
<tr>
<th>번호</th>
<th>이름</th>
<th>제목</th>
<th>날짜</th>
</tr>
<%
for(int i = 0; i<list.size(); i++){
%> <!-- 요청 후 jsp파일이 컴파일 될 때 아래 값들이 들어간다. -->
<!-- 목록만 만들어주고 보여주기 때문에 content는 제외 -->
<!-- java영역 내용을 html 안으로 넣어줄 때 세미콜론(;)대신 %=사용 -->
<!-- List의 size만큼 tr이 만들어 진다. -->
<!-- 자바영역 안에 공백은 공백인코딩값이 들어가지기 때문에 절대 사용하면 안된다. -->
<tr>
<td><%= list.get(i).getMyno() %></td>
<td><%= list.get(i).getMyname() %></td>
<td><a href="myselect.jsp?myno=<%=list.get(i).getMyno()%>"><%= list.get(i).getMytitle() %></a></td>
<td><%= list.get(i).getMydate() %></td>
</tr>
<%
}
%>
<tr>
<td colspan="4" align="right">
<input type="button" onclick="location.href='myinsert.jsp'" value="글쓰기"/>
</td>
</tr>
</table>
</body>
</html>
3) myselect.jsp
<%@page import="com.my.dto.MyBoardDto"%>
<%@page import="com.my.dao.MyBoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
th{background: skyblue;}
td{background: skyblue;}
</style>
</head>
<body>
<%
int myno = Integer.parseInt(request.getParameter("myno"));
MyBoardDao dao = new MyBoardDao();
MyBoardDto dto = dao.selectOne(myno);
%>
<h1>Select</h1>
<table>
<tr>
<th>이름</th>
<td><%=dto.getMyname() %></td>
</tr>
<tr>
<th>제목</th>
<td><%=dto.getMytitle() %></td>
</tr>
<tr>
<th>내용</th>
<td>
<textarea rows="10" cols="60" readonly="readonly"><%=dto.getMycontent() %></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" onclick="location.href='myupdate.jsp?myno=<%=dto.getMyno() %>'" value="수정"/>
<input type="button" onclick="deleteCheck();" value="삭제"/>
<input type="button" onclick="location.href='mylist.jsp'" value="목록"/>
</td>
</tr>
</table>
<script type="text/javascript">
function deleteCheck(){
if(confirm("정말 삭제하시겠습니까?") ==true) {
location.href="mydelete.jsp?myno=<%=dto.getMyno() %>";
} else {
alert("뒤로 돌아가려면 목록을 눌러주세요.")
location.href="myselect.jsp?myno=<%=dto.getMyno() %>";
}
}
</script>
</body>
</html>
4) myupdate.jsp
<%@page import="com.my.dto.MyBoardDto"%>
<%@page import="com.my.dao.MyBoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int myno = Integer.parseInt(request.getParameter("myno"));
MyBoardDao dao = new MyBoardDao();
MyBoardDto dto = dao.selectOne(myno);
%>
<form action="myupdateres.jsp?myno=<%=myno %>" method="post">
<!-- <input type="hidden" name="myno" value="myno"/> -->
<table border="1">
<tr>
<th>이름</th>
<td><input type="text" name="myname" value="<%=dto.getMyname() %>" readonly="readonly"/></td>
</tr>
<tr>
<th>제목</th>
<td><input type="text" name="mytitle" value="<%=dto.getMytitle() %>"/></td>
</tr>
<tr>
<th>내용</th>
<td><textarea rows="10" cols="60" name="mycontent"><%=dto.getMycontent() %></textarea></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="취소" onclick="location.href='mylist.jsp'"/>
<input type="submit" value="수정"/>
</td>
</tr>
</table>
</form>
</body>
</html>
5) myupdateres.jsp
<%@page import="com.my.dto.MyBoardDto"%>
<%@page import="com.my.dao.MyBoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int myno = Integer.parseInt(request.getParameter("myno"));
//String myname = request.getParameter("myname");
String mytitle = request.getParameter("mytitle");
String mycontent = request.getParameter("mycontent");
MyBoardDto dto = new MyBoardDto();
dto.setMyno(myno);
//dto.setMyname(myname);
dto.setMytitle(mytitle);
dto.setMycontent(mycontent);
MyBoardDao dao = new MyBoardDao();
int res = dao.update(dto);
if(res>0){
%>
<script type="text/javascript">
alert("글 수정 성공");
location.href="mylist.jsp";
</script>
<%
} else {
%>
<script type="text/javascript">
alert("글 수정 실패");
location.href="myupdate.jsp?myno=<%=myno%>";
</script>
<%
}
%>
</body>
</html>
6) mydelete.jsp
<%@page import="com.my.dto.MyBoardDto"%>
<%@page import="com.my.dao.MyBoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int myno = Integer.parseInt(request.getParameter("myno"));
MyBoardDao dao = new MyBoardDao();
int res = dao.delete(myno);
if(res>0){
%>
<script type="text/javascript">
alert("글 삭제 성공");
location.href="mylist.jsp";
</script>
<%
} else {
%>
<script type="text/javascript">
alert("글 작성 실패");
location.href="myselect.jsp?myno=<%=myno%>";
</script>
<%
}
%>
</body>
</html>
7) myinsert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>insert</h1>
<!-- get방식은 사이즈가 정해져 있기 때문에 정보량이 많으면 오류날수 있으니 무조건 post로! -->
<form action="myinsertres.jsp" method="post">
<table border="1">
<tr>
<th>이름</th>
<td><input type="text" name="myname"/></td> <!-- name=key, 값=value -->
</tr>
<tr>
<th>제목</th>
<td><input type="text" name="mytitle"/></td>
</tr>
<tr>
<th>내용</th>
<td><textarea rows="10" cols="60" name="mycontent"></textarea> </td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="button" value="취소" onclick="location.href='mylist.jsp'"/>
<input type="submit" value="작성"/>
</td>
</tr>
</table>
</form>
</body>
</html>
8) myinsertres.jsp
<%@page import="com.my.dto.MyBoardDto"%>
<%@page import="com.my.dao.MyBoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<% response.setContentType("text/html; charset=UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//request : 요청했다는 의미 + 요청한다는 객체
String myname = request.getParameter("myname"); //getParameter는 form에서 name(key)를 요청할 수 있다.
String mytitle = request.getParameter("mytitle");
String mycontent = request.getParameter("mycontent");
MyBoardDto dto = new MyBoardDto();
dto.setMyname(myname);
dto.setMytitle(mytitle);
dto.setMycontent(mycontent);
MyBoardDao dao = new MyBoardDao();
int res = dao.insert(dto);
if(res>0){
%>
<script type="text/javascript">
alert("글 작성 성공");
location.href="mylist.jsp";
</script>
<%
} else {
%>
<script type="text/javascript">
alert("글 작성 실패");
location.href="myinsert.jsp";
</script>
<%
}
%>
</body>
</html>
728x90
반응형
'Web > Jsp_servlet' 카테고리의 다른 글
[JSP]MVC2게시판만들기_02 (0) | 2020.08.12 |
---|---|
[JSP]MVC2게시판만들기_01 (0) | 2020.08.12 |
[JSP]MVC게시판 만들기_01 (0) | 2020.08.06 |
[JSP]JSP/Servlet이란 (0) | 2020.08.05 |
[java.util.Date]와 [java.sql.Date]차이 (0) | 2020.08.05 |