index.jsp보기 접기
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>회원관리</h1> <a href='/${initParam.context_root } /register_form.jsp'>회원가입폼</a><br> <a href='/${initParam.context_root } /login_form.jsp'>로그인폼</a> <a href='/${initParam.context_root } /getAllMemberInfo'>전체 회원 조회</a> </body> </body> </html>
접기
register_form.jsp보기 접기
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="/${initParam.context_root } /RegisterMemberServlet1" method="post"> <table border="1"> <tr> <td align="center">id</td><td><input type="text" name="id" maxlength="10" size="10"></td> </tr> <tr> <td align="center">pwd</td><td><input type="password" name="pwd" maxlength="4" size="4"></td> </tr> <tr> <td align="center">이름 : </td><td><input type="text" name="name" maxlength="10" size="10"></td> </tr> <tr> <td align="center">주민번호 : </td> <td> <input type="text" name="register_no" maxlength="13" size="13"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="가입"> <input type="reset" value="다시작성"></td> </tr> </table> </form> </body> </html>
접기
RegisterMemberServlet1.java 보기 접기
package member.servlet;
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import member.dto.MemberDTO;
public class RegisterMemberServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; private Connection connection = null; private PreparedStatement preparedStatement = null;
public RegisterMemberServlet1() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.요청파라미터 조회 //2. Business Logic 처리 //1. Connection //2. PreparedStatement //3. SQL 문 전송(insert) //4. close //3. 응답처리 // 클라이언트로 받은 가입정보를 출력한뒤 가입처리 성공 메세지 응답. //1.요청 파라미터 조회 request.setCharacterEncoding("utf-8"); String id = request.getParameter("id"); String password = request.getParameter("pwd"); String name = request.getParameter("name"); String registerNo = request.getParameter("register_no"); int mileage = 1000; response.setContentType("text/html;charset=utf-8"); //Business Logic 처리 ServletContext ctx = getServletContext(); String url = ctx.getInitParameter("url"); String user = ctx.getInitParameter("user"); String dbPwd = ctx.getInitParameter("password"); String resUrl = null; try { connection = DriverManager.getConnection(url, user, dbPwd); String sql = "insert into member values(?,?,?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, id); preparedStatement.setString(2, password); preparedStatement.setString(3, name); preparedStatement.setString(4, registerNo); preparedStatement.setInt(5, mileage); int cnt = preparedStatement.executeUpdate(); System.out.println(cnt+"개의 행이 삽입되었습니다."); //1. Business Logic처리 //2. 조회한 정보 request scope에 binding MemberDTO memberDTO = new MemberDTO(id,password,name,registerNo,mileage); //3. member_list.jsp로 수행이동 request.setAttribute("memberDTO", memberDTO); resUrl = "/res/register_success.jsp"; } catch (SQLException e) { request.setAttribute("error_message", e.getMessage()); resUrl = "/res/error.jsp"; } finally{ if(preparedStatement!=null){ try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } RequestDispatcher rdp = request.getRequestDispatcher(resUrl); rdp.forward(request, response); }
}
접기
register_success.jsp보기 접기
<%@page import="member.dto.MemberDTO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html>< %@taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core " %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body><c:set var="memberDTO" scope="request" value="${memberDTO }"/> <%-- <% MemberDTO memberDTO = (MemberDTO)request.getAttribute("memberDTO"); %> --%> <h2>가입 환영합니다.</h2> <h3>가입 시 입력한 정보입니다. </h3> <%-- ID : <%=memberDTO.getId() %> --%>ID : ${requestScope.memberDTO.id } <%-- Password : <%=memberDTO.getPassword() %> --%>Password :${requestScope.memberDTO.password } <%-- 이름 : <%=memberDTO.getName() %> --%>이름 :${requestScope.memberDTO.name } <%-- 주민번호 : <%=memberDTO.getRegisterNumber().substring(0,6) %>-<%=memberDTO.getRegisterNumber().substring(6) %> --%>주민번호 :${requestScope.memberDTO.registerNumber } <%-- 마일리지 : <%=memberDTO.getMileage() %><p> --%>마일리지 :${requestScope.memberDTO.mileage } <a href="/${initParam.context_root }/login_form.jsp">로그인</a> <a href="/${initParam.context_root }/index.jsp">메인페이지</a> </body> </html>
접기
login_form.jsp보기 접기
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core " %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head><c:set var="error_message" scope="request" value="${error_message }"/> <%-- <% String error_message = (String)request.getAttribute("error_message"); if(error_message!=null){ %> --%><font color="red">${error_message } </font> <%-- <font color="red"><%=error_message %></font> --%> <%-- <% } %> --%> <body> <form action="/${initParam.context_root } /login" method="post"> <table> <tr> <td><h2>로그인</h2><p></td> </tr> <tr> <td>id</td><td><input type="text" name="id"></td> </tr> <tr> <td>password</td><td><input type="password" name="password"></td> </tr> <tr> <td colspan="2"><input type="submit" value="로그인"><input type="reset" value="초기화"></td> </tr> </table> </form> </body> </html>
접기
LoginServlet.java보기 접기
package member.servlet;
import java.io.IOException;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import member.dto.MemberDTO; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; private Connection connection = null; private PreparedStatement preparedStatement = null; private ResultSet rset = null; public LoginServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 요청파라미터 조회 request.setCharacterEncoding("utf-8"); String id = request.getParameter("id"); String password = request.getParameter("password"); HttpSession session = request.getSession(); response.setContentType("text/html;charset=utf-8"); //Business Logic 처리 ServletContext ctx = getServletContext(); String url = ctx.getInitParameter("url"); String user = ctx.getInitParameter("user"); String dbPwd = ctx.getInitParameter("password"); String sql = "select * from member where id=?"; String resUrl=null; try { connection = DriverManager.getConnection(url, user, dbPwd); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, id); rset = preparedStatement.executeQuery(); System.out.println("로그인 성공!!"); if(rset.next()) { if(rset.getString("password").equals(password)){ MemberDTO memberDTO = new MemberDTO(id, password, rset.getString(3), rset.getString(4), rset.getInt(5)); session.setAttribute("memberDTO", memberDTO); response.sendRedirect("/member_servlet_text/res/login_success.jsp"); }else{ request.setAttribute("error_message", "패스워드가 틀렸습니다"); resUrl = "/login_form.jsp"; } }else{//id가 없는 경우 request.setAttribute("error_message", "아이디가 틀렸습니다"); resUrl = "/login_form.jsp"; }
} catch (SQLException e) { request.setAttribute("error_message", e.getMessage()); RequestDispatcher rdp = request.getRequestDispatcher("/res/error.jsp"); rdp.forward(request, response);
} finally{ if(rset!=null){ try { rset.close(); } catch (SQLException e) { e.printStackTrace(); } } if(preparedStatement!=null){ try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } if(resUrl!=null){ RequestDispatcher rdp = request.getRequestDispatcher(resUrl); rdp.forward(request, response); } }
}
접기
login_success.jsp보기 접기
<%@page import="member.dto.MemberDTO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head>< %@taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core " %> <c:set var="memberDTO" scope="session" value="${memberDTO }"/> <%-- <%
MemberDTO memberDTO = (MemberDTO)session.getAttribute("memberDTO"); %> --%> <body> <%-- <%=memberDTO.getId() %>님환영합니다.<p> 이름 : <%=memberDTO.getName() %><br> 패스워드 : <%=memberDTO.getPassword() %><br> 주민번호 : <%=memberDTO.getRegisterNumber() %><br> 마일리지 : <%=memberDTO.getMileage() %><br> --%>${sessionScope.memberDTO.id }님환영합니다.<p> 이름 : ${sessionScope.memberDTO.name }<br> 패스워드 : ${sessionScope.memberDTO.password }<br> 주민번호 : ${sessionScope.memberDTO.registerNumber }<br> 마일리지 : ${sessionScope.memberDTO.mileage }<br> <a href='/${initParam.context_root }/logout'>로그아웃</a> <a href='/${initParam.context_root }/getMemberInfo'>회원정보조회(로그인한 회원의 정보)</a> <!--<input type="button" value="회원정보조회" onClick="location.href='/member_servlet/getMemberInfo'"> --> </body> </html>
접기
GetLoginMemberInfoServlet.java보기 접기
package member.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import member.dto.MemberDTO;
public class GetLoginMemberInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public GetLoginMemberInfoServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession httpSession = request.getSession(); RequestDispatcher rdp =null; MemberDTO memberDTO=(MemberDTO)httpSession.getAttribute("memberDTO"); String resUrl = null; if(memberDTO==null){ request.setAttribute("error_message", "로그인한 정보가 없습니다."); resUrl = "/login_form.jsp"; }else{ resUrl ="/res/member_details.jsp"; } rdp = request.getRequestDispatcher(resUrl); rdp.forward(request, response); }
}
접기
member_detail.jsp보기 접기
<%@page import="member.dto.MemberDTO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%-- <% MemberDTO memberDTO = (MemberDTO)session.getAttribute("memberDTO"); %>
<h2>회원정보보기</h2><p> 아이디 : <%=memberDTO.getId() %><br> 이름 : <%=memberDTO.getName() %><br> 주민번호 : <%=memberDTO.getRegisterNumber().substring(0, 6) %>-<%=memberDTO.getRegisterNumber().substring(6) %><br> 마일리지 : <%=memberDTO.getMileage() %><br> <p> <a href="/member_servlet/modifyForm">회원정보수정</a> <a href="/member_servlet/removeMember">회원 탈퇴</a> </p> --%> <h2>회원정보보기</h2><p> 아이디 : ${sessionScope.memberDTO.id }<br> 이름 : ${sessionScope.memberDTO.name }<br> 주민번호 : ${sessionScope.memberDTO.registerNumber }<br> 마일리지 : ${sessionScope.memberDTO.mileage }<br> <p> <a href="/${initParam.context_root }/modifyForm">회원정보수정</a> <a href="/${initParam.context_root }/removeMember">회원 탈퇴</a> </p> </body> </html>
접기
ModifyFormServlet.java보기 접기
package member.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import member.dto.MemberDTO;
public class ModifyFormServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public ModifyFormServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession httpSession = request.getSession(); MemberDTO memberDTO = (MemberDTO) httpSession.getAttribute("memberDTO"); String resUrl = null; if(memberDTO==null){ httpSession.setAttribute("error_message", "로그인 정보가 없습니다"); resUrl = "/login_form.jsp"; }else{ resUrl = "/res/modify_form.jsp"; } RequestDispatcher requestDispatcher = request.getRequestDispatcher(resUrl); requestDispatcher.forward(request, response); }
}
접기
modifyForm.jsp보기 접기
<%@page import="member.dto.MemberDTO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <%-- <% MemberDTO memberDTO = (MemberDTO)session.getAttribute("memberDTO"); %> --%> <body><h2>회원정보수정</h2><hr><p> <form action="/${initParam.context_root }/modifyMember" method="post"> <table width="500"> <tr> <td>아이디</td><td>${sessionScope.memberDTO.id }</td> </tr> <tr> <td>비밀번호</td><td><input type="text" name="password"></td> </tr> <tr> <td>이름</td><td><input type="text" name="name" value="${sessionScope.memberDTO.name }"></td> </tr> <tr> <td>주민번호</td><td><input type="text" name="register_no" value="${sessionScope.memberDTO.registerNumber }"></td> </tr> <tr> <td>마일리지 ${sessionScope.memberDTO.mileage }</td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="정보수정"><input type="reset" value="다시작성"></td> </tr> </table> </form> </body> </html>
접기
ModifyMemberServlet.java보기 접기
package member.servlet;
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import member.dto.MemberDTO;
public class ModifyMemberServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public ModifyMemberServlet() { super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); // 1. 파라미터 조회 String password = request.getParameter("password"); String name = request.getParameter("name"); String registerNo = request.getParameter("register_no");
// 2. DB연결 ServletContext servletContext = getServletContext(); String url = servletContext.getInitParameter("url"); String user = servletContext.getInitParameter("user"); String dbPwd = servletContext.getInitParameter("password");
Connection connection = null; PreparedStatement preparedStatement = null;
String sql = "update member set password=?, name=?, registerNumber=? where id=?"; String resUrl = null;
HttpSession httpSession = request.getSession(); MemberDTO memberDTO = (MemberDTO) httpSession.getAttribute("memberDTO"); if (memberDTO != null) { //3. 회원 정보 수정 처리 try { connection = DriverManager.getConnection(url, user, dbPwd); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, password); preparedStatement.setString(2, name); preparedStatement.setString(3, registerNo); preparedStatement.setString(4, memberDTO.getId()); int cnt = preparedStatement.executeUpdate(); System.out.println(cnt + "개의 행이 수정되었습니다."); //수정 정보를 session scope에 넣기 // memberDTO.setPassword(password); // memberDTO.setName(name); // memberDTO.setRegisterNumber(registerNo); //아래와 같이 넣어도 된다. memberDTO = new MemberDTO(memberDTO.getId(), password, name, registerNo,memberDTO.getMileage()); httpSession.setAttribute("memberDTO", memberDTO); // resUrl = "/res/modify_success.jsp";//아래 소스와 소스가 같아서 변경 처리 했다. resUrl = "/res/member_details.jsp"; } catch (SQLException e) { request.setAttribute("error_message", e.getMessage()); resUrl = "/res/error.jsp"; } finally { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }else{ //로그인이 안된경우 request.setAttribute("error_message", "로그인 정보가 없습니다. 수정 불가능 합니다."); resUrl = "/login_form.jsp"; } RequestDispatcher requestDispatcher = request.getRequestDispatcher(resUrl); requestDispatcher.forward(request, response); }
}
접기
member_details.jsp보기 접기
<%@page import="member.dto.MemberDTO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%-- <% MemberDTO memberDTO = (MemberDTO)session.getAttribute("memberDTO"); %>
<h2>회원정보보기</h2><p> 아이디 : <%=memberDTO.getId() %><br> 이름 : <%=memberDTO.getName() %><br> 주민번호 : <%=memberDTO.getRegisterNumber().substring(0, 6) %>-<%=memberDTO.getRegisterNumber().substring(6) %><br> 마일리지 : <%=memberDTO.getMileage() %><br> <p> <a href="/member_servlet/modifyForm">회원정보수정</a> <a href="/member_servlet/removeMember">회원 탈퇴</a> </p> --%> <h2>회원정보보기</h2><p> 아이디 : ${sessionScope.memberDTO.id }<br> 이름 : ${sessionScope.memberDTO.name }<br> 주민번호 : ${sessionScope.memberDTO.registerNumber }<br> 마일리지 : ${sessionScope.memberDTO.mileage }<br> <p> <a href="/${initParam.context_root }/modifyForm">회원정보수정</a> <a href="/${initParam.context_root }/removeMember">회원 탈퇴</a> </p> </body> </html>
접기
modify_success.jsp보기 접기
<%@page import="member.dto.MemberDTO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <%-- <% MemberDTO memberDTO = (MemberDTO)session.getAttribute("memberDTO"); %> --%> <body> <%-- <h2>수정된 정보</h2><hr><p> 아이디 : <%=memberDTO.getId() %><br> 비밀번호 : <%=memberDTO.getPassword() %><br> 이름 : <%=memberDTO.getName() %><br> 주민번호 : <%=memberDTO.getRegisterNumber().substring(0, 6) %>-<%=memberDTO.getRegisterNumber().substring(6) %><br> 마일리지 : <%=memberDTO.getMileage() %><br> --%> <h2>수정된 정보</h2><hr><p> 아이디 :${sessionScope.memberDTO.id }<br> 비밀번호 :${sessionScope.memberDTO.password }<br> 이름 : ${sessionScope.memberDTO.name }<br> 주민번호 :${sessionScope.memberDTO.registerNumber }<br> 마일리지 :${sessionScope.memberDTO.mileage }<br> </body> </html>
접기
RemoveMemberServlet.java보기 접기
package member.servlet;
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import member.dto.MemberDTO;
public class RemoveMemberServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public RemoveMemberServlet() { super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession httpSession = request.getSession(); MemberDTO memberDTO = (MemberDTO)httpSession.getAttribute("memberDTO"); //DB연결 Connection connection = null; PreparedStatement preparedStatement = null; ServletContext servletContext = getServletContext(); String url = servletContext.getInitParameter("url"); String user = servletContext.getInitParameter("user"); String dbPwd = servletContext.getInitParameter("password"); String sql = "delete from member where id=?"; String resUrl = null; if(memberDTO!=null){ try { connection = DriverManager.getConnection(url, user, dbPwd); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, memberDTO.getId()); int cnt = preparedStatement.executeUpdate(); System.out.println(cnt+"개의 행이 삭제 되었습니다."); resUrl = "/index.jsp"; httpSession.invalidate(); //httpSession.removeAttribute("memberDTO"); } catch (SQLException e) { e.printStackTrace(); request.setAttribute("error_message", e.getMessage()); resUrl = "/res/error.jsp"; } finally{ if(preparedStatement!=null){ try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }else{ request.setAttribute("error_message", "로그인 정보가 없습니다."); resUrl = "/login_form.jsp"; } RequestDispatcher requestDispatcher = request.getRequestDispatcher(resUrl); requestDispatcher.forward(request, response); } }
접기
GetAllMemberInfoServlet.java보기 접기
package member.servlet;
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import member.dto.MemberDTO;
public class GetAllMemberInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public GetAllMemberInfoServlet() { super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = getServletContext(); String url = servletContext.getInitParameter("url"); String user = servletContext.getInitParameter("user"); String dbPwd = servletContext.getInitParameter("password");
Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String sql = "select * from member"; ArrayList<MemberDTO> list = new ArrayList<MemberDTO>(); String resUrl = null; try { connection = DriverManager.getConnection(url, user, dbPwd); preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { MemberDTO memberDTO = new MemberDTO(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getInt(5)); list.add(memberDTO); } request.setAttribute("member_list", list); resUrl ="/member_list.jsp"; } catch (SQLException e) { e.printStackTrace(); request.setAttribute("error_message", e.getMessage()); resUrl = "/res/error.jsp"; } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } }
} RequestDispatcher requestDispatcher = request.getRequestDispatcher(resUrl); requestDispatcher.forward(request, response); }
}
접기
member_list.jsp보기 접기
<%@page import="member.dto.MemberDTO"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core " %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title><c:set var="member_list" scope="request" value="${member_list }"/> <%-- <% ArrayList<MemberDTO> member_list = (ArrayList<MemberDTO>)request.getAttribute("member_list"); %> --%> <body>
<h2>모든 회원 목록조회</h2><hr><p> <table width="500" border="1" cellspacing="0"> <tr align="center"> <td>순번</td><td>아이디</td><td>이름</td><td>주민번호</td><td>마일리지</td> </tr> <%-- <% for(int i=0;i<member_list.size();i++){ %> <tr align="center"> <td><%=i+1 %></td><td><%=member_list.get(i).getId() %></td><td><a href="/servlet_member/SearchMemberByID?id=<%=member_list.get(i).getId()%>"><%=member_list.get(i).getName() %></a></td><td><%=member_list.get(i).getRegisterNumber().substring(0, 6) %>-<%=member_list.get(i).getRegisterNumber().substring(6) %></td><td><%=member_list.get(i).getMileage() %></td> </tr> <%} %> --%><c:forEach items="${requestScope.member_list }" var="member_list" varStatus="cnt"> <tr align="center"> <td>${cnt.count }</td><td>${member_list.id }</td><td><a href="/servlet_member/SearchMemberByID?id=${member_list.id }">${member_list.name }</a></td><td>${member_list.registerNumber }</td><td>${member_list.mileage }</td> </tr> </c:forEach> </table>
</body> </html>
접기
member_servlet_text.zip