안녕하세요.
https://blog.naver.com/sysysy0302 여니입니다 :)
@WebServlet("/update.me")
public class MemberUpdateController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MemberUpdateController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 마이페이지로 이동
request.getRequestDispatcher("views/member/myPage.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 내정보변경
request.setCharacterEncoding("UTF-8");
String userId = request.getParameter("userId");
String userName = request.getParameter("userName");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String address = request.getParameter("address");
String[] interestArr = request.getParameterValues("interest");
String interest = "";
if(interestArr != null) {
interest = String.join(", " , interestArr);
}
Member m = new Member(userId, userName, phone, email, address, interest);
Member updateMember = new MemberService().updateMember(m);
if(updateMember == null) {// 실 패
request.setAttribute("errorMsg", "회원정보 수정에 실패했습니다.");
request.getRequestDispatcher("views/common/errorPage.jsp").forward(request, response);
}else { // 성 공
//session영역안에 업데이트된 사용자정보 담아주기
HttpSession session = request.getSession();
session.setAttribute("alertMsg", "성공적으로 회원정보를 수정했습니다.");
session.setAttribute("loginUser", updateMember);// 같은 키값으로 존재할수 없음 => 즉 덮어쓰기 가능
// 응답페이지 url 재요청
response.sendRedirect(request.getContextPath()+"/update.me");
}
MemberService
public int updateMember(Connection conn, Member m) {
//UPDATE => 처리된 행의 갯수
int result = 0;
PreparedStatement pstmt = null;
String sql = prop.getProperty("updateMember");
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, m.getUserName());
pstmt.setString(2, m.getPhone());
pstmt.setString(3, m.getEmail());
pstmt.setString(4, m.getAddress());
pstmt.setString(5, m.getInterest());
pstmt.setString(6, m.getUserId());
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
MemberDao
public int updateMember(Connection conn, Member m) {
//UPDATE => 처리된 행의 갯수
int result = 0;
PreparedStatement pstmt = null;
String sql = prop.getProperty("updateMember");
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, m.getUserName());
pstmt.setString(2, m.getPhone());
pstmt.setString(3, m.getEmail());
pstmt.setString(4, m.getAddress());
pstmt.setString(5, m.getInterest());
pstmt.setString(6, m.getUserId());
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
member-mapper.xml
<entry key="updateMember">
UPDATE MEMBER
SET USER_NAME = ? ,
PHONE = ? ,
EMAIL = ? ,
ADDRESS = ? ,
INTEREST = ?,
MODIFY_DATE = SYSDATE
WHERE USER_ID = ?
</entry>
'⋆ 。゜☁︎ 。⋆ 。゜☾゜。⋆⋆ 。゜☁︎ 。⋆ 。゜☾゜。⋆ > JSP' 카테고리의 다른 글
이클립스 Servlet (doPost 방식-delete문-update) (1) | 2023.07.05 |
---|---|
이클립스 Servlet (doPost 방식-updatePwd) (0) | 2023.07.05 |
이클립스 Servlet (doPost 방식-insert) (0) | 2023.07.05 |
JSP/ECLIPSE - 이클립스 Servlet 환경설정, 아파치톰캣 서버등록 (0) | 2023.06.26 |
JSP/ECLIPSE - Servlet(절대경로방식, 상대경로방식) (0) | 2023.06.26 |