* 이름, 주민번호, 패스워드 바꾸기(ID, 마일리지 제외)
- servlet class
package : member.servlet
name : ModifyFormServlet
url-mapping : /modifyForm
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);
}
}
jsp
WebContent
이름 : /res/modify_form.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="/member_servlet/modifyMember" method="post">
<table width="500">
<tr>
<td>아이디</td><td><%=memberDTO.getId() %></td>
</tr>
<tr>
<td>비밀번호</td><td><input type="text" name="password"></td>
</tr>
<tr>
<td>이름</td><td><input type="text" name="name" value="<%=memberDTO.getName() %>"></td>
</tr>
<tr>
<td>주민번호</td><td><input type="text" name="registerNo1" value="<%=memberDTO.getRegisterNumber().substring(0,6) %>">-<input type="text" name="registerNo2" value="<%=memberDTO.getRegisterNumber().substring(6) %>"></td>
</tr>
<tr>
<td>마일리지<%=memberDTO.getMileage() %></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="정보수정"><input type="reset" value="다시작성"></td>
</tr>
</table>
</form>
</body>
</html>
--------------------------------
시작 : member_detail.jsp
로직 : ModifyFormServlet
성공 : 로그인 한 경우 : modify_form.jsp
실패 : 로그인 안한 경우 : login_form.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>
</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 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>
<%
MemberDTO memberDTO = (MemberDTO)session.getAttribute("memberDTO");
%>
<body>
<h2>회원정보수정</h2><hr><p>
<form action="/member_servlet/modifyMember" method="post">
<table width="500">
<tr>
<td>아이디</td><td><%=memberDTO.getId() %></td>
</tr>
<tr>
<td>비밀번호</td><td><input type="text" name="password"></td>
</tr>
<tr>
<td>이름</td><td><input type="text" name="name" value="<%=memberDTO.getName() %>"></td>
</tr>
<tr>
<td>주민번호</td><td><input type="text" name="registerNo1" value="<%=memberDTO.getRegisterNumber().substring(0,6) %>">-<input type="text" name="registerNo2" value="<%=memberDTO.getRegisterNumber().substring(6) %>"></td>
</tr>
<tr>
<td>마일리지<%=memberDTO.getMileage() %></td>
</tr>
<tr>
<td colspan="2" align="center"><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>
<%
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>
============================================================
- servlet class
package : member.servlet
name : ModifyFormServlet
url-mapping : /modifyMember
jsp
WebContent
이름 : /res/modify_success.jsp
--------------------------------
시작 : modify_form.jsp
로직 : ModifyMemberServlet
성공 : 처리 완료 한 경우 : modify_success.jsp(수정된 정보를 출력)
실패 : 로그인 안한 경우 : login_form.jsp
처리도중 exception : error.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="/member_servlet/modifyMember" method="post">
<table width="500">
<tr>
<td>아이디</td><td><%=memberDTO.getId() %></td>
</tr>
<tr>
<td>비밀번호</td><td><input type="text" name="password"></td>
</tr>
<tr>
<td>이름</td><td><input type="text" name="name" value="<%=memberDTO.getName() %>"></td>
</tr>
<tr>
<td>주민번호</td><td><input type="text" name="registerNo1" value="<%=memberDTO.getRegisterNumber().substring(0,6) %>">-<input type="text" name="registerNo2" value="<%=memberDTO.getRegisterNumber().substring(6) %>"></td>
</tr>
<tr>
<td>마일리지<%=memberDTO.getMileage() %></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 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 registerNo1 = request.getParameter("registerNo1");
String registerNo2 = request.getParameter("registerNo2");
String registerNo = registerNo1 + registerNo2;
// 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";
} 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>
<%
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>
</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>
- 결과
'프로그래밍 > JSP Programming' 카테고리의 다른 글
2012-5-3 jsp를 활용한 모든 회원 조회하기 (0) | 2012.05.03 |
---|---|
2012-5-2 jsp를 활용한 회원 탈퇴하기 (0) | 2012.05.02 |
2012-5-2 로그인한 회원정보 조회하기 (0) | 2012.05.02 |
2012-5-1 jsp를 이용한 회원등록하기 & 로그인, 로그아웃하기(session활용) (0) | 2012.05.02 |
2012-4-30 로그인 & 로그아웃하기 (0) | 2012.04.30 |