디지털 컨버전스/JSP

[JSP] output 기능

gimyeondong 2020. 4. 27. 17:47

OutputProc.jsp

<%@page import="kh.backend.dto.MessagesDTO"%>
<%@page import="java.util.List"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="kh.backend.dao.MessagesDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
	<table border=1 align=center>
		<tr>
			<td>글번호</td>
			<td>작성자</td>
			<td>글내용</td>
			<td>작성일</td>
		</tr>
		<%
		MessagesDAO dao = new MessagesDAO();
		List<MessagesDTO> list = dao.selectAll();
		for (MessagesDTO dto : list) {
		%>
		<tr>
			<td><%=dto.getSeq()%>
			<td><%=dto.getName()%>
			<td><%=dto.getMsg()%>
			<td><%=dto.getWrite_date()%>
		</tr>
		<%
			}
		%>
		<tr>
			<td colspan=4 align=center><button id="back">돌아가기</button>
		</tr>
	</table>
	<script>
			document.getElementById('back').onclick = function() {
				location.href = 'index.jsp';
			}
		</script>
</body>
</html>

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
	<fieldset>
		<legend>Index</legend>
		<button id="toInput">toInput</button>
		<button id="toOutput">toOutput</button>
	</fieldset>
	
	<script>
		$("#toInput").on("click",function(){
			location.href = "input.jsp";
		})
		$("#toOutput").on("click",function(){
			location.href = "OutputProc.jsp";
		})
	
	</script>
</body>
</html>
더보기

input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<style>
            fieldset{
                text-align: center;
            }
        </style>
    </head>
    <body>
        <form action="InputProc.jsp" method="post">
            <fieldset>
                <legend>Input</legend>
                <input type=text name=name placeholder="Input name"><br>
                <input type=text name=msg placeholder="Input msg"><br>
                <input type=submit>
            </fieldset>
        </form>
    </body>    
</html>

 

inputProc.jsp

<%@page import="kh.backend.dto.MessagesDTO"%>
<%@page import="kh.backend.dao.MessagesDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Input Process</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
	<%
	String name = request.getParameter("name");
	String msg = request.getParameter("msg");
	MessagesDAO dao = new MessagesDAO();
	int result = dao.insert(new MessagesDTO(0, name, msg, null));
	if (result > 0) {
	%>
	<script>
		alert("Input complete");
		location.href = "index.jsp";
	</script>
	<%
		} else {
	%>
	<script>
		alert("Input failed");
		location.href = "index.jsp";
	</script>
	<%
		}
	%>

</body>
</html>

 

MessagesDAO

package kh.backend.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import kh.backend.dto.MessagesDTO;

public class MessagesDAO {
	private Connection getConnection() throws Exception{
		String url = "jdbc:oracle:thin:@localhost:1521:xe";
		String id = "kh";
		String pw = "kh";
		Class.forName("oracle.jdbc.driver.OracleDriver");
		return DriverManager.getConnection(url,id,pw);
	}
	
	public int insert(MessagesDTO dto) throws Exception{
		String sql = "insert into messages values(messages_seq.nextval, ?,?,sysdate)";
		
		try(Connection con = this.getConnection();	
		PreparedStatement pstat = con.prepareStatement(sql)){
			pstat.setString(1, dto.getName());
			pstat.setString(2, dto.getMsg());
			int result = pstat.executeUpdate();
			con.commit();
			return result;
		}
	}
	public List<MessagesDTO> selectAll() throws Exception{
		String sql = "select * from messages";
		try(Connection con = this.getConnection();
			PreparedStatement pstat = con.prepareStatement(sql);
			ResultSet rs = pstat.executeQuery();){
			
			List<MessagesDTO> result = new ArrayList<>();
			while(rs.next()) {
				int seq = rs.getInt("seq");
				String name = rs.getString("name");
				String msg = rs.getString("msg");
				Timestamp write_date = rs.getTimestamp("write_date");
				MessagesDTO dto = new MessagesDTO(seq,name,msg,write_date);
				result.add(dto);
			}
			return result;
		}
	}
	
	public int delete(String seq) throws Exception {

		String sql = "delete from messages where seq=?";
		try(Connection con = this.getConnection();	
				PreparedStatement pstat = con.prepareStatement(sql);) {
			pstat.setString(1, seq);
			int result = pstat.executeUpdate();
			con.commit();
			return result;
		}
	}
	
	public int update(String seq, String name, String msg) throws Exception {

		String sql = 
				"update messages set name = ? , msg=?  where seq=?";
		try(Connection con = this.getConnection();	
				PreparedStatement pstat = con.prepareStatement(sql);) {
			pstat.setString(1, name);
			pstat.setString(2, msg);
			pstat.setString(3, seq);
			int result = pstat.executeUpdate();
			con.commit();
			return result;
		}
	}
}

 

MessagesDTO

package kh.backend.dto;

import java.sql.Timestamp;

public class MessagesDTO {
	private int seq;
	private String name;
	private String msg;
	private Timestamp write_date;
	
	public MessagesDTO() {}
	public MessagesDTO(int seq, String name, String msg, Timestamp write_date) {
		super();
		this.seq = seq;
		this.name = name;
		this.msg = msg;
		this.write_date = write_date;
	}
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Timestamp getWrite_date() {
		return write_date;
	}
	public void setWrite_date(Timestamp write_date) {
		this.write_date = write_date;
	}
	
	
}

 


파일 경로, 위치 확인하고 실행할것!