넣고 싶은 파일 : include01.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>
<h1>include 01 jsp</h1>
</body>
</html>
메인 파일 : include.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>
<main>
<h1>여기는 main입니다.</h1>
<%@include file="include01.jsp" %>
</main>
</body>
</html>
<%-- <%
String radius = request.getParameter("radius");
Circle circle = new Circle(Double.valueOf(radius));
%>
--%>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Main JSP</title>
</head>
<body>
<h1>메인 페이지입니다</h1>
<jsp:forward page="sub.jsp" />
<h1>꼬리말입니다</h1>
</body>
</html>
sub.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>
<h3>서브 페이지 입니다. sub jsp file</h3>
</body>
</html>
main.jsp (jsp:forward를 jsp:include로 바꿈)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Main JSP</title>
</head>
<body>
<h1>메인 페이지입니다</h1>
<jsp:include page="sub.jsp" />
<h1>꼬리말입니다</h1>
</body>
</html>
forward.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>forward</title>
</head>
<body>
<jsp:forward page="forward_param.jsp">
<jsp:param name="id" value="abcde" />
<jsp:param name="pw" value="1234" />
</jsp:forward>
</body>
</html>
forward_param.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>forward</title>
</head>
<body>
<%
String id, pw;
%>
<%
id = request.getParameter("id");
pw = request.getParameter("pw");
%>
ID : <%= id %><br />
PW : <%= pw %><br />
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
<style>
span{
display:inline-block;
width:40px;
text-align:center;
}
</style>
</head>
<body>
<form action="login_ok.jsp" method="post">
<span>ID</span> : <input type="text" name="id" size="10"/><br />
<span>PW</span> : <input type="text" name="pw" size="10"/><br />
<input type="submit" value="전송" />
</form>
</body>
</html>
login_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login ok</title>
</head>
<body>
<%!
String id, pw;
%>
<%
id = request.getParameter("id");
pw = request.getParameter("pw");
if(id.equals("abcd") && pw.equals("1234")){
Cookie cookie = new Cookie("id",id);
// 쿠키 객체 생성 후 아이디의 쿠키를 남긴다.
cookie.setMaxAge(60);
// 1시간 동안 웹 브라우저에 저장.
response.addCookie(cookie);
response.sendRedirect("welcome.jsp");
// redirect : 유저가 해당 페이지로 접속하게 한다.
}else{
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>welcome</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
for(int i=0; i<cookies.length; i++){
out.println(cookies[i].getValue() + "<br>");
}
// 쿠키 삭제 : setMaxAge(0)
%>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
<style>
span{
display:inline-block;
width:40px;
text-align:center;
}
</style>
</head>
<body>
<form action="loginok_session.jsp" method="post">
<span>ID</span> : <input type="text" name="id" size="10"/><br />
<span>PW</span> : <input type="text" name="pw" size="10"/><br />
<input type="submit" value="전송" />
</form>
</body>
</html>
loginok_session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>loginok_session</title>
</head>
<body>
<%!
String id, pw;
%>
<%
id = request.getParameter("id");
pw = request.getParameter("pw");
if(id.equals("abcd") && pw.equals("1234")){
session.setAttribute("id", id); // session은 내장 객체, 객체생성을 하지 않아도 만들어진다.
session.setAttribute("pw", pw);
response.sendRedirect("welcome_session.jsp");
}else{
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
welcome_session.jsp
<%@page import = "java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>welcome</title>
</head>
<body>
<%
String id = (String)session.getAttribute("id");
String pw = (String)session.getAttribute("pw");
// session의 return 타입은 Object
%>
<%= id %> 님이 접속했습니다.
비밀번호는 <%= pw %>
<hr />
<%
Enumeration enumeration = session.getAttributeNames();
while(enumeration.hasMoreElements()){
String sName = enumeration.nextElement().toString();
String sValue = (String)session.getAttribute(sName);
out.print(sName + " : " + sValue + "<br>");
}
out.println("======================<br>");
/* 특정 세션 데이터 삭제*/
session.removeAttribute("pw");
enumeration = session.getAttributeNames();
while(enumeration.hasMoreElements()){
String sName = enumeration.nextElement().toString();
String sValue = (String)session.getAttribute(sName);
out.print(sName + " : " + sValue + "<br>");
}
/* 모든 데이터 삭제 */
out.println("======================<br>");
session.invalidate();
// 세션 무효화 확인
if(request.isRequestedSessionIdValid()){
out.print("session valid");
}else{
out.print("session Invalid");
}
%>
</body>
</html>