login.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="LoginCheck">
ID : <input type="text" name="id"><br>
PW : <input type="password" name="pw"><br>
<input type="submit" value="로그인">
</form>
</body>
</html>
LoginCheck.java
package com;
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;
@WebServlet("/LoginCheck")
public class LoginCheck extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println(id);
System.out.println(pw);
if (id.equals("ThisIsAdminId") && pw.equals("1234")) { // 간단하게 로그인 구현
HttpSession session = request.getSession();
session.setAttribute("id", id);
session.setMaxInactiveInterval(100); // session 나이설정
response.sendRedirect("LoginSuccess.jsp");
} else {
response.sendRedirect("Login.jsp");
}
}
}
loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String id = (String) session.getAttribute("id"); // Object의 장점 : 다형성
if (id != null) { // login체크로 url을 알더라도 들어오는 것을 방지
out.print(id + "님 환영합니다.");
out.print("<a href='Logout'>로그아웃</a>");
} else {
response.sendRedirect("Login.jsp");
}
%>
</body>
</html>
Logout.java
package com;
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;
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("id");
response.sendRedirect("Login.jsp");
}
}
'Programming > Servlet & JSP' 카테고리의 다른 글
방명록(어플리케이션) (0) | 2020.02.20 |
---|---|
장바구니(쿠키) (0) | 2020.02.20 |
전화번호 관리 (0) | 2020.02.20 |
쿠키 확인, 생성, 삭제 (0) | 2020.02.19 |
멤버 관리 (jsp, servlet, 로그인, 회원 가입, 정보 관리) (0) | 2020.02.19 |