본문 바로가기

Java/jsp&servlet

[jsp] 회사 홈페이지 만들기 (6) - 글쓰기,댓글-

글쓰기도 심플심플

 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글작성</title>
<link rel="stylesheet" type="text/css" href="main_c.css">
<link rel="stylesheet" type="text/css" href="write.css">
</head>
<body>
<jsp:include page="nav_top2.jsp"/>
<form method="post" action="write_action2.jsp" >
        <table class="table2" align="center" cellpadding="2">

            <tr>
                <td>작성자</td>
                <td><input type="hidden" name="empno" value="<%=session.getAttribute("user_empno")%>"><%=session.getAttribute("user_name")%></td>

            </tr>

            <tr>
                <td>제목</td>
                <td><input type="text" name="title" size="148"></td>
            </tr>

            <tr>
                <td>내용</td>
                <td><textarea name="content" cols="139" rows="30"></textarea></td>
            </tr>

        </table>

        <center>
            <input class="button2" type="submit" value="글작성" />
            <input class="button2" type="reset" value="취소" /></td>
        </center>

    </form>
</body>
</html>

글쓰는 form 코오드

 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%
	Class.forName("oracle.jdbc.driver.OracleDriver");
	Connection conn = null; 
	Statement stmt = null;
    ResultSet rs = null;
    
    request.setCharacterEncoding("UTF-8");
		
		String title = request.getParameter("title");
		String empno = request.getParameter("empno");
		String content = request.getParameter("content");
		
		
		
		 try
      {
            String jdbcDriver = "jdbc:oracle:thin:@localhost:1521:ORCL";
            String dbUser = "C##LINAJUNG";
            String dbPass = "1234";
   
            String query = "insert into board_ann  (ann_num,title, empno, contents, ann_date) values(tmp_seq2.NEXTVAL, '" +title +"','"+empno+"','"+content+ "',sysdate)";
            conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            if(rs.next()) {
            	%><script type="text/javascript">
	            alert('글이 작성되었습니다.');
	            location.replace('board_ann.jsp?');
	        </script>
	        <% } else {
	        	%><script type="text/javascript">
            alert('글 작성에 실패하였습니다.');
            history.back();
        	</script>
	       <% }
      }catch(SQLException e){
          out.println(e.getMessage());
          e.printStackTrace();
    }finally{
          // 6. 사용한 Statement 종료
          if(rs != null) try { rs.close(); } catch(SQLException ex) {}
          if(stmt != null) try { stmt.close(); } catch(SQLException ex) {}
 
          // 7. 커넥션 종료
          if(conn != null) try { conn.close(); } catch(SQLException ex) {}
    }
	%>

form 데이터를 post로 받아와 db에 입력해주는 jsp

 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%
	Class.forName("oracle.jdbc.driver.OracleDriver");
	Connection conn = null; 
	Statement stmt = null;
    ResultSet rs = null;
    
    request.setCharacterEncoding("UTF-8");
		
		String num = request.getParameter("comment_ann_num");
		String empno = request.getParameter("comment_empno");
		String content = request.getParameter("comment");
		
		
		
		 try
      {
            String jdbcDriver = "jdbc:oracle:thin:@localhost:1521:ORCL";
            String dbUser = "C##LINAJUNG";
            String dbPass = "1234";
   
            String query = "insert into comments (com_num, ann_num, empno, com_contents, com_date) values(tmp_seq3.NEXTVAL,'"+num+"','"+empno+"','"+content+ "',sysdate)";
            conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            if(rs.next()) {
            	%><script type="text/javascript">
	            alert('댓글이 작성되었습니다.');
	            location.replace('view2.jsp?no=<%=num%>');
	        </script>
	        <% } else {
	        	%><script type="text/javascript">
            alert('댓글 작성에 실패하였습니다.');
            history.back();
        	</script>
	       <% }
      }catch(SQLException e){
          out.println(e.getMessage());
          e.printStackTrace();
    }finally{
          
          if(rs != null) try { rs.close(); } catch(SQLException ex) {}
          if(stmt != null) try { stmt.close(); } catch(SQLException ex) {}
 
          
          if(conn != null) try { conn.close(); } catch(SQLException ex) {}
    }
	%>

댓글 작성 jsp