본문 바로가기
네이버클라우드/JAVA 웹 프로그래밍

JAVA 59일차 (2023-08-14) 자바 프로그래밍_로그아웃 구현하기_개인프로젝트 - 마트 관리 시스템

by prometedor 2023. 8. 15.

=>

=>

LogoutServlet.java

package bitcamp.report.handler;

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;

@WebServlet("/auth/logout")
public class LogoutServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    request.getSession().invalidate();
    response.sendRedirect("/");
  }
}

 

 

=>

=>

HomeServlet.java

package bitcamp.report.handler;

import java.io.IOException;
import java.io.PrintWriter;
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 bitcamp.report.vo.Member;

@WebServlet("/index.html")
public class HomeServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE html>");
    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");
    out.println("<meta charset='UTF-8'>");
    out.println("<title>비트캠프</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>MyApp2</h1>");
    out.println("<ul>");
    out.println("  <li><a href='/member/list'>회원</a></li>");
    out.println("  <li><a href='/board/list?category=1'>게시판</a></li>");
    out.println("  <li><a href='/board/list?category=2'>공지사항</a></li>");

    Member loginUser = (Member) request.getSession().getAttribute("loginUser");
    if (loginUser == null) {
      out.println("  <li><a href='/auth/form.html'>로그인</a></li>");
    } else {
      out.printf("  <li>%s <a href='/auth/logout'>로그아웃</a></li>", loginUser.getName());
    }
    out.println("</ul>");
    out.println("</body>");
    out.println("</html>");

  }
}

 

=>

=>

index2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mart Management</title>
</head>
<body>
<h1>마트 관리 시스템</h1>
<ul>
  <li><a href="/member/list">직원</a></li>
  <li><a href="/item/list">물품</a></li>
  <li><a href="/board/list?category=1">게시글</a></li>
  <li><a href="/board/list?category=2">공지사항</a></li>
  <li><a href="/auth/form.html">로그인</a></li>
</ul>
</body>
</html>

ㄴ index.html 파일이름을 index2.html 로 변경하기

=>

테스트

=>

=>

ㄴ 로그인 선택

=>

=>

=>

ㄴ 게시판에 들어가서 새 글 등록

=>

ㄴ 새 글 등록이 제대로 됨을 확인 후 메인으로 이동

=>

ㄴ 로그아웃 선택

=>

ㄴ 로그아웃이 됨을 확인

=>

=>

ㄴ 로그아웃 된 상태이므로 로그인 창이 노출됨