6 JSP 6 JSP u u u request

  • Slides: 35
Download presentation
6. JSP 의 내부 객체 제 6장 JSP의 내부 객체 u u u 내부

6. JSP 의 내부 객체 제 6장 JSP의 내부 객체 u u u 내부 객체란? request, response, out 내부 객체 session, application, page. Context 내부 객체 page, config 내부 객체 exception 내부 객체

n JSP 페이지의 내부 객체 Type 설명 request javax. servlet. http. Http. Servlet. Request

n JSP 페이지의 내부 객체 Type 설명 request javax. servlet. http. Http. Servlet. Request 파라미터를 포함한 요청을 담고 있는 객체 response javax. servlet. http. Http. Servlet. Response 요청에 대한 응답을 담고 있는 객체 out javax. servlet. jsp. Jsp. Writer 페이지 내용을 담고 있는 출력 스트림 객체 session javax. servlet. http. Http. Session 세션 정보를 담고 있는 객체 application javax. servlet. Servlet. Context 애플리케이션 Context의 모든 페이지가 공유할 데이터를 담고 있는 객체 page. Context javax. servlet. jsp. Page. Context 페이지 실행에 필요한 Context 정보를 담고 있는 객체 page javax. servlet. jsp. Http. Jsp. Page JSP 페이지의 서블릿 객체 config javax. servlet. Servlet. Config JSP 페이지의 서블릿 설정 데이터 초기화 정보 객체 exception java. lang. Throwable JSP 페이지의 서블릿 실행시 처리하지 못한 예외 객체

request 예제 //Request. Example 1. html 01 <html> 02 <body> 03 <h 1>Request Example

request 예제 //Request. Example 1. html 01 <html> 02 <body> 03 <h 1>Request Example 1</h 1> 04 <FORM METHOD=POST ACTION="Request. Example 1. jsp"> 05 성명 : <INPUT TYPE="text" NAME="name"> 06 학번 : <INPUT TYPE="text" NAME="student. Num"> 07 성별 : 남자 <INPUT TYPE="radio" NAME="sex" VALUE="man" checked> 08 여자 <INPUT TYPE="radio" NAME="sex" VALUE="woman"> 09 전공 : <SELECT NAME="major"> 10 <OPTION SELECTED VALUE="국문학과">국문학과</OPTION> 11 <OPTION VALUE="영문학과">영문학과</OPTION> 12 <OPTION VALUE="수학과">수학과</OPTION> 13 <OPTION VALUE="정치학과">정치학과</OPTION> 14 <OPTION VALUE="체육학과">체육학과</OPTION> 15 </SELECT> 16 <INPUT TYPE="submit" value="보내기"> 17 </FORM> 18 </body> 19 </html>

//Request. Example 1. jsp 01 <h 1>Request Example 1</h 1> 02 <%@ page content.

//Request. Example 1. jsp 01 <h 1>Request Example 1</h 1> 02 <%@ page content. Type="text/html; charset=EUC-KR"%> 03 <% 04 request. set. Character. Encoding("euc-kr"); //요청된 값들의 인코딩 타입을 한글로 인식하기 위한 부분 05 %> 06 <% 07 String name = request. get. Parameter("name"); 08 String student. Num = request. get. Parameter("student. Num"); 09 String sex = request. get. Parameter("sex"); 10 String major = request. get. Parameter("major"); //Request. Example 1. htm 페이지에서 요청한 파라미터 값들을 //request객체의 get. Parameter 메서드로 리턴 시킴 11 12 if(sex. equals("man")){ 13 sex = "남자"; 14 }else{ 15 sex = "여자"; 16 } 17 %> 18 <body> 19 성명 : <%=name%><p> 20 학번 : <%=student. Num%><p> 21 성별 : <%=sex%><p> 22 학과 : <%=major%> 23 </body> 2. 1 request 예제

01 02 03 04 05 06 07 08 09 10 11 12 13 14

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <%@ page content. Type="text/html; charset=EUC-KR"%> <% String protocol = request. get. Protocol(); String server. Name = request. get. Server. Name(); int server. Port = request. get. Server. Port(); String remote. Addr = request. get. Remote. Addr(); String remote. Host = request. get. Remote. Host(); String method = request. get. Method(); String. Buffer request. URL = request. get. Request. URL(); String request. URI = request. get. Request. URI(); String use. Browser = request. get. Header("User-Agent"); String file. Type = request. get. Header("Accept"); %> <html> <body> <h 1>Request Example 2</h 1> 프로토콜 : <%=protocol%><p> 서버의 이름 : <%=server. Name%><p> 서버의 포트 번호 : <%=server. Port%><p> 사용자 컴퓨터의 주소 : <%=remote. Addr%><p> 사용자 컴퓨터의 이름 : <%=remote. Host%><p> 사용 method : <%=method%><p> 요청 경로(URL): <%=request. URL%><p> 요청 경로(URI) : <%=request. URI%><p> 현재 사용하는 브라우저 : <%=use. Browser%><p> 브라우저가 지원하는 file의 type : <%=file. Type%><p> </body> </html> 2. 1 request 예제

response n 클래스 q n 주요 기능 q n javax. servlet. http. Http. Servlet.

response n 클래스 q n 주요 기능 q n javax. servlet. http. Http. Servlet. Response 사용자 요청에 대한 응답 처리, 페이지 전환 Response 객체의 요청 메소드 메서드 설명 void set. Header(name, value) 응답에 포함될 Header를 설정한다. void set. Content. Type(type) 출력되는 페이지의 content. Type 설정한다. String get. Character. Encoding() 응답 페이지의 문자 인코딩 Type을 리턴한다. void send. Redirect(url) 지정된 URL로 요청을 재전송한다

response 예제 //Response. Example 1. jsp 01 <h 1>Response Example 1</h 1> 02 <%

response 예제 //Response. Example 1. jsp 01 <h 1>Response Example 1</h 1> 02 <% 03 response. send. Redirect("Response. Example 1_1. jsp"); 04 //response. send. Redirect("http: //localhost: 8080/ Response. Example 1_1. jsp"); //지정한 URL로 응답을 보낸다. //URL은 절대 URL 또는 상대 URL로 지정을 할 수 있다. 05 %>

//Response. Example 1_1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR"%> 02 <% 03 response.

//Response. Example 1_1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR"%> 02 <% 03 response. set. Header("Pragma", "no-cache"); 04 if(request. get. Protocol(). equals("HTTP/1. 1")){ 05 response. set. Header("Cache-Control", "no-cache"); 06 } //HTTP 1. 1과 이전의 버전까지 value 값을 "no-cache"로 지정하여 // 브라우저가 캐시된 문서를 사용하지 않고 클라이언트가 요청할 때 마다 //서버에 새로운 문서를 전송받도록 설정. 07 %> 08 <html> 09 <body> 10 <h 1>Response Example 1</h 1> 11 http: //localhost: 8080/Response. Example 1. jsp가<p> 12 http: //localhost: 8080/Response. Example 1_1. jsp로 변경되었습니다. 13 </body> 14 </html>

out 예제 //Out. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 buffer="5

out 예제 //Out. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 buffer="5 kb" 03 %> //page 지시자의 buffer 속성은 JSP 페이지의 출력 크기를 킬로바이트 단위로 //지정하는 속성이며 기본값은 “ 8 KB”이지만 이 페이지는 “ 5 kb”로 지정 04 <% 05 int total. Buffer = out. get. Buffer. Size(); 06 int remain. Buffer = out. get. Remaining(); 07 int use. Buffer = total. Buffer - remain. Buffer; //out 객체가 제공하는 버퍼에 관련된 메서드로 현재 실행된 페이지 bufffer의 크기, // 남은 크기, 현재 사용량을 구하고 있다. 08 %> 09 <h 1>Out Example 1</h 1> 10 <b>현재 페이지의 Buffer 상태</b><p> 11 출력 Buffer의 전체 크기 : <%=total. Buffer%>byte<p> 12 남은 Buffer의 크기 : <%=remain. Buffer%>byte<p> 13 현재 Buffer의 사용량 : <%=use. Buffer%>byte<p>

session 예제 //Session. Example 1. html 01 <html> 02 <body> 03 <h 1>Session Example

session 예제 //Session. Example 1. html 01 <html> 02 <body> 03 <h 1>Session Example 1</h 1> 04 <FORM METHOD=POST ACTION="Session. Example 1. jsp"> 05 아이디 : <INPUT TYPE="text" NAME="id"><p> 06 비밀번호 : <INPUT TYPE="password" NAME="password"><p> 07 <INPUT TYPE="submit" VALUE="로그인"> 08 </FORM> 09 </body> 10 </html>

//Session. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 session="true" //현재 페이지의

//Session. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 session="true" //현재 페이지의 세션의 사용 여부를 설정하는 page 지시자 //지정하지 않아도 기본값으로 true값이 설정 03 %> 04 <% 05 request. set. Character. Encoding("euc-kr"); 06 %> 07 <% 08 String id = request. get. Parameter("id"); 09 String password = request. get. Parameter("password"); //Session. Example 1. html에서 요청한 값들을 request객체의 get. Parameter 메서드로 리턴 10 11 session. set. Attribute("id. Key", id); //09라인에서 입력을 받은 id값을 id. Key로 session 객체에 연결을 설정 12 session. set. Max. Inactive. Interval(60*5); //session 객체의 연결 시간을 5(60*5)분으로 설정. //만약 연결 후 5분이 경과가 되면 session 객체는 자동으로 연결 속성을 종료 13 %> 14 <h 1>Session Example 1</h 1> 15 <FORM METHOD=POST ACTION="Session. Example 1_1. jsp"> 16 1. 가장 좋아하는 계절은? 17 <INPUT TYPE="radio" NAME="season" VALUE="봄">봄 18 <INPUT TYPE="radio" NAME="season" VALUE="여름">여름 19 <INPUT TYPE="radio" NAME="season" VALUE="가을">가을 20 <INPUT TYPE="radio" NAME="season" VALUE="겨울">겨울<p> 21 22 2. 가장 좋아하는 과일은? 23 <INPUT TYPE="radio" NAME="fruit" VALUE="watermelon">수박 24 <INPUT TYPE="radio" NAME="fruit" VALUE="melon">메론 25 <INPUT TYPE="radio" NAME="fruit" VALUE="apple">사과 26 <INPUT TYPE="radio" NAME="fruit" VALUE="orange">오렌지<p> 27 <INPUT TYPE="submit" VALUE="결과보기"> 3. 1 session 예제

//Session. Example 1_1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR"%> 02 <% 03 request.

//Session. Example 1_1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR"%> 02 <% 03 request. set. Character. Encoding("euc-kr"); 04 %> 05 <h 1>Session Example 1</h 1> 06 <% 07 String season = request. get. Parameter("season"); 08 String fruit = request. get. Parameter("fruit"); // Session. Example 1. jsp에서 요청한 값들을 request객체의 get. Parameter 메서드로 리턴 09 String id = (String)session. get. Attribute("id. Key"); // session 객체에서 id. Key로 연결된 값을 문자열 id로 리턴 // 결과적으로 Session. Example 1. html에서 입력한 id값을 session에 저장을 하였다가 다시 현재 페이지에서 리턴 10 String session. Id = session. get. Id(); //세션의 세션 ID를 리턴합니다. //세션 ID는 요청하는 클라이언트들의 구분을 위해 JSP 컨테이너가 제공하는 고유한 값으로 할당 11 interval. Time = session. get. Max. Inactive. Interval(); //set. Max. Inactive. Interval(time) 로 지정된 값을 리턴한다. 지정한 값이 없으면 기본값으로 1800(초)으로 리턴 12 13 if(id != null){ // session 객체에 id 값이 null 값이 아니면 if문 이하가 출력이 되지만 // session 객체에 id값이 없으면 else문 이하의 내용이 출력 14 %> 15 <b><%=id%></b>님이 좋아하시는 계절과 과일은<p> 16 <b><%=season%></b>과 <b><%=fruit%></b> 입니다. 17 세션 ID : <%=session. Id%><p> 18 세션을 유지 시간 : <%=interval. Time%>초<p> 19 <% 20 session. invalidate(); // session 객체를 종료 21 }else{ 22 out. println("세션의 시간이 경과를 하였거나 다른 이유로 연결을 할 수가 없습니다. "); //session의 시간 경과 또는 다른 이유로 session 객체의 연결이 종료 23 } 3. 1 session 예제

application 예제 //Application. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR"%> 02 <%

application 예제 //Application. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR"%> 02 <% 03 String server. Info = application. get. Server. Info(); // 서블릿 컨테이너의 이름과 버전을 리턴 04 String mime. Type = application. get. Mime. Type("Request. Example 1. html"); // Request. Example 1. html 파일의 MIME 타입을 리턴 05 String real. Path = application. get. Real. Path("/"); // ROOT directory를 로컬 파일 시스템 경로로 변경 하여 리턴 06 application. log("application 내부 객체 로그 테스트"); // 파라미터로 지정된 문자열을 로그 파일에 저장 07 %> 08 <h 1>Application Example 1</h 1> 09 서블릿 컨테이너의 이름과 버전 : <%=server. Info%><p> 10 Request. Example 1. html의 MIME Type : <%=mime. Type%><p> 11 로컬 파일 시스템 경로 : <%=real. Path%><p>

o page. Context n 클래스 q n javax. servlet. jsp. Page. Context 주요 기능

o page. Context n 클래스 q n javax. servlet. jsp. Page. Context 주요 기능 q 현재 JSP 페이지의 Context를 나타내며, page. Context 내부 객체를 통해서 다른 내부 객체에 접근 n q include, forward 액션의 실제구현에 응용됨 n n q 예) Jsp. Writer page. Out = page. Context. get. Out(); page. Context. forward(“해당URL”); page. Context. include(“해당URL”); <%@page language=java. . . %>인 경우 크게 의미는 없음

page. Context 예제 //in. For. Test. jsp <%@ page content. Type="text/html; charset=euc-kr" %> <html><body>

page. Context 예제 //in. For. Test. jsp <%@ page content. Type="text/html; charset=euc-kr" %> <html><body> 안녕하세요. <% if(request. get. Parameter("id"). equals("j. Forward")){ page. Context. forward("forward. jsp"); }else if(request. get. Parameter("id"). equals("j. Include")){ page. Context. include("include. jsp"); }else{ %> <%=request. get. Parameter("id") %>님, forward와 include를 테스트하는 페이지입니다. get. Parameter의 id를 설정해주세요. forward를 테스트하려면 j. Forward를, include를 테스트하려면 j. Include를 입력하세요. <% } %> 그럼, 즐JSP하세요!!! </body></html>

page 예제 //Page. Example 1. jsp 01 <%@ page info = "JSPStudy. co. kr"

page 예제 //Page. Example 1. jsp 01 <%@ page info = "JSPStudy. co. kr" 02 content. Type="text/html; charset=EUC-KR"%> 03 <% 04 String page. Info = this. get. Servlet. Info(); //page 객체 자신을 나타내는 this 라는 키위드(예약어)로 //page 지시자의 info 속성 값을 리턴 05 %> 06 <h 1>Page Example 1</h 1> 07 현재 페이지의 info값 : <%=page. Info%>

config 예제 //Config. Test. jsp <%@ page content. Type="text/html; charset=euc-kr" %> <html> <body> <h

config 예제 //Config. Test. jsp <%@ page content. Type="text/html; charset=euc-kr" %> <html> <body> <h 2> 내장객체 config를 사용한 정보출력 </h 2> <h 3> <% out. println("서블릿 이름 : " + config. get. Servlet. Name()+" "); Servlet. Context context = config. get. Servlet. Context(); out. println("서버버전 : "+context. get. Major. Version()+". "+ context. get. Minor. Version()); %> </h 3> <body> <html>

exception 예제 //Exception. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 error.

exception 예제 //Exception. Example 1. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 error. Page="Exception. Example 2. jsp" 03 %> //현재 페이지의 실행시 예외가 발생하면 //Exception. Example 2. jsp 페이지에서 처리 하게끔 설정 04 <% 05 int one = 1; 06 int zero = 0; 07 %> 08 <h 1>Exception Example 1</h 1> 09 one / zero = <%=one/zero%><p>

exception 예제 //Exception. Example 2. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 is.

exception 예제 //Exception. Example 2. jsp 01 <%@ page content. Type="text/html; charset=EUC-KR" 02 is. Error. Page="true" 03 %> //에러 페이지로 지정하기 위해서 is. Error. Page 속성을 true로 설정 04 <% 05 String message = exception. get. Message(); // 예외가 발생한 페이지의 예외 메시지를 리턴 06 String object. Message = exception. to. String(); // 예외가 발생한 페이지의 에러 실체의 클래스명과 예외 메시지를 리턴 07 %> 08 <h 1>Exception Example 1</h 1> 09 에러 메세지 : <b><%=message%></b><p> 10 에러 실체의 클래스명과 에러 메세지 : <b><%=object. Message%></b>