<%@ 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>
</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>
<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>
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);
}
}
<%@ 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:if test="${requestScope.error_message!=null }">
<font color="red">${requestScope.error_message }</font>
</c:if>
<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>
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 contextRoot = getServletContext().getInitParameter("context_root");
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("/"+contextRoot+"/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);
}
}
}
<%@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>
${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='/${initParam.context_root }/getMemberInfo'"> -->
</body>
</html>
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);
}
}
<%@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>
<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>
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);
}
}
<%@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>
<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>
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=?, register_Number=? 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);
}
}
<%@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>
<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>
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 LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LogoutServlet() {
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 session = request.getSession();
//로그인 여부 체크
MemberDTO memberDTO = (MemberDTO) session.getAttribute("memberDTO");
if (memberDTO != null) {
session.invalidate(); //세션을 지우기
} else {
request.setAttribute("error_message", "먼저 로그인 해주세요");
}
RequestDispatcher rdp = request.getRequestDispatcher("/login_form.jsp");
rdp.forward(request, response);
}
}
<%@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>
<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>
<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>
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);
}
}
<%@ 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>
<body>
<c:if test="${requestScope.error_message!=null }">
실행 도중 오류가 발생했습니다.<br>
오류 내용 ${error_message }
</c:if>
<a href='/${initParam.context_root }/index.jsp'>메인페이지로 이동</a>
</body>
</html>
- 결과보기
'프로그래밍 > JSP Programming' 카테고리의 다른 글
2012-5-9 MVC 디자인 패턴 (0) | 2012.05.09 |
---|---|
2012-5-9 Filter(여러컴포넌트에서 공통적으로 처리해야하는 것을 뽑아내서 처리) (0) | 2012.05.09 |
2012-5-9 tag만을 이용한 회원등록,로그인,회원탈퇴페이지 만들기 (0) | 2012.05.09 |
2012-5-8 JSTL정의 및 태그 실습 (0) | 2012.05.08 |
2012-5-7 EL(Expression Language) - 표현식 언어 (0) | 2012.05.07 |