out 객체


: 서블릿/JSP 컨테이너가 응답 페이지를 만들기 위해 사용하는 출력 스트림 객체로

  java.io.Writer 클래스를 상속한 javax.servlet.jsp.JspWriter 클래스 타입으로

  선언됨

  

  out 객체를 사용하지 않고도 표현식(<%= %>) 를 이용해서 자바코드의

  변수 값들과 메소드의 리턴값들을 출력할수 있습니다.

  

  리턴 타입            메소드                  용도

  

  null(없음)           clear( )             출력 버퍼에 저장된 내용을 버림 이미 다 채워져서

                                                    클라이언트로 전송되었을 경우 IOException 발생

  null             clearBuffer( )          출력 버퍼에 저장된 내용을 버림 clear( ) 메소드와 달리

                                                    버퍼에 담긴 내용이 이미 전송된 이후에도 예외를

                                                    발생시키지 않고 현재 저장되어 있는 버퍼만 버림

  

  null                    flush( )             현재 버퍼에 저장되어 있는 내용을 클라이언트로

                                                     전송하고 출력 스트림을 종료함

                                                     

  boolean      isAutoFlush()           Page 지시어의 autoFlush 속성으로 지정된 값을

                                                     리턴함 즉 출력 버퍼가 다 채워졌을때 버퍼 내용을

                                                     클라이언트로 전송하도록 지정되어 있으면 true

                                                     리턴하고 출력 버퍼가 다 채워졌을때 예외가

                                                     발생하도록 지정되어 있으면 false 를 리턴함

                                                     

  null          print(String str)          출력 스트림으로 str 문자열을 출력함

  

  null          println(String str)       출력 스트림으로 str 문자열을 출력하고 \n

  

  null          newline(String str)     출력 스트림으로 str 문자열을 출력하고 \r\n



1. out 객체 사용


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>out 객체 사용</title>

</head>

<body>

<%

out.println("이부분은 출력되지 않습니다.");

out.clear(); // 버퍼의 내용 제거


%>


<H2> 현재 페이지의 출력 버퍼의 상태</H2>


초기 출력 버퍼의 크기 :<%= out.getBufferSize() %> byte<P>

남은 출력 버퍼의 크기 :<%= out.getRemaining() %> byte<P>


autoFlush : <%=out.isAutoFlush() %><P>


</body>

</html>



2. out 객체 사용 autoflush Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>out 객체 사용 autoflush Test</title>

</head>

<body>


<%@ page autoFlush="false" buffer="1kb" %>


<H2> 현재 autoFlush = <%= out.isAutoFlush() %> </H2><P>


<%

for(int i = 1; i < 25; i++)

{

out.println("남은 출력 버퍼의 크기(out.getRemaining()) : " + out.getRemaining()+ "<br>");

// autoFlush() 가 false 이면 프로그래머가 버퍼를 출력해야 한다.

if(out.getRemaining()<50)

{

out.println("<br>");

out.flush();

}

}

%>


</body>

</html>



. application 객체


: 내장 객체 application 은 javax.servlet.ServletContext 인터페이스 자료유형으로

  웹 어플리케이션에서 유지 관리되는 여러 환경 정보를 관리한다.

  

  웹 어플리케이션이란 여러개의 서블릿과 JSP 로 구성되는 

  웹 서비스 응용 프로그램 단위로 내장 객체 application 은 

  서블릿과 서버간의 자료를 교환하는 메소드를 제공한다.



3. application Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>application Test</title>

</head>

<body>


<%! int application = 0; %>

<%! int count = 0; %>


<%

String scount = (String)application.getAttribute("count");


if(scount != null)

{

count = Integer.parseInt(scount);

}

else {

count = 0;

}


application.setAttribute("count", Integer.toString(++count));

application.log("현재재 까지 조회수 : " + count);

%>


서버 컨테이너 정보 : <%=application.getServerInfo() %><br>

현재까지의 조회회 수 : <%=count %>


</body>

</html>



.exception 객체


: 예외 처리 exception 객체는 페이지 지시자에서 is ErrorPage="true"로

  지정한 경우 이용할 수 있는 내장 객체 이다.

  내장 객체 exception 은 지정한 예외처리 페이지에서 적절한 예외 처리를

  구현한다.



4. exception Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>error_1.jsp Test</title>

</head>

<body>


<%@ page isErrorPage="true" %>


<H1> 예외 처리 페이지 </H1>


오류 문자열[exception.toString()] :<%=exception.toString() %><br>

오류 메시지[exception.getMessage()] :<%=exception.getMessage() %>

<br>


</body>

</html>

error_1.jsp로 저장



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>exception Test</title>

</head>

<body>

<%@ page autoFlush="false" buffer="1kb" errorPage="error_1.jsp" %>


<%

for(int i = 1; i <25; i++)

{

out.println("남은 출력 버퍼의 크기(out.getReamining())"+out.getRemaining()+"<br>");

}

%>


</body>

</html>



.pageContext 객체


: JSP 페이지와 관련된 프로그램 코드에서 다른 내장 객체를 얻거나

  하나의 페이지에서 다른 페이지로 요청 처리 제어권을 임시 혹은

  영구적으로 넘겨 주는 데 사용함

  

  

  반환값                   메소드                   용도

  

  

  void                    forward(String)    다른 서블릿 혹은 JSP로 요청을 이동

                                                         현재 페이지의 요청과 응답에 관한 제어권을

                                                         URL 로 지정된 주소로 영구적 넘김

                                                         forward() 된 페이지의 요청처리가 종료되면

                                                         응답도 종료됨  

        

  void                    include(String)     지정된 페이지를 현재의 위치에 삽입

                                                         현재 페이지의 요청과 응답에 관한 제어권을

                                                         URL 로 지정된 주소로 넘김

                                                         include 된 페이지의 요청처리가 끝나면

                                                         다시 원래 페이지로 돌아옴

  

  Exception           getException()     Exception 객체를 반환

  HttpSession        getSession()         HttpSession 객체를 반환

                                                         클라이언트의 세션 정보를 담고 있는

                                                         객체를 리턴함



5. page context Test


<font color="green">


다른 화일을 삽입하는 include(),<br> 제어권을 넘기는 forward() 메소드 제공


</font>

include.html로 저장



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>page context Test</title>

</head>

<body>


<h2> pageContext Test </h2>


<% pageContext.getOut().println("include.html 추가"); %>


<hr size="5" color="red">


<%= "include.html 추가" %>


<hr size="5" color="green">


<% pageContext.forward("include.html"); %>


<hr size="5" color="blue">


</body>

</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>page context Test</title>

</head>

<body>


<h2> pageContext Test </h2>


<% pageContext.getOut().println("include.html 추가"); %>


<hr size="5" color="red">


<%= "include.html 추가" %>


<hr size="5" color="green">


<% pageContext.include("include.html"); %>


<hr size="5" color="blue">


</body>

</html>



. page 객체


: 내장 객체 page 는 JSP 페이지 자체를 나타내는 객체로서

  _jspSrrvice() 에

  

   final java.lang.object page = this;

   

   object page = this; 로 되어 있다

   

내장  객체 page 는 자바에서 자기 자신을 나타내는 this 로 

사용한다.



6. page Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>page Test</title>

</head>

<body>


<%@ page info="내장 객체 page : page 자기 자신의 객체" %>


<%= this.getServletInfo() %><P>

<%= this.getServletInfo() %><P>

<%= this.getServletInfo() %><P>


</body>

</html>



. 액션 태그


: JSP 액션 태그는 XML 스타일의 태그로 기술하며

  특정한 동작 기능을 수행한다. JSP 액션 태그는 아래와 같은 형식으로

  사용한다.

  

  <jsp: 태그키워드     태그속성 ="태그 값" />

  

  <jsp:include     page="sub.jsp" />

  

  

  만약 태그에서 매개 변수 지정과같은 내용이 있다면

  

  <jsp:태그 키워드 태그속성="태그 값">

  

    매개 변수 지정

  </jsp:태그 키워드>

  

  

  <jsp:include page="includesub.jsp">

  

      <jsp:param name="weeks" value="52" />

  

  </jsp:include>

  

  

  : JSP 액션 태그는 클라이언트 혹은 서버에게 어떠한 작동을 행하도록

    명령을 내리는 태그 이다.

    

    크게 JSP 페이지 안에서 자바빈즈를 사용할 수 있도록 하는 액션태그와

    페이지를 활용할 수 있도록 하는 액션태그로 나눌 수 있습니다.

    

    

    액션태그의 종류

    

    

    태그 종류                      태그 형식                                         용도

    

    include              <jsp:include page="test.jsp" />                현재 JSP 페이지에서

                                                                                               다른 페이지를 삽입

    param               <jsp:include page="test.jsp">

                             <jsp:param name="id" value="king" />

                             </jsp>

    

    forward            <jsp:forward page="test.jsp" />                현재 JSP 페이지의

                                                                                       제어를 다른 페이지에 전달

    param               <jsp:forward page="test.jsp">

                             <jsp:param name="id" value="queen" />

                             </jsp>                                                                                    

    

    plugin               <jsp:plugin type="applet" code="test" /> 자바 에플릿등을

                                                                                                     플러그인

    useBean            <jsp:useBean id="login" class="../LoginBean" />

                                                                      자바 빈즈 사용

                                                                      

    setProperty       <jsp:setProperty name="login" property="pass" />

                                                                        자바빈즈의 속성을 지정하는 메소드 호출

    

    getProperty      <jsp:getProperty name="login" property="pass" />

                                                                        자바빈즈의 속성을 반환하는 메소드 호출



7. main.jsp Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>sub.jsp Test</title>

</head>

<body>

<hr size="5" color="red">


<font color="blue">

이부분은 include 태그가 있던 자리로 sub.jsp 화일의 결과가 삽입 됩니다.


</font>


<hr size="5" color="green">


</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>sub2.jsp Test</title>

</head>

<body>

<div style="background-color:yellow ; color:red ;">


이부분은 include 태그가 있던 자리로 sub.jsp 화일의 결과가 삽입 됩니다.

</div>


</body>

</html>

sub2.jsp로 저장



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>main.jsp Test</title>

</head>

<body>


<h2> include 액션태그 </h2>


main.jsp 화일 시작 부분입니다.<br>

include 태그는 페이지 화일 결과를 태그 위치에 삽입합니다.<br>


<jsp:include page="sub.jsp" />


main.jsp 화일 끝 부분입니다.<br>


<jsp:include page="sub2.jsp" />


</body>

</html>



. 지시자 include 와 액션 태그 include 차이


: 지시자 include 는 소스 코드 형태로 삽입 되고

  액션태그 include 는 처리 결과를 삽입한다.

  

  지시자 include 는 소스 코드 형태로 삽입 되므로 중복된 소스가

  있는 경우 주의가 필요하다 특히 변수가 중복되는 경우

  오류가 발생한다.



8. includedire Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>includefile.jsp file test</title>

</head>

<body>

<% int n = 52; %>


<hr size="5" color="green">


1년은 <%= n %> 주 입니다.


<hr size="5" color="cyan">


</body>

</html>

includefile.jsp로 저장



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>includedirective.jsp Test</title>

</head>

<body>

<% int i = 12 ; %>

<%// int n = 365; %>


<% int days = 365; %>


1년은 <%= i %> 달 입니다.

<%@ include file="includefile.jsp" %>

<br>

1년은 <%= days %> 일 입니다.


</body>

</html>



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>includedirection.jsp Test</title>

</head>

<body>


<% int i = 12; %>

<% int n = 365; %>


1년은 <%= i %> 달입니다.


<%--<jsp:include page="includefile.jsp" /> --%>


<%pageContext.include("includefile.jsp"); %>


1년은 <%= n %> 일 입니다.


</body>

</html>



. 태그 forward 와 include 차이


: 태그 include 는 page 속성에 지정된 페이지의 일 처리가 끝나면 다시 현재의

  페이지로 돌아와 처리를 진행해 나가지만 태그 forward 는 page 속성에

  지정된 페이지로 제어가 넘어가면 현재 페이지로 다시 돌아오지 않고

  이동 된 페이지에서 실행을 종료 합니다.



9. forwardmain.jsp Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>forwardsub.jsp file test</title>

</head>

<body>

<hr size="5" color="red">

<font color="green">


이전에 있던 forwardmain.jsp 의 내용은 출력 되지 않습니다.<br>

이부분은 forward 태그가 있던 자리로 forwardsub.jsp 의 결과만

출력 됩니다.<br>

이후 에 있던 forwardmain.jsp 의 내용은 나오지 않습니다.


</font>


<hr size="5" color="green">


</body>

</html>

forwardsub.jsp로 저장



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>forwardmain.jsp Test</title>

</head>

<body>


<h2> forward 액션 태그 사용 </h2>


forwardmain.jsp 화일의 시작 부분입니다.<br>

forward 태그는 페이지 속성 화일로 제어를 넘깁니다.<br>

forwardmain.jsp 페이지의 내용은 하나도 출력 되지 않습니다.<br>


<%--<jsp:forward page="forwardsub.jsp" />--%>


<% response.sendRedirect("forwardsub.jsp"); %>


forwardmain.jsp 화일 끝부분입니다.<br>


</body>

</html>



10. forward_1.html Test


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>forward_1.jsp Test</title>

</head>

<body>


<%--<jsp:forward page="send.jsp" />--%>

<%

 pageContext.forward("send.jsp");

%>


</body>

</html>

forward_1.jsp로 저장



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>send.jsp Test</title>

</head>

<body>


<%

response.sendRedirect("http://" + request.getParameter("url"));

%>


</body>

</html>

send.jsp로 저장



<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>forward_1.html Test</title>

</head>

<body>

<h2> 접속할 사이트를 입력하세요 </h2>


<form action="forward_1.jsp" method="post" name="test">


URL : <input type="text" name="url"><br>

<input type="submit" value="보내기">

<input type="reset" value="취소">


</form>


</body>

</html>


+ Recent posts