로그인을 했다면 로그인한 상태에서 보여질 수 있는 메인페이지를 제작해야한다.
<main.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Member page</title>
</head>
<body>
<h2 style="text-align: center;">Member Page</h2>
<div style="text-align: center;">
<img src="images/bonobono.jpg" width="250px" height="300px">
</div>
</body>
</html>
멤버페이지에 보여지는 사진은 무작위로 선정해서 일단 메인페이지에 띄워놓는다.
로그인을 했으면 로그아웃을 할 수 있어야한다.
로그아웃을 눌렀을 때 로그아웃되는 servlet을 작성하자
package com.green.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LogoutServlet
*/
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LogoutServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = "login_form.jsp";
HttpSession session = request.getSession();
session.invalidate(); // 세션이 해지되고, 저장된 값이 제거됨.
request.getRequestDispatcher(url).forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
그리고 메인페이지에서 회원정보를 수정할 회원정보 페이지를 만들자
<mypage.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Member Information</title>
</head>
<body>
<form method="post" action="UpdateServlet" name="frm">
<table align="center" width="600">
<tr>
<td colspan="2">My Page</td>
</tr>
<tr>
<td>ID</td>
<td><input type="text" name="id" value="${loginUser.id }" readonly="readonly"></td>
</tr>
<tr>
<td>PASSWORD</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td>NAME</td>
<td><input type="text" name="name" value="${loginUser.name }"></td>
</tr>
<tr>
<td>LEVEL</td>
<td><select name="lev">
<c:choose>
<c:when test="${result == 2 }">
<option value="A" selected="selected">Admin</option>
<option value="B">Member</option>
</c:when>
<c:otherwise>
<option value="A">Admin</option>
<option value="B" selected="selected">Member</option>
</c:otherwise>
</c:choose>
</select></td>
</tr>
<tr>
<td>GENDER</td>
<td><select name="gender">
<c:choose>
<c:when test="${loginUser.gender == 1}">
<option value="1" selected="selected">Male</option>
<option value="2">Female</option>
</c:when>
<c:otherwise>
<option value="1">Male</option>
<option value="2" selected="selected">Female</option>
</c:otherwise>
</c:choose>
</select></td>
</tr>
<tr>
<td>PHONE</td>
<td><input type="text" name="phone" value="${loginUser.phone }"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="modify"> <input type="reset" value="cancle"></td></tr>
</table>
</form>
</body>
</html>
그리고 회원수정페이지로 넘어갈 수 있게 servlet파일을 만든다
<MyPageServlet..java>
package com.green.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyPageServlet
*/
@WebServlet("/MyPageServlet")
public class MyPageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyPageServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = "mypage.jsp";
request.getRequestDispatcher(url).forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
세션에 있는 loginUser 정보를 가져와 각각의 정보를 채워넣는다.
그러기 위해서는 저장된 정보를 가져오는 메소드가 필요하다.
EmployeeDAO에 저장된 정보를 불러오는 메소드를 추가한다.
<EmployeeDAO.getEmployee()>
// 아이디를 조건으로 회원정보를 조회해 온다.
public EmployeeVO getEmployee(String id) {
Connection conn = null;
PreparedStatement pstmt = null; // ready for select Query
ResultSet rs = null;
EmployeeVO employee = null;
String sql = "select * from employees where id = ?";
try {
// Connection Database
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) { // 조회한 데이터가 존재하므로 아이디는 존재한다는 의미
employee = new EmployeeVO();
employee.setId(rs.getString("id"));
employee.setPass(rs.getString("pass"));
employee.setName(rs.getString("name"));
employee.setLev(rs.getString("lev"));
employee.setEnter(rs.getDate("enter"));
employee.setGender(rs.getString("gender"));
employee.setPhone(rs.getString("phone"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBManager.close(conn, pstmt, rs);
}
return employee;
}
그리고 불러온 데이터를 회원수정페이지에 채워넣는다.
그리고 정보를 수정할 수 있는 메소드를 EmployeeDAO에 작성한다
<EmployeeDAO.updateEmployee()>
public int updateEmployee(EmployeeVO empVo) {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update employees set pass=?, name=?, lev=?, gender=?, phone=? where id=?";
int result = -1;
try {
conn = DBManager.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, empVo.getPass());
pstmt.setString(2, empVo.getName());
pstmt.setString(3, empVo.getLev());
pstmt.setString(4, empVo.getGender());
pstmt.setString(5, empVo.getPhone());
pstmt.setString(6, empVo.getId());
result = pstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
DBManager.close(conn, pstmt);
}
return result;
}
회원정보페이지에서 수정이 끝나고 수정버튼을 눌렀을 때, 업데이트할 수 있는 servlet을 만든다.
<UpdateServlet.java>
package com.green.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.green.dao.EmployeeDAO;
import employee.EmployeeVO;
/**
* Servlet implementation class UpdateServlet
*/
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String url = "";
EmployeeVO empVo = new EmployeeVO();
empVo.setId(request.getParameter("id"));
empVo.setPass(request.getParameter("pass"));
empVo.setName(request.getParameter("name"));
empVo.setLev(request.getParameter("lev"));
empVo.setGender(request.getParameter("gender"));
empVo.setPhone(request.getParameter("phone"));
EmployeeDAO empDao = EmployeeDAO.getInstance();
int result = empDao.updateEmployee(empVo);
if (result > 0) {
request.setAttribute("message", "success modify");
url = "update_done.jsp";
//check updated information
EmployeeVO newEmp = empDao.getEmployee(empVo.getId());
request.setAttribute("employee", newEmp);
// modify information on session
HttpSession session = request.getSession();
session.setAttribute("loginUser", newEmp);
request.getRequestDispatcher(url).forward(request, response);
} else {
request.setAttribute("message", "error modify");
}
}
}
그리고나서 업데이트가 완료되었는지 업데이트 된 내용을 보여주는 페이지를 제작하여 마무리한다.
<update_done.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="header.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Member Information</title>
</head>
<body>
<form method="post" action="UpdateServlet" name="frm">
<table align="center" width="600">
<tr>
<td colspan="2"><h3>Member Information</h3><span style="color:red;">${message }</span></td>
</tr>
<tr>
<td>ID</td>
<td>${employee.id }</td>
</tr>
<tr>
<td>PASSWORD</td>
<td>${employee.pass }</td>
</tr>
<tr>
<td>NAME</td>
<td>${employee.name }</td>
</tr>
<tr>
<td>LEVEL</td>
<td>
<c:choose>
<c:when test="${employee.lev=='A'}">
Admin
</c:when>
<c:otherwise>
Member
</c:otherwise>
</c:choose>
</td>
</tr>
<tr>
<td>GENDER</td>
<td>
<c:choose>
<c:when test="${employee.gender == '1'}">
Male
</c:when>
<c:otherwise>
Female
</c:otherwise>
</c:choose>
</td>
</tr>
<tr>
<td>PHONE</td>
<td>${employee.phone }</td>
</tr>
<tr align="center">
<td colspan="2"><input type="button" value="Main Page" onclick="location.href='login_form.jsp'"></td></tr>
</table>
</form>
</body>
</html>
수정이 완료됐으면 메인페이지로 넘어가게한다.
지금 확인 페이지에서 버튼을 누르면 로그인페이지로 넘어가게 되어있지만
추후에 로그인페이지에서 세션확인 후 메인페이지로 넘어가는 코드를 수정한다.
'JSP, Servlet > JSP를 이용하여 회원관리 페이지 만들기' 카테고리의 다른 글
[JSP] 회원관리 페이지 만들기 - 회원등록 페이지 (0) | 2020.09.09 |
---|---|
[JSP] 회원관리 페이지 만들기 - 데이터 설정 및 데이터 연결 (0) | 2020.09.03 |
[JSP] 회원관리 페이지 만들기 - 로그인 폼 만들기 (0) | 2020.09.03 |