본문 바로가기

Web/Frame Work

Model 1 ( 방명록 )

guestbook.jsp 

<?xml version="1.0" encoding="EUC-KR" ?>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<%@ page import="javax.sql.*,java.sql.*,gb.*,java.util.*"%>     ( import 역할을 해준다. )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR" />
<title>방명록</title>
</head>


<body>

 

<%-- 글작성할수 있는 Table 을 의미한다. --%>
 <form action ="gb_write.jsp" method="post">
 <table border="1" align="center" width="500">
 <tr>
  <td>글쓴이</td>
  <td><input type="text" name="write"/></td>
 </tr>
 
 <tr>
  <td colspan="2">
   <textarea rows="10" cols="10" name="content"></textarea>
  </td>
 </tr>
 <tr>
  <td colspan="2">
   <input type="submit" value="저장"/>
  </td>
 </tr>
 </table>
 </form>
 
<hr />


 <%-- 글 작성후에 저장을 하게되면 바로 <hr/> 태그 밑에 저장이 된다. --%>
 <table border="1" align="center" width="500">
 <%
  DataSource ds = (DataSource) application.getAttribute("ds");
  GbDao dao = new GbDao(ds); //객체 생성
  ArrayList<GbBean> list = dao.getList();
  
  for(GbBean bean : list) {
 %>  
  <tr>
   <td><%=bean.getNo() %></td>
   <td><%=bean.getWrite() %></td>
   <td><%=bean.getWdate() %></td>
   <!-- gb_del.jsp만들고 dao 메소드 호출해서 삭제가 되도록 -->
   <td><a href ="gb_del.jsp"?no=<%=bean.getNo() %>>삭제</a></td>
  </tr>  
  <tr>
   <td colspan="4"><%=bean.getContent() %></td>
  </tr>  
<%
}
%>
</table>
</body>
</html>

 

 

 

gb_write.jsp 

<?xml version="1.0" encoding="EUC-KR" ?>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR" />
<title>Insert title here</title>
</head>
<body>
 <!-- application 영역에서 DataSource 타입의 ds란 변수를 가져온다. -->
 <jsp:useBean id="ds" type="javax.sql.DataSource" scope="application"></jsp:useBean>

 <!-- scope="page" 가 기본값, 객체가 만들어져서 page 영역에 저장된다. -->


 <jsp:useBean id="dao" class="gb.GbDao">
  <jsp:setProperty property="ds" name="dao" value="<%=ds%>" />
 </jsp:useBean>


<!-- jsp:setProperty 태그는 특정 bean의 property(멤버변수)에 값을 세팅해주는 태그, 만약
useBean 태그 안쪽에 쓰이면 useBean에 의해서 객체가 만들어질때만 setProperty가 작동하고,
만들어지지 않고 객체를 가져오기만 할 경우에는 작동하지 않는다. -->


<!-- GbBean의 객체를 만들고 넘어오는 parameter와 이름이 같은
filed(멤버변수, property)에 값을 자동으로 세팅해준다. -->


<jsp:useBean id="bean" class="gb.GbBean"></jsp:useBean>
<jsp:setProperty property="*" name="bean"/>
<%=dao %>,<%=bean.getWrite() %>,<%=bean.getContent() %>


 <%
  String writer = request.getParameter("writer");
  String content = request.getParameter("content");
 %>

 

 <h1><%=writer%></h1>
 <h1><%=content%></h1>
 
<%
 dao.write(bean);
 response.sendRedirect("/pp/guestbook.jsp"); //response결과 값 출력후에 이쪽으로 이동!!

%>
</body>
</html> 

 

GbBean.java 

package gb;

 

//Data Bean( 데이터를 저장하기 위한 용도의 빈 ) 글 한건

 

public class GbBean {
 private String no;
 private String write;
 private String wdate;
 private String content;

 

 public GbBean() {

 

 }

 

 public GbBean(String no, String write, String wdate, String content) {
  super();
  this.no = no;
  this.write = write;
  this.wdate = wdate;
  this.content = content;
 }

 public String getNo() {
  return no;
 }

 

 public void setNo(String no) {
  this.no = no;
 }

 

 public String getWrite() {
  return write;
 }

 

 public void setWrite(String write) {
  this.write = write;
 }

 

 public String getWdate() {
  return wdate;
 }

 

 public void setWdate(String wdate) {
  this.wdate = wdate;
 }

 

 public String getContent() {
  return content;
 }

 

 public void setContent(String content) {
  this.content = content;
 }

 

 

 

GbDao.java 

package gb;

import java.sql.*;
import java.util.*;

import javax.sql.*;

import org.eclipse.jdt.internal.compiler.flow.*;

 

//DAO : Data Access Object ( 데이터 접근 객체 )


public class GbDao {


 private DataSource ds;                               //지역변수

 

 public GbDao() {

 

 }

 

 public GbDao(DataSource ds) {
  this.ds = ds;
 }

 

 public DataSource getDs() {
  return ds;
 }

 

 public void setDs(DataSource ds) {
  this.ds = ds;
 }

 

 public ArrayList<GbBean> getList() throws SQLException {

 

  ArrayList<GbBean> list = new ArrayList<>();
  String sql1 = "select * from guestbook";

  try (Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql1);) {

 

   while (rs.next()) {
             String no = rs.getString(1);
             String write = rs.getString(2);
             String wdate = rs.getString(3);
             String content = rs.getString(4);
             list.add(new GbBean(no, write, wdate, content));
   }


   return list;
  }
 }

 public void write(GbBean bean) throws SQLException {


  String sql = "insert into guestbook(no,write,content) values(seq_gb.nextval,?,?)";

 

  try (Connection conn = ds.getConnection();
       PreparedStatement pstmt = conn.prepareStatement(sql);) {

  

    String write = bean.getWrite();                    //bean에 있는 Write를 가져온다.
    String content = bean.getContent();            //bean에 있는 Content를 가져온다.

    

    pstmt.setString(1, write);
    pstmt.setString(2, content);

    pstmt.executeUpdate();
  }
 }

 

public void del(GbBean bean) throws SQLException{
  String sql = "delete from guestbook where no = ?";
  
  try(Connection conn = ds.getConnection();
   PreparedStatement pstmt = conn.prepareStatement(sql);){


   String no = bean.getNo();
   pstmt.setString(1, no);
   pstmt.executeUpdate();
  }
 }

 

gb_del.jsp 

 <?xml version="1.0" encoding="EUC-KR" ?>
<%@page import="java.util.ArrayList"%>
<%@page import="gb.GbDao"%>
<%@page import="javax.sql.DataSource"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR" />
<title>Insert title here</title>
</head>
<body>
  <jsp:useBean id="ds" type="javax.sql.DataSource" scope="application"></jsp:useBean>
 <jsp:useBean id="dao" class="gb.GbDao">
  <jsp:setProperty property="ds" name="dao" value="<%=ds%>" />
 </jsp:useBean>

<jsp:useBean id="bean" class="gb.GbBean"></jsp:useBean>
<jsp:setProperty property="*" name="bean"/>

 

<%


 request.getParameter("no");
 dao.del(bean);                                             //삭제 시켜주는 메서드
 response.sendRedirect("/pp/guestbook.jsp"); //response결과 값 출력후에 이쪽으로 이동!!


%>
</body>
</html>

 

 

'Web > Frame Work' 카테고리의 다른 글

resultClass resultMap  (0) 2013.01.23
MyBatis] 반복되는 쿼리 묶기 SQL, include 태그  (0) 2012.12.17
스트럿츠 돌아가는 구조  (0) 2012.11.14
Mode 1  (0) 2012.07.30