36: Servlet modify.jsp

jk·2024년 2월 22일
0

kdt 풀스택

목록 보기
69/127



1. 아래를 구현하시오.

 - main.jsp 에서 회원정보 수정 버튼 넣음
 - 해당 수정 버튼을 누르면 modify.jsp 로 넘어감
 - modifyOK 에서 수정후 로그인 페이지로 넘어감
	//code1
	//./src/com/sejong/ex/dao/MemberDao.java
	public int updateMember(MemberDto member) {
		int result = MEMBER_LOGIN_FAIL;
		MemberDto dto = null;
		Connection connection = null;
		PreparedStatement pstmt = null;
		String query = "update members set pw = ?, email = ?, address = ? where id = ?";
		System.out.println(query + ";");
		try {
			connection = dataSource.getConnection();
			pstmt = connection.prepareStatement(query);
			pstmt.setString(1, member.getPw());
			pstmt.setString(2, member.getEmail());
			pstmt.setString(3, member.getAddress());
			pstmt.setString(4, member.getId());
			result = pstmt.executeUpdate();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
				};
				if (connection != null) {
					connection.close();
				};
			} catch(Exception e2) {
				e2.printStackTrace();
			};
		};
		return result;
	}
<!-- code2 -->
<!-- ./WebContent/main.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="com.sejong.ex.dto.MemberDto"
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main.jsp</title>
</head>
<body>
<h1>메인 페이지 입니다.</h1>
<%
	MemberDto member = (MemberDto)session.getAttribute("member");
	%>
<h1><%=member.getId()%>님 안녕하세요</h1>	
	<%
	if (member == null) {
		out.println("<a href=\"login.jsp\">로그인 페이지 이동</a>");
	} else {
		out.println("<a href=\"main.jsp\">로그인 페이지 이동</a>");
	};
%>
<form action="logout.jsp" method="post">
	<input type="submit" value="로그아웃">
	<input type="button" value="회원정보 수정" onclick="javascript:window.location='modify.jsp'">
</form>
</body>
</html>
<!-- code3 -->
<!-- ./WebContent/modify.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.sejong.ex.dto.MemberDto"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>modify.jsp</title>
</head>
<body>
<h1>회원정보 수정 페이지</h1>
<%
	MemberDto member = (MemberDto)session.getAttribute("member");
%>
<form action="modifyOK.jsp" method="post">
	아이디: <%=member.getId()%><br/>
	패스워드: <input type="password" name="pw" size="20"><br/>
	이메일: <input type="text" name="email" size="20"><br/>
	주소: <input type="text" name="address" size="20"><br/>
	<input type="submit" value="수정">
</form>
</body>
</html>
<!-- code4 -->
<!-- ./WebContent/modifyOK.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.sejong.ex.dao.MemberDao"
    import="com.sejong.ex.dto.MemberDto"
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>modifyOK.jsp</title>
</head>
<body>
<%
	request.setCharacterEncoding("utf-8");
	MemberDto member = (MemberDto)session.getAttribute("member");
	String id = member.getId();
	System.out.println(id);
	String pw = request.getParameter("pw");
	String email = request.getParameter("email");
	String address = request.getParameter("address");
	member = new MemberDto(id, pw, email, address);
	MemberDao dao = new MemberDao();
	int result = dao.updateMember(member);
	System.out.println(result);
	if (result == 0) {
		response.sendRedirect("login.jsp");
	} else {
		session.setAttribute("member", member);
		response.sendRedirect("main.jsp");
	};
%>
</body>
</html>
profile
Brave but clumsy

0개의 댓글