③ 로그아웃은 간단한게 쓰던 세션을 불러와서 무효화 해주면 된다.
● HttpSession session = request.getSession(false); //새 세션 말고 쓰던거 가져와라
● session.invalidate(); //세션 무효화
package member.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 logout
*/
@WebServlet("/member/logout")
public class logout extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public logout() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession(false); //새 세션 말고 쓰던거 가져와라
session.invalidate(); //세션 무효화
RequestDispatcher dis = request.getRequestDispatcher("/index.jsp");
dis.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);
}
}
④ 로그인 후 내 정보 보러 가기
<button><a href = "${pageContext.request.contextPath }/member/myinfo?id=${sessionScope.loginId }"> 내 정보 확인 </a></button><br/>
위의 버튼을 누르게 되면 하기와 같은 페이지로 이동하게 된다.
아래 주소창에서 보이는 것 처럼 {pageContext.request.contextPath }/member/myinfo?id=${sessionScope.loginId } = webApp3/member/myinfo?id=admin_id 이다
▶ Servlet 페이지 구현
package member.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 member.MemberService;
import member.memberVo;
/**
* Servlet implementation class MyInfo
*/
@WebServlet("/member/myinfo")
public class MyInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyInfo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("euc-kr");
response.setCharacterEncoding("euc-kr");
response.setContentType("text/html; charset = EUC-KR");
String id = request.getParameter("id");
MemberService service = new MemberService();
memberVo m = service.getMember(id);
request.setAttribute("m", m);
RequestDispatcher dis = request.getRequestDispatcher("/member/myinfo.jsp");
dis.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
request.setCharacterEncoding("euc-kr");
response.setCharacterEncoding("euc-kr");
response.setContentType("text/html; charset = EUC-KR");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
MemberService service = new MemberService();
service.Editmyinfo(new memberVo(id,pwd,name,null));
response.sendRedirect(request.getContextPath() + "/member/myinfo?id=" +id);
}
}
▶ JSP 페이지 구현
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
a:link { color: black; text-decoration: none;}
a:visited { color: black; text-decoration: none;}
a:hover { color: black; text-decoration: underline;}
</style>
</head>
<body>
<form action = "${pageContext.request.contextPath }/member/myinfo" method ="post" >
<h3> ${m.id }님의 정보</h3>
<table border = "1">
<tr><th>ID</th><td><input type = "text" name = "id" value = "${m.id }" readonly></td></tr>
<tr><th>PWD</th><td><input type = "password" name = "pwd" value = "${m.pwd }"></td></tr>
<tr><th>NAME</th><td><input type = "text" name = "name" value = "${m.name }"></td></tr>
<tr><th>EMAIL</th><td><input type = "text" name = "email" value = "${m.email }" readonly></td></tr>
<tr><th>수정</th>
<td><input type = "submit" value = "수정"> </td></tr>
<tr><th>탈퇴</th>
<td><button><a href = "${pageContext.request.contextPath }/member/out?id=${m.id }"> 탈퇴</a></button></td>
<tr><th>기타</th>
<td><button><a href = "${pageContext.request.contextPath }/member/result.jsp" >메인으로 돌아가기 </a></button></td> </tr>
</table>
</form>
</body>
</html>
'MVC > MVC 모델 실습' 카테고리의 다른 글
[MVC 실습] 로그인하고 게시판 운영하기_6 (0) | 2023.04.03 |
---|---|
[MVC 실습] 로그인하고 게시판 운영하기_5 (0) | 2023.04.03 |
[MVC 실습] 로그인하고 게시판 운영하기_4 (0) | 2023.04.03 |
[MVC 실습] 로그인하고 게시판 운영하기_2 (0) | 2023.04.03 |
[MVC 실습] 로그인하고 게시판 운영하기_1 (0) | 2023.04.03 |