12. JSP 스코프
[[ JSP에서의 스코프(영역 ***) ]]
1.application
- 모든 서블릿, jsp에서 공유하는 영역
- 웹서버가 종료될때까지 유효한 영역
- 값을 저장할때는 application.setAttribute("이름", 객체);
값을 꺼내올때는 Object ob = application.getAttribute("이름");
2. session(**)
- 웹브라우저가 종료될때까지 또는 일정유지시간(세션유지시간)동안 유효한 영역
- 값을 저장할때는 session.setAttribute("이름",객체);
값을 꺼내올때는 Object ob = session.getAttribute("이름");
- 사용예: 로그인 처리, 장바구니, ..
3. request(**)
- 클라이언트에 응답할때까지 유효
- 값을 저장할때는 request.setAttribute("이름",객체);
값을 꺼내올때는 Object ob = request.getAttribute("이름");
4. page
- 하나의 페이지에서만 유효(기본값으로 설정되어 있음)
1. application
1.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
//어플리케이션 스코프에 값 저장하기.
String user="song";
String pwd="1234";
application.setAttribute("user",user);
application.setAttribute("pwd",pwd);
%>
어플리케이션영역에 값 저장성공<br/>
<%
String a=(String)application.getAttribute("user");
String b=(String)application.getAttribute("pwd");
%>
user:<%=a %><br/>
pwd:<%=b %><br/>
<a href="2.jsp">2.jsp로 이동</a>
</body>
</html>
2.jsp
; 페이지 이동후에도 값 유지.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String a = (String)application.getAttribute("user");
String b = (String)application.getAttribute("pwd");
%>
user:<%=a %><br/>
pwd:<%=b %>
</body>
</html>
2. session(**)
1.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
//세션스코프에 값 저장하기.
session.setAttribute("id","song");
session.setAttribute("pwd","1234");
//세션에 저장된 값 꺼내오기
String id=(String)session.getAttribute("id");
String pwd=(String)session.getAttribute("pwd");
%>
<h1>세션에 저장된 데이터</h1>
id:<%=id %><br/>
pwd:<%=pwd %><br/>
<a href="2.jsp">2.jsp</a>
</body>
</html>
2.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String id=(String)session.getAttribute("id");
String pwd=(String)session.getAttribute("pwd");
%>
<h1>세션에 저장된 값</h1>
id:<%=id %><br/>
pwd:<%=pwd %>
</body>
</html>
3. request(**)
login.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="loginOk.jsp">
아이디<input type="text" name="id"/><br/>
비밀번호<input type="password" name="pwd"/><br/>
<input type="submit" value="로그인"/>
<input type="reset" value="취소"/>
</form>
</body>
</html>
loginOk.jsp
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="test.db.DBConnection"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
// 전송된 아이디와 비밀번호 얻어오기.
request.setCharacterEncoding("euc-kr");
String id=request.getParameter("id");
String pwd=request.getParameter("pwd");
//DB에 해당정보가 존재하는 지 검사하기.
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
boolean isMember = false;
try{
con=DBConnection.getCon();
String sql="select * from members where id=? and pwd=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
rs=pstmt.executeQuery();
if(rs.next()){
isMember=true;
}
}catch(SQLException se){
System.out.println(se.getMessage());
}finally{
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
}
if(isMember){
session.setMaxInactiveInterval(60);
session.setAttribute("id", id );
response.sendRedirect("../main.jsp");
}else{
%>
<script type="text/javascript">
alert("아이디/비밀번호가 맞지 않습니다.");
history.go(-1);
</script>
<%
}
%>
</body>
</html>