1. 로그인 정보 체크
로그인 X ->login_form.jsp
로그인 O -> delete처리 후 session 제거 -> index.html
<%@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>
</body>
</html>
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.html";
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);
}
}
<!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='/member_servlet/register_form.html'>회원가입폼</a><br>
<a href='/member_servlet/login_form.jsp'>로그인폼</a>
<a href='/member_servlet/getAllMemberInfo'>전체 회원 조회</a>
</body>
</html>
<%@ 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>
<%
String error_message = (String)request.getAttribute("error_message");
if(error_message!=null){
%>
<font color="red"><%=error_message %></font>
<%
}
%>
<body>
<form action="/member_servlet/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>
<%@ 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>
<%
String errMessage = (String)request.getAttribute("error_message");
%>
실행 도중 오류가 발생했습니다.<br>
오류 내용<%=errMessage %>
<a href='/member_servlet/index.html'>메인페이지로 이동</a>
</body>
</html>