JSP 2. 기초, 쿠키와 세션
request.setAttribute("key", "value");
RequestDispatcher rd = request.getRequestDispatcher("xxx.jsp");
rd.forward(request, response);
xxx.jsp -> $("key")
int arr[] = new int[5];
<c:forEach var="num" items="${arr}">
${num}<br>
</c:forEach>
쿠키와 세션
쿠키: 웹서버가 웹브라우저로 데이터를 보냈다가 웹서버 쪽으로 다시 되돌려 받는 방법
response.addCookie(new Cookie("key", "value"));
response.addCookie(new Cookie("key1", "value1"));
Cookie cookies[] = request.getCookiese();
cookie.getName();
cookie.getValue();
쿠키 유지시간 설정
cookie.setMaxAge(3600);
특정 경로명의 URL로만 전송
cookie.setPath("/path1/path/")
쿠키 getValue
private String getCookieValue(Cookie[] cookies, String name) {
String value= null;
if(cookies == null)
return null;
for(Cookie cookie : cookies) {
if(cookie.getName().equals(name))
return cookie.getValue();
}
return null;
}
세션: 웹브라우저를 거치지 않고 웹서버에 있는 데이터 영역을 통해 데이터를 전달하는 방법
HttpSession session = request.getSession();
session.setAttribute("key", "value");
session.getAttribute("key");
session.removeAttribute("key");
session.invalidate();