728x90
반응형

JSP

<!-- 서블릿으로 value전송 -->
<form name="likeFrm" action="<%= request.getContextPath()%>/board/boardView" method="POST">
	<input type="hidden" id="likeMemId" name="memberId" value="<%= memberLoggedIn.getMemberId() %>" />
	<input type="hidden" id="likeBrId" name="br_cp_id" value="<%= roomBoard.getBr_cp_id() %>" />
	<input type="hidden" id=likeBoardNum name="board_num" value="<%= room.getBoard_num() %>" />
</form>

<div id="roomDetail-LikeAlarm">
	<!-- like가 true인지 false인지에 따라 컬러하트/흑백하트 보여주기 -->
	<img src=<%= like ? "../images/colorHeart.png" : "../images/heart.png" %> id="likeBtn" alt="" />좋아요 
</div>

 

 

Ajax

//like
$(document).ready(function(){
    $("#likeBtn").click(function(){
		$.ajax({
			url: "<%= request.getContextPath() %>/board/boardView",
			method: "POST", 
			dataType: "text", //html, text, json, xml 리턴된 데이터에 따라 자동설정됨
			data:  {"memberId": $("#likeMemId").val(),
				"board_num" : $("#likeBoardNum").val(),
				"br_cp_id" : $("#likeBoardNum").val()
				}, //사용자 입력값전달
			success: function(data){
				history.go(0);
			},
			error: function(xhr, textStatus, errorThrown){
				console.log("ajax 요청 실패!");
				console.log(xhr, textStatus, errorThrown);
			}
		});
    });
});

 

 

Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String memberId = request.getParameter("memberId");
	String br_cp_id = request.getParameter("br_cp_id");
	int board_num = Integer.parseInt(request.getParameter("board_num"));

	BoardLike bl = new BoardService().selectLikeOne(board_num, memberId);
	boolean exitistLike = bl == null ? false : true;
	
    if(exitistLike) {
	
    //like 삭제
	int result = new BoardService().deleteLike(bl);

	}else {
	
    //like 추가
	BoardLike like = new BoardLike(memberId, board_num, "T");
	int result = new BoardService().insertLike(like);
	}

	BoardLike resultBl = new BoardService().selectLikeOne(board_num, memberId);
	boolean like = resultBl == null ? false : true;

}
반응형

'프로그래밍 > Ajax' 카테고리의 다른 글

[Ajax] ajax success 부분 안타는 오류  (0) 2022.08.11
[ajax] ajax 통신 시 데이터 null 값  (0) 2022.08.03
09.21(아이디 중복 검사)  (0) 2020.09.21
09.15(제이퀴리와 Ajax)  (0) 2020.09.15
09.14(Ajax)  (0) 2020.09.14
복사했습니다!