[JSP] 댓글 기능 구현

728x90

view.jsp(댓글 기능부분 발췌)

<script>
	function comment_action(){
		if(tb_comment.value == ""){
			alert("내용을 입력하세요");
			return false;
		}
		
	}
</script>
<%if(session_name != null && session_name != ""){ %>
<form action = "comment_insert.jsp" method = "post" onsubmit="return comment_action()">
<input type="hidden" name = "tb_table" value = "<%=code %>">
<input type = "hidden" name = "tb_uid" value = "<%=uid %>">
<table width = 1100 align="center" border=0>
	<tr>
		<td colspan=3 align="left">댓글</td>
	</tr>
	<tr>
		<td width=300><%=session_name %></td>
		<td width=700><input id="tb_comment" name = "tb_comment" style="width:98%;height:20px"></td>
		<td align=right width=100 ><button style="width:50px;height:30px">작성</button></td>
		
	</tr>
</table>
</form>
<%} %>
<% 
String sql_comment = "select * from comment where tb_table = '"+code+"' and tb_uid = "+uid+" order by uid desc";

Connection con_comment = DriverManager.getConnection(url, user, password);
Statement stmt_comment = con_comment.createStatement();
ResultSet rs_comment = stmt_comment.executeQuery(sql_comment);

%>
<table>
	<tr>
		<td height=15></td>
	</tr>
</table>

<table width= 1100 align="center" border=0>
	
	<%
				
		while(rs_comment.next()){ 
		
		String tb_date = rs_comment.getString("tb_date").substring(0,10);
		int c_uid = rs_comment.getInt("uid");
		String t_id = rs_comment.getString("tb_id");
		
	%>
	<tr>
		<td width=300><%=rs_comment.getString("tb_name") %> | &nbsp;<%=tb_date %></td>
		<td width=700><%=rs_comment.getString("tb_comment") %></td>
		<%if(session_level != null && session_level.equals("10")){ %>
		<td width=100 align=right><a href = "comment_del.jsp?code=<%=code %>&uid=<%=uid%>&c_uid=<%=c_uid%>"><img src ="img/comment_del.gif" width=50%></a></td>
		<%}else if(session_id != null && session_id.equals(t_id)){ %>
		<td width=100 align=right><a href = "comment_del.jsp?code=<%=code %>&uid=<%=uid%>&c_uid=<%=c_uid%>"><img src ="img/comment_del.gif" width=50%></a></td>
		<%}else{ %>
		<td></td>
		<%} %>
	</tr>
	<%
	
	}
	rs_comment.close();
	stmt_comment.close();
	con_comment.close();
	%>
</table>

 

댓글 데이터베이스에 저장(comment_insert.jsp)

<%@page import="java.sql.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file = "/include/header.jsp" %>

<%
request.setCharacterEncoding("utf-8");

String code = request.getParameter("tb_table");

String uid = request.getParameter("tb_uid");

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

Date today = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String signdate = sd.format(today);

String sql = "insert into comment values (null,'"+code+"',"+uid+",'"+session_id+"','"+session_name+"','"+tb_comment+"','"+signdate+"')";

Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);


String sql_com_count = "update "+code+" set com_count = com_count+1 where uid ="+uid+"";
Connection con_com_count = DriverManager.getConnection(url, user, password);
Statement stmt_com_count = con_com_count.createStatement();
stmt_com_count.executeUpdate(sql_com_count);

%>

<script>
	location.href = "view.jsp?code=<%=code%>&uid=<%=uid%>";
</script>

 

댓글 삭제

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file = "/include/header.jsp" %>

<%
request.setCharacterEncoding("utf-8");

String code = request.getParameter("code");
String uid = request.getParameter("uid");
String c_uid = request.getParameter("c_uid");

String sql = "delete from comment where uid="+c_uid+"";

Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);

String sql_com_count = "update "+code+" set com_count = com_count-1 where uid ="+uid+"";
Connection con_com_count = DriverManager.getConnection(url, user, password);
Statement stmt_com_count = con_com_count.createStatement();
stmt_com_count.executeUpdate(sql_com_count);

%>

<script>
	location.href = "view.jsp?code=<%=code%>&uid=<%=uid%>";
</script>