안경잡이개발자

728x90
반응형

  이번 시간에는 JSP 강의평가(Lecture Evaluation) 웹 사이트 개발하기의 여덟 번째 시간입니다. 지난 시간에는 로그인 및 로그아웃 기능을 구현했습니다. 이제 로그인 이후에 사용자들이 강의 평가 글을 등록하는 기능을 작성해보도록 하겠습니다. 또한 특정한 게시글 등에 대해서 신고를 하는 기능 또한 구현을 하려고 합니다. 바로 시작을 해보도록 합시다.



  위와 같이 두 개의 JSP 페이지를 추가합니다. 하나는 reportAction.jsp이고, 다른 하나는 evaluationRegisterAction.jsp입니다. 그리고 evaluation 패키지를 생성합니다. 이후에 해당 패키지 안에 EvaluationDTO.java, EvaluationDAO.java를 작성해주시면 됩니다.


  ▶ EvaluationDTO.java: 하나의 강의 평가 정보를 담고 있는 데이터베이스 트랜잭션 객체 역할을 담당합니다.


package evaluation;


public class EvaluationDTO {


int evaluationID;

String userID;

String lectureName;

String professorName;

int lectureYear;

String semesterDivide;

String lectureDivide;

String evaluationTitle;

String evaluationContent;

String totalScore;

String creditScore;

String comfortableScore;

String lectureScore;

int likeCount;

public int getEvaluationID() {

return evaluationID;

}

public void setEvaluationID(int evaluationID) {

this.evaluationID = evaluationID;

}

public String getUserID() {

return userID;

}

public void setUserID(String userID) {

this.userID = userID;

}

public String getLectureName() {

return lectureName;

}

public void setLectureName(String lectureName) {

this.lectureName = lectureName;

}

public String getProfessorName() {

return professorName;

}

public void setProfessorName(String professorName) {

this.professorName = professorName;

}

public int getLectureYear() {

return lectureYear;

}

public void setLectureYear(int lectureYear) {

this.lectureYear = lectureYear;

}

public String getSemesterDivide() {

return semesterDivide;

}

public void setSemesterDivide(String semesterDivide) {

this.semesterDivide = semesterDivide;

}

public String getLectureDivide() {

return lectureDivide;

}

public void setLectureDivide(String lectureDivide) {

this.lectureDivide = lectureDivide;

}

public String getEvaluationTitle() {

return evaluationTitle;

}

public void setEvaluationTitle(String evaluationTitle) {

this.evaluationTitle = evaluationTitle;

}

public String getEvaluationContent() {

return evaluationContent;

}

public void setEvaluationContent(String evaluationContent) {

this.evaluationContent = evaluationContent;

}

public String getTotalScore() {

return totalScore;

}

public void setTotalScore(String totalScore) {

this.totalScore = totalScore;

}

public String getCreditScore() {

return creditScore;

}

public void setCreditScore(String creditScore) {

this.creditScore = creditScore;

}

public String getComfortableScore() {

return comfortableScore;

}

public void setComfortableScore(String comfortableScore) {

this.comfortableScore = comfortableScore;

}

public String getLectureScore() {

return lectureScore;

}

public void setLectureScore(String lectureScore) {

this.lectureScore = lectureScore;

}

public int getLikeCount() {

return likeCount;

}

public void setLikeCount(int likeCount) {

this.likeCount = likeCount;

}

public EvaluationDTO(int evaluationID, String userID, String lectureName, String professorName, int lectureYear,

String semesterDivide, String lectureDivide, String evaluationTitle, String evaluationContent,

String totalScore, String creditScore, String comfortableScore, String lectureScore, int likeCount) {

super();

this.evaluationID = evaluationID;

this.userID = userID;

this.lectureName = lectureName;

this.professorName = professorName;

this.lectureYear = lectureYear;

this.semesterDivide = semesterDivide;

this.lectureDivide = lectureDivide;

this.evaluationTitle = evaluationTitle;

this.evaluationContent = evaluationContent;

this.totalScore = totalScore;

this.creditScore = creditScore;

this.comfortableScore = comfortableScore;

this.lectureScore = lectureScore;

this.likeCount = likeCount;

}

}


  ▶ EvaluationDAO.java: 강의 평가와 관련된 데이터베이스 접근을 가능하게 해주는 객체입니다.


package evaluation;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;


public class EvaluationDAO {


private Connection conn;

private ResultSet rs;


public EvaluationDAO() {

try {

String dbURL = "jdbc:mysql://localhost:3306/LectureEvaluation";

String dbID = "데이터베이스 아이디";

String dbPassword = "데이터베이스 비밀번호";

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection(dbURL, dbID, dbPassword);

} catch (Exception e) {

e.printStackTrace();

}

}

public int write(EvaluationDTO evaluationDTO) {

PreparedStatement pstmt = null;

try {

String SQL = "INSERT INTO EVALUATION VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0);";

pstmt = conn.prepareStatement(SQL);

pstmt.setString(1, evaluationDTO.getUserID());

pstmt.setString(2, evaluationDTO.getLectureName());

pstmt.setString(3, evaluationDTO.getProfessorName());

pstmt.setInt(4, evaluationDTO.getLectureYear());

pstmt.setString(5, evaluationDTO.getSemesterDivide());

pstmt.setString(6, evaluationDTO.getLectureDivide());

pstmt.setString(7, evaluationDTO.getEvaluationTitle());

pstmt.setString(8, evaluationDTO.getEvaluationContent());

pstmt.setString(9, evaluationDTO.getTotalScore());

pstmt.setString(10, evaluationDTO.getCreditScore());

pstmt.setString(11, evaluationDTO.getComfortableScore());

pstmt.setString(12, evaluationDTO.getLectureScore());

return pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if(pstmt != null) pstmt.close();

if(conn != null) conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return -1;

}

}


  ▶ reportAction.jsp: 사용자가 신고서를 작성하면, 관리자의 메일로 그 신고 내용을 받습니다.


<%@page import="javax.mail.Transport"%>

<%@page import="javax.mail.Message"%>

<%@page import="javax.mail.Address"%>

<%@page import="javax.mail.internet.InternetAddress"%>

<%@page import="javax.mail.internet.MimeMessage"%>

<%@page import="javax.mail.Session"%>

<%@page import="javax.mail.Authenticator"%>

<%@page import="java.util.Properties"%>

<%@page import="java.io.PrintWriter"%>

<%@page import="user.UserDAO"%>

<%@page import="util.SHA256"%>

<%@page import="util.Gmail"%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

String userID = null;

if(session.getAttribute("userID") != null) {

userID = (String) session.getAttribute("userID");

}

if(userID == null) {

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("alert('로그인을 해주세요.');");

script.println("location.href = 'userLogin.jsp'");

script.println("</script>");

script.close();

return;

}

request.setCharacterEncoding("UTF-8");

String reportTitle = null;

String reportContent = null;

if(request.getParameter("reportTitle") != null) {

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

}

if(request.getParameter("reportContent") != null) {

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

}

if (reportTitle == null || reportContent == null) {

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("alert('입력이 안 된 사항이 있습니다.');");

script.println("history.back();");

script.println("</script>");

script.close();

return;

}

// 사용자에게 보낼 메시지를 기입합니다.

String host = "http://localhost:8080/Lecture_Evaluation/";

String from = "구글 계정 아이디";

String to = "관리자/개발자의 이메일 주소";

String subject = "강의평가 사이트에서 접수된 신고 메일입니다.";

String content = "신고자: " + userID + "<br>제목: " + reportTitle + "<br>내용: " + reportContent;

// SMTP에 접속하기 위한 정보를 기입합니다.

Properties p = new Properties();

p.put("mail.smtp.user", from);

p.put("mail.smtp.host", "smtp.googlemail.com");

p.put("mail.smtp.port", "465");

p.put("mail.smtp.starttls.enable", "true");

p.put("mail.smtp.auth", "true");

p.put("mail.smtp.debug", "true");

p.put("mail.smtp.socketFactory.port", "465");

p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

p.put("mail.smtp.socketFactory.fallback", "false");

 

try{

    Authenticator auth = new Gmail();

    Session ses = Session.getInstance(p, auth);

    ses.setDebug(true);

    MimeMessage msg = new MimeMessage(ses); 

    msg.setSubject(subject);

    Address fromAddr = new InternetAddress(from);

    msg.setFrom(fromAddr);

    Address toAddr = new InternetAddress(to);

    msg.addRecipient(Message.RecipientType.TO, toAddr);

    msg.setContent(content, "text/html;charset=UTF-8");

    Transport.send(msg);

} catch(Exception e){

    e.printStackTrace();

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("alert('오류가 발생했습니다.');");

script.println("history.back();");

script.println("</script>");

script.close();

    return;

}

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("alert('정상적으로 신고되었습니다.');");

script.println("history.back();");

script.println("</script>");

script.close();

    return;

%>


  ▶ evaluationRegisterAction.jsp: 사용자가 평가를 등록하게 되면, 그 평가 등록을 처리합니다.


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="evaluation.EvaluationDTO"%>

<%@ page import="evaluation.EvaluationDAO"%>

<%@ page import="java.io.PrintWriter"%>

<%

request.setCharacterEncoding("UTF-8");


String userID = null;

if(session.getAttribute("userID") != null) {

userID = (String) session.getAttribute("userID");

}

if(userID == null) {

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("alert('로그인을 해주세요.');");

script.println("location.href = 'userLogin.jsp'");

script.println("</script>");

script.close();

return;

}

request.setCharacterEncoding("UTF-8");

String lectureName = null;

String professorName = null;

int lectureYear = 0;

String semesterDivide = null;

String lectureDivide = null;

String evaluationTitle = null;

String evaluationContent = null;

String totalScore = null;

String creditScore = null;

String comfortableScore = null;

String lectureScore = null;

if(request.getParameter("lectureName") != null) {

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

}

if(request.getParameter("professorName") != null) {

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

}

if(request.getParameter("lectureYear") != null) {

try {

lectureYear = Integer.parseInt(request.getParameter("lectureYear"));

} catch (Exception e) {

System.out.println("강의 연도 데이터 오류");

}

}

if(request.getParameter("semesterDivide") != null) {

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

}

if(request.getParameter("lectureDivide") != null) {

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

}

if(request.getParameter("evaluationTitle") != null) {

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

}

if(request.getParameter("evaluationContent") != null) {

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

}

if(request.getParameter("totalScore") != null) {

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

}

if(request.getParameter("creditScore") != null) {

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

}

if(request.getParameter("comfortableScore") != null) {

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

}

if(request.getParameter("lectureScore") != null) {

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

}

if (lectureName == null || professorName == null || lectureYear == 0 || semesterDivide == null ||

lectureDivide == null || evaluationTitle == null || evaluationContent == null || totalScore == null ||

creditScore == null || comfortableScore == null || lectureScore == null ||

evaluationTitle.equals("") || evaluationContent.equals("")) {

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("alert('입력이 안 된 사항이 있습니다.');");

script.println("history.back();");

script.println("</script>");

script.close();

return;

} else {

EvaluationDAO evaluationDAO = new EvaluationDAO();

int result = evaluationDAO.write(new EvaluationDTO(0, userID, lectureName, professorName, lectureYear,

semesterDivide, lectureDivide, evaluationTitle, evaluationContent,

totalScore, creditScore, comfortableScore, lectureScore, 0));

if (result == -1) {

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("alert('평가 등록에 실패했습니다.');");

script.println("history.back();");

script.println("</script>");

script.close();

return;

} else {

PrintWriter script = response.getWriter();

script.println("<script>");

script.println("location.href = './index.jsp';");

script.println("</script>");

script.close();

return;

}

}

%>


  이제 한 번 테스트를 해봅시다.



  위와 같이 신고를 해보겠습니다.


  

  그럼 위와 같이 관리자 이메일로 신고 내용이 도착하는 것을 알 수 있습니다.


  평가 또한 등록해보도록 하겠습니다.



  성공적으로 평가 데이터가 데이터베이스에 등록되었습니다.

728x90
반응형