메뉴 건너뛰기

조회 수 3637 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

JSP문서안에 JAVA언어를 넣기 위한 방식

스크립틀릿(Scriptlet), 선언(declaration), 표현식(expression)


스크립틀릿(Scriptlet)

JSP페이지에서 JAVA언어를 사용하기 위한 요소 중 가장 많이 사용되는 요소.

대부분의 Java코드를 사용할 수 있다.

<% %>

scriptlet.jsp

  1. <%@ page language="java" contentType="text/html; charset=EUC-KR"
  2.    pageEncoding="EUC-KR"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  5. <title>Insert title here</title>
  6. </head>
  7.     <%
  8.         int i = 0;
  9.         while(true){
  10.             i++;
  11.             out.println("2 * " + i + " = " + (2 * i)  + "<br />");
  12.     %>
  13.         =========<br />
  14.     <%
  15.             if(i >= 9) break;
  16.         }
  17.     %>
  18. </body>
  19. </html>






선언(declaration)

JSP페이지 내에서 사용되는 변수 또는 메소드를 선언할 때 사용한다.

선언된 변수 및 메소드는 전역의 의미로 사용한다.

<%! %>

declaration.jsp

  1. <%@ page language="java" contentType="text/html; charset=EUC-KR"
  2.    pageEncoding="EUC-KR"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  5. <title>Insert title here</title>
  6. </head>
  7.     <%!
  8.         int i = 10;
  9.         String str = "ABCDE";
  10.     %>
  11.    
  12.     <%!
  13.         public int sum(int a, int b) {
  14.             return a+b;
  15.         }
  16.     %>
  17.    
  18.     <%
  19.         out.println("i = " + i + "<br />");
  20.         out.println("str = " + str + "<br />");
  21.         out.println("sum = " + sum(1,5) + "<br />");
  22.     %>
  23. </body>
  24. </html>






표현식(expression)

JSP페이지 내에서 사용되는 변수의 값 또는 메소드 호출 결과값을 출력하기 위해 사용한다.

결과값은 String 타입이며, 세미콜론(semicolon); 사용 할 수 없다.

<%= %>

expression.jsp

  1. <%@ page language="java" contentType="text/html; charset=EUC-KR"
  2.    pageEncoding="EUC-KR"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  5. <title>Insert title here</title>
  6. </head>
  7.     <%!
  8.         int i = 10;
  9.         String str = "abc";
  10.        
  11.         private int sum(int a, int b) {
  12.             return a+b;
  13.         }
  14.     %>
  15.    
  16.     <%= i %><br />
  17.     <%= str %><br />
  18.     <%= sum(15) %>
  19.  
  20. </body>
  21. </html>




  1. 주석(comments)

    Date2017.09.12 Views3114
    Read More
  2. 지시자(Directive)

    Date2017.09.12 Views3123
    Read More
  3. 스크립틀릿(Scriptlet), 선언(declaration), 표현식(expression)

    Date2017.09.12 Views3637
    Read More
  4. JSP 동작 원리, 내부 객체

    Date2017.09.12 Views3579
    Read More
  5. Oracle Database DB연결, table 생성

    Date2017.09.11 Views4262
    Read More
Board Pagination Prev 1 2 3 4 Next
/ 4

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved