import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
public class LoginServlet5 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String pw = request.getParameter("pw");
if(id != null && pw != null) {
HttpSession session = request.getSession();
session.setAttribute("idKey", id);
}
response.sendRedirect("05.login.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>
</head>
<body>
<%
String id = (String)session.getAttribute("idKey");
if(id != null) {
%>
<%=id %>님 반갑습니다<p/>
<a href="05.logout.jsp">로그아웃</a>
<%
} else {
%>
<h1>로그인</h1>
<form action="LoginServlet" method="post">
ID : <input name="id"><p/>
PW : <input type="password" name="pw"><p/>
<input type="submit" value="로그인">
</form>
<%
}
%>
</body>
</html>
<%@ 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>
</head>
<body>
<%
session.invalidate();
response.sendRedirect("05.login.jsp");
%>
</body>
</html>