반응형

- 내장 객체란?

JSP 파일이 자바 소스 파일로 변환될때 _jspServiece 메소드 내에 자동으로 선언 및 초기화하는 변수를 뜻합니다.

 

- request, response

 

request = HttpServeletRequest 타입으로 요청 정보 처리

response = HttpServletResponse 타입으로 응답 정보 처리

 

예제 )

 

html

1
2
3
4
5
6
7
<body>
<br>
<form action="irum.go" method="post">
자료 입력 : <input type="text" name="data">
<input type="submit">
</form>
</body>
 

 

jsp

1
2
3
4
5
6
7
8
9
<body>
<br>
<%
request.setCharacterEncoding("utf-8");
 String data = request.getParameter("data");
 
%>
<%=data %>
</body>
 

 

출력 결과

 

 

- Session

 

session은 javax.servlet.http.HttpSession의 내장 객체로 클라이언트마다 하나씩 생성, 정보 유지시 사용합니다.

 

ex ) 

 

HTML

1
2
3
4
5
6
7
8
<body>
* 자료 입력 *
<br>
<form action="j6session2.jsp" method="post">
id : <input type="text" name="id"><p/>
<input type="submit" value="로그인">
</form>
</body>
 

 

JSP1

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
session.setAttribute("idkey", id);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>세션 연습 중</h2>
<form action="j6session2.jsp" method="post">
 
<input type="submit" value="결과보기">
 
</form>
</body>
 

 

jsp2

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>선택 결과 보기</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String id = (String)session.getAttribute("idkey");
if(id != null) {
%>
<%=id %> 님입니다.<br><br>
세션 아이디 : <%=session.getId() %> <br><br>
<%
else {
    out.println("이미 연결 되어있습니");
}
%>
</body>
</html>
 

 

Html 에서의 name ="id"는 jsp1의 3번째 줄로 가서 4번째쭐의 session.Attrivute "idKey"에 저장됩니다. 그 후 결과 보기를 다시 눌러주면 jsp2의 12번째줄로 넘어갑니다.

이후 15번째 줄에 html에서 읿력한 id 값이 출력 됩니다.

 

출력 결과는 다음과 같습니다.

 

 

반응형

'JSP·Servlet' 카테고리의 다른 글

JSP Beans  (0) 2020.05.03
JSP 에서 사용하는 태그  (0) 2020.05.03
JSP란 ?  (0) 2020.05.03

+ Recent posts