리다이렉트와 포워드의 차이
리다이렉트 : 단순히 페이지를 이동시킴
포워드 : list, map, vo 등을 담아서 이동
모달 사용 시
https://adminlte.io/themes/v3/pages/UI/modals.html 참고
서버 세션을 사용하면 클라이언트(크롬)의 상태를 저장할 수 있음
쿠키와의 차이점은 세션은 웹 브라우저가 아닌 서버에 값을 저장한다는 점
웹 컨테이너(톰켓)에서 클라이언트(크롬)의 정보를 보관 및 상태를 유지할 때 사용
로그인한 사용자 정보를 유지하기 위한 목적
클라이언트마다 세션이 생성(크롬과 edge의 세션이 다름)
(웹 브라우저마다 세션이 따로 존재하기 때문에, 세션은 웹 브라우저와 관련된 정보를 저장하기에 알맞은 장소임)
웹 브라우저에 정보 보관 : 쿠키
(클라이언트 측의 데이터 보관소)
웹 컨테이너에 정보 보관 : 세션
(서버측의 데이터 보관소)
쿠키와 마찬가지로 세션도 생성을 해야만 정보를 저장할 수 있음
일단 세션을 생성하면 session 기본 객체를 통해서 세션을 사용할 수 있음
page 디렉티브의 session 속성 값을 true로 지정
-> 세션이 존재하지 않을 경우 세션이 생성되고, 세션이 존재할 경우 이미 생성된 세션을 접근
session 기본 객체를 이용해서 세션에 접근
-> session 기본값은 true(생략 가능)이므로 false로 하지 않는 이상 항상 세션 사용
속성 이용해서 클라이언트 관련 정보 저장
세션이 생성되면 session 기본 객체를 통해서 세션을 사용할 수 있음
메서드 리턴타입 설명
getId() String 세션의 고유 ID를 구함(세션ID)
getCreationTime() long 세션이 생성된 시간을 구함. 시간은 1970년 1월 1일 이후 흘러간 시간을 의미. 단위는 1/1000초
getLastAccessedTime() long 웹 브라우저가 가장 마지막에 세션에 접근한 시간을 구함. 시간은 1970년 1월 1일 이후 흘러간 시간을 의미. 단위는 1/1000초
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>세션 정보</title>
</head>
<body>
<%
//long 타입의 시간 값을 저장하기 위해 사용
Date time = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
%>
세션 ID : <%=session.getId() %> <br>
<%
//세션의 생성 시간을 Date 객체인 time에 저장
time.setTime(session.getCreationTime());
%>
세션 생성시간 : <%=formatter.format(time)%> <br>
<%
//세션의 마지막 접근 시간(long) -> Date 타입으로 세팅
time.setTime(session.getLastAccessedTime());
%>
최근 접근시간 : <%=formatter.format(time) %> <br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
session.setAttribute("MEMBERID", "ddit");
session.setAttribute("NAME", "개똥이");
%>
<!DOCTYPE html>
<html>
<head>
<title>세션에 정보 저장</title>
</head>
<body>
세션에 정보를 저장했습니다.<br>
<a href="getMemberInfo.jsp">저장된 세션의 정보 보기</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String memberId = (String)session.getAttribute("MEMBERID"); //ddit 가져올거야 , 결과는 object 형태이므로 형변환한다
String name = (String)session.getAttribute("NAME"); //개똥이 가져올거야
%>
<!DOCTYPE html>
<html>
<head>
<title>저장된 세션의 정보 확인</title>
</head>
<body>
회원 id : <%=memberId %><br>
회원 이름 : <%=name %><br>
<a href="setMemberInfo.jsp">되돌아가기</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>Session</title>
</head>
<body>
<form method="post" action="session_process.jsp">
<p>아 이 디 : <input type="text" name="id"></p>
<p>비밀번호 : <input type="password" name="passwd"></p>
<p><input type="submit" value="전송"></p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String memberId = (String)session.getAttribute("MEMBERID"); //ddit 가져올거야 , 결과는 object 형태이므로 형변환한다
String name = (String)session.getAttribute("NAME"); //개똥이 가져올거야
%>
<!DOCTYPE html>
<html>
<head>
<title>저장된 세션의 정보 확인</title>
</head>
<body>
회원 id : <%=memberId %><br>
회원 이름 : <%=name %><br>
<a href="setMemberInfo.jsp">되돌아가기</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String userID = (String)session.getAttribute("userID");
String userPW = (String)session.getAttribute("userPW");
%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1><%=userID %>님 반갑습니다.</h1>
<a href="session_out.jsp">로그아웃</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
session.invalidate();
response.sendRedirect("session.jsp");
%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1 class="display-3">로그인</h1>
</div>
</div>
<div class="container" align="center">
<div class="col-md-4 col-md-offset-4">
<h3 class="form-singin-heading">Please Sign in</h3>
<c:if test="${param.error eq '1'}">
<!--parma 목록 중에 error라는 목록이 있냐? 그 값이 1이 맞냐? -->
<div class="alert alert-danger">
아이디와 비밀번호를 확인해주세요.
</div>
</c:if>
</div>
<!-- j_security_check, j_username, j_password : 폼 기반 인증 처리 -->
<form class="form-signin" method="post"
action="j_security_check">
<div class="form-group">
<label for="inputUserName" class="sr-only">User Name</label>
<input type="text" class="form-control" placeholder="ID"
name="j_username" required autofocus />
</div>
<div class="form-group">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" class="form-control" placeholder="Password"
name="j_password" required autofocus />
</div>
<button class="btn btn btn-lg btn-success btn-block"
type="submit">로그인</button>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
//로그인 인증(login.jsp)에 실패 시 강제 이동
response.sendRedirect("login.jsp?error=1");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
//로그아웃
//security를 통해 로그인 인증을 할 때 웹 브라우저에 저장된 모든 사용자를 삭제함
session.invalidate();
response.sendRedirect("addProduct.jsp");
%>
프로그램이 처리되는 동안 특정한 문제가 발생 시 처리를 중단하고 다른 처리를 하는 것(오류 처리)
Page 디렉티브를 이용
<error-page>
<error-code>오류 코드</error-code>
<location>오류 페이지 URI</location>
</error-page>
<error-page>
<exception-type>예외 유형</exception>
<location>오류 페이지의 URI</location>
</error-page>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title>페이지 오류</title>
</head>
<body>
<!-- top 인클루드 시작 -->
<jsp:include page="/ch03/top.jsp" />
<!-- top 인클루드 끝 -->
<div class="jumbotron">
<div class="container">
<h2 class="alert alert-danger">요청하신 페이지를 찾을 수 없습니다.</h2>
</div>
</div>
<!--
중요!!!!!!!!!!!!!!!!
request.getRequestURL() : 오류 발생 시 해당 오류 페이지 경로를 출력
http://localhost:8090/ch04/addProduct.jsp
request.getQueryString() : 요청 파라미터(productId=P1234)
결과 : http://localhost:8090/ch04/addProduct.jsp?productId=P1234
-->
<div class="container">
<p><%=request.getRequestURL()%>?<%=request.getQueryString()%></p>
<p>
<a href="/ch04/products.jsp" class="btn btn-secondary">
상품 목록 »
</a>
</p>
</div>
<!-- bottom 인클루드 시작 -->
<jsp:include page="/ch03/bottom.jsp" />
<!-- bottom 인클루드 끝 -->
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<title>상품 아이디 오류</title>
</head>
<body>
<!-- top 인클루드 시작 -->
<jsp:include page="/ch03/top.jsp" />
<!-- top 인클루드 끝 -->
<div class="jumbotron">
<div class="container">
<h2 class="alert alert-danger">해당 상품이 존재하지 않습니다.</h2>
</div>
</div>
<!--
중요!!!!!!!!!!!!!!!!
request.getRequestURL() : 오류 발생 시 해당 오류 페이지 경로를 출력
http://localhost:8090/ch04/addProduct.jsp
request.getQueryString() : 요청 파라미터(productId=P1234)
결과 : http://localhost:8090/ch04/addProduct.jsp?productId=P1234
-->
<div class="container">
<p><%=request.getRequestURL()%>?<%=request.getQueryString()%></p>
<p>
<a href="/ch04/products.jsp" class="btn btn-secondary">
상품 목록 »
</a>
</p>
</div>
<!-- bottom 인클루드 시작 -->
<jsp:include page="/ch03/bottom.jsp" />
<!-- bottom 인클루드 끝 -->
</body>
</html>