if(info.getEmail().equals("admin"))
일 때 <a href="select.jsp">회원전체검색</a>
-> 추가됨<nav>
<% if(info != null){ %>
<!-- id가 admin이면회원전체검색이 되도록!! -->
<%if(info.getEmail().equals("admin")){ %>
<a href="select.jsp">회원전체검색</a>
<a href="update.jsp">개인정보수정<a>
<a href="LogoutServiceCon">로그아웃<a>
<%} else{ %>
<a href="update.jsp">개인정보수정<a>
<a href="LogoutServiceCon">로그아웃<a>
<%} %>
<%} else {%>
<a href ="#menu">로그인<a>
<%} %>
</nav> </header>
<tr><td></td></tr>
로 출력href='DeleteServiceCon?email="+email+"'
DeleteServiceCon 서블릿 파일에 쿼리스트링형태로 키값을 담아서 보내줌!!! (폼태그처럼 데이터를 전송할수없어서)<%@page import="java.net.URLEncoder"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="Model.MemberDAO"%>
<%@page import="Model.MemberDTO"%>
<%@page import="oracle.jdbc.driver.DBConversion"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 1.request영역에 저장된 정보를 가져오시오. -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Forty by HTML5 UP</title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="assets/css/main.css" />
<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
</head>
<style>
</style>
<body style="text-align: center;">
<!-- Wrapper -->
<div id="wrapper">
<!-- Menu -->
<nav id="Update">
<table>
<caption><h2>회원관리페이지</h2></caption>
<tr>
<td>Email</td>
<td>Tel</td>
<td>Address</td>
</tr>
<!-- 2.모든 회원의 이메일(email),전화번호(tel),주소(address)를 출력하시오. -->
<%
//db연결
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String dbid = "hr";
String dbpw = "hr";
conn = DriverManager.getConnection(url, dbid, dbpw);
String sql = "select * from web_member2";
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while(rs.next()){
String email = rs.getString(1);
String tel = rs.getString(3);
String address = rs.getString(4);
out.print("<tr>");
out.print("<td>"+ email +"</td>");
out.print("<td>"+ tel +"</td>");
out.print("<td>"+ address +"</td>");
out.print("<td><a href='DeleteServiceCon?email="+email+"'>삭제</a></td>");
out.print("</tr>");
}
//db연결해제
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null){
rs.close();
}if(psmt!=null){
psmt.close();
}if(conn!=null){
conn.close();
}
}
%>
</table>
</nav>
<a href="main.jsp" class="button next scrolly">되돌아가기</a>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="assets/js/main.js"></script>
</body>
</html>
public int delete(String email) {
dbconn();
try {
String sql="delete from web_member2 where email=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, email);
cnt = psmt.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
} finally {
dbclose();
}
return cnt;
}
package Controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Model.MemberDTO;
import Model.MemberDAO;
@WebServlet("/DeleteServiceCon")
public class DeleteServiceCon extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
MemberDAO dao = new MemberDAO();
int cnt = dao.delete(email);
if(cnt>0) {
response.sendRedirect("select.jsp");
}else {
response.sendRedirect("select.jsp");
System.out.println("삭제실패");
}
}
}
package Model;
public class MessageDTO {
//필드명이 선언시 테이블 컬럼명과 같아야 좋다
private int num;
private String send_name;
private String receive_email;
private String content;
private String sendDate; //날짜계산을 안하므로 String type
public MessageDTO(int num, String send_name, String receive_email, String content, String sendDate) {
super();
this.num = num;
this.send_name = send_name;
this.receive_email = receive_email;
this.content = content;
this.sendDate = sendDate;
}
public MessageDTO(String send_name, String receive_email, String content) {
super();
this.send_name = send_name;
this.receive_email = receive_email;
this.content = content;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getSend_name() {
return send_name;
}
public void setSend_name(String send_name) {
this.send_name = send_name;
}
public String getReceive_email() {
return receive_email;
}
public void setReceive_email(String receive_email) {
this.receive_email = receive_email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSendDate() {
return sendDate;
}
public void setSendDate(String sendDate) {
this.sendDate = sendDate;
}
}
<section>
<!-- content용량 때문에 post방식을 사용 -->
<form action="MsgCon" method="post">
<div class="field half first">
<label for="name">Name</label> <input type="text" name="send_name" id="name"
placeholder="보내는 사람 이름" />
</div>
<div class="field half">
<label for="email">Email</label> <input type="text" name="receive_email" id="email"
placeholder="보낼 사람 이메일" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="content" id="message" rows="6"></textarea>
</div>
<ul class="actions">
<li><input type="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Clear" /></li>
</ul>
</form>
</section>
public int sendMsg(MessageDTO dto) {
try {
dbconn();
String sql = "insert into web_massage values(msg_num_seq.nextval, ?, ?, ?, sysdate)";
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getSend_name());
psmt.setString(2, dto.getReceive_email());
psmt.setString(3, dto.getContent());
cnt = psmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
dbclose();
}
return cnt;
}
package Controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Model.MessageDAO;
import Model.MessageDTO;
@WebServlet("/MsgCon")
public class MsgCon extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post방식 인코딩
request.setCharacterEncoding("UTF-8");
String s_name = request.getParameter("send_name");
String r_email = request.getParameter("receive_email");
String content = request.getParameter("content");
//DTO객체 생성
MessageDTO dto = new MessageDTO(s_name, r_email, content);
MessageDAO dao = new MessageDAO();
int cnt = dao.sendMsg(dto);
if(cnt>0) {
System.out.println("메시지전송 성공");
}else {
System.out.println("메시지전송 실패");
}
response.sendRedirect("main.jsp");
}
}
로그인을 했다면 web_massage 테이블에 저장된 메시지 정보 가져오기
메세지 출력하는 메소드 List<E> : ArrayList 상위클래스
-->더 포괄적인 변수를 사용할 수 있다. object
-로그인정보가 담긴 session info값이 not null일때
-매개변수 : 로그인한 이메일
-리턴 : List<MessageDTO> 반환
-->전역변수로 처리하면 계속 null값이 뜨기때문에 메소드안에서 지역변수로 선언
-받은사람 email과 로그인한 사람email이 동일해야해서, select문의 where절 의 바인드변수에 .setString으로 받아온 email주소를 넣는다!
-DB에서 받아와야할 값은 컬럼 순서대로 num, send_name, receive_email=email, content,sendDate 순으로 각 변수에 담아준다. -> MessageDTO msgdto = null;클래스영역에 선언해주기 (while문밖에서도 쓰여야됨)-> 이 값들을 msgdto에 담고, msgdto를 list에 추가함
public List<MessageDTO> selectMsg(String email){
List<MessageDTO> msglist = new ArrayList<MessageDTO>();
try {
dbconn();
String sql = "select * from web_massage where receive_email=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, email);
rs = psmt.executeQuery();
while(rs.next()) {
int num = rs.getInt(1);
String send_name = rs.getString(2);
String receive_email = email;
String content = rs.getString(4);
String sendDate = rs.getString(5);
MessageDTO msgdto = new MessageDTO(num, send_name, receive_email, content, sendDate);
msglist.add(msgdto);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
dbclose();
} return msglist;
}
package Controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Model.MemberDTO;
import Model.MessageDAO;
import Model.MessageDTO;
@WebServlet("/goMain")
public class goMain extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//세션정보
HttpSession session = request.getSession();
MemberDTO dto = (MemberDTO)session.getAttribute("info");
//로그인한 사람의 메시지 출력
if(dto !=null) {
//로그인상태
MessageDAO dao = new MessageDAO();
List<MessageDTO> msglist = dao.selectMsg(dto.getEmail());
// List에 담긴 정보 가진채로 페이지 이동 forward방식
// 이동 전 List 정보 저장
// request scope영역에 리퀘스트에 저장을 하면 리스폰스객체를 가진채로 이동을 함
request.setAttribute("msglist", msglist);
}else {
}
//forward방식으로 페이지를 이동!
RequestDispatcher rd = request.getRequestDispatcher("main.jsp");
rd.forward(request, response);
}
}
List<MessageDTO>msglist = (List<MessageDTO>)request.getAttribute("msglist");
<header class="major">
<h2>나에게 온 메세지 확인하기</h2>
</header>
<p></p>
<ul class="actions">
<%
/* 로그인을 했으면 메세지 출력 send_name, content, sendDate */
if(info != null){
/* for each문 -> 확장 for문 */
/* for(객체타입 변수명 : 배열){} */
for(MessageDTO mdto : msglist){
%>
<li>
<span><%= mdto.getSend_name() %></span>
<span><%= mdto.getContent() %></span>
<span><%= mdto.getSendDate() %></span>
</li>
<% }
}else{%>
<li>로그인을 하세요.</li>
<li><a href="#" class="button next scrolly">전체삭제하기</a></li>
<% }%>
</ul>
컨트롤러의 마지막 sendRedirect 경로에 "goMain"으로 변경!, jsp에서도 main.jsp으로 가는 모든 경로를 "goMain"으로 변경!