πŸ’» Programming/JSP

[JSP] JSP μ‹œμž‘ν•˜κΈ°

μ˜€λŠ˜μ€ JSPλ₯Ό λ§Œλ“€κ³  μ‹€ν–‰ν•΄λ³΄λŠ” μ‹œκ°„μ„ 가져보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€. 

 

ν•˜μ§€λ§Œ κ·Έ 전에 μ•žμ„œμ„œ μ•Œλ €λ“œλ¦΄ 것이 ν•˜λ‚˜ 더 μžˆμŠ΅λ‹ˆλ‹€. λ°”λ‘œ Scriptletμž…λ‹ˆλ‹€.

 

Scriptlet은 λ³€μˆ˜μ„ μ–Έ, λ©”μ†Œλ“œ μ„ μ–Έ, μžλ°”μ—μ„œ μ‚¬μš©λ˜λŠ” 문법듀, λ˜λŠ” μœ νš¨ν•œ 슀크립트 νŽ˜μ΄μ§€ ν‘œν˜„λ“±μ„ 내뢀에 κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

Scriptlet의 문법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

<% code fragment %>

 

그리고 XMLμ—μ„œ μ‚¬μš©ν•  λ•Œμ—λŠ” μ•„λž˜μ™€ 같이 μ“Έ 수 μžˆμŠ΅λ‹ˆλ‹€.  

<jsp:scriptlet> code fragment </jsp:scriptlet>

HTML νƒœκ·Έλ‚˜ JSP μš”μ†Œ λ“± λͺ¨λ“  ν…μŠ€νŠΈλŠ” scriptlet의 내뢀에 μ¨μ„œλŠ” μ•ˆλ©λ‹ˆλ‹€.  

μ•„λž˜λŠ” JSP μ‚¬μš© μ˜ˆμ œμž…λ‹ˆλ‹€.  

<html> <head><title>Hello World</title></head> <body> Hello World!<br/> <% out.println("Your IP address is " + request.getRemoteAddr()); %> </body> </html>

NOTE: Apache Tomcat μ›Ήμ„œλ²„κ°€ C:\apache-tomcat-7.0.2 에 μ„€μΉ˜λ˜μ–΄ μžˆλ‹€λŠ” κ°€μ •ν•˜μ— μœ„ HTML μ½”λ“œλ₯Ό hello.jsp μ•ˆμ— μž‘μ„±ν•˜κ³  νŒŒμΌμ„  C:\apache-tomcat-7.0.2\webapps\ROOT λ””렉토리에 λ„£μŠ΅λ‹ˆλ‹€. 그리고 μ›ΉλΈŒλΌμš°μ €μ—μ„œ http://localhost:8080/hello.jsp μ— μ ‘μ†ν•΄λ³΄μ„Έμš”. λ¬Όλ‘  μ›Ήμ„œλ²„λ₯Ό κΈ°λ™μ‹œν‚€μ…”μ•Όκ² μ£ ?? 그럼 μ•„λž˜ 그림처럼 κ²°κ³Όκ°€ λ‚˜μ˜¬ κ²ƒμž…λ‹ˆλ‹€. 

 

 

 

 



μ™€μš°~~ ν•΄λƒˆμŠ΅λ‹ˆλ‹€~
우리의 첫 JSP둜 λ§Œλ“  μ›ΉνŽ˜μ΄μ§€μ—μš”~ ^__^ 

 

 

JSP μ„ μ–ΈλΆ€:

μ„ μ–ΈλΆ€λΌλŠ” 것은 ν•˜λ‚˜ μ΄μƒμ˜ λ³€μˆ˜λ‚˜ λ©”μ†Œλ“œλ₯Ό μ„ μ–Έν•˜λŠ” 것을 λ§ν•©λ‹ˆλ‹€. JSP파일 λ‚΄λΆ€μ—μ„œ 좔후에 μ‚¬μš©λ  λ³€μˆ˜λ‚˜ λ©”μ†Œλ“œλ₯Ό 미리 μ„ μ–Έν•΄λ‘λŠ” μ„ μ–ΈλΆ€μ£ .  

 

JSP μ„ μ–ΈλΆ€ 문법:

<%! declaration; [ declaration; ]+ ... %>

XMLμ—μ„œ μ‚¬μš©ν•˜μ‹€ λ•ŒλŠ” μ•„λž˜μ²˜λŸΌ μ‚¬μš©ν•˜μ„Έμš”.

<jsp:declaration> code fragment </jsp:declaration>

μ•„λž˜λŠ” JSP μ„ μ–ΈλΆ€μ˜ μ˜ˆμ œμž…λ‹ˆλ‹€.

<%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %>

JSP ν‘œν˜„λΆ€:

JSP ν‘œν˜„λΆ€λŠ” κ²°κ³Όκ°€ ν•˜λ‚˜μ˜ String으둜 λ°˜ν™˜λ©λ‹ˆλ‹€. JSP νŒŒμΌλ‚΄μ—μ„œ HTMLνƒœκ·Έλ‘œ 감싸져 μžˆλŠ”μ§€μ™€λŠ” 관계없이 μ‚¬μš©λ  수 있으며 μžλ°”λ¬Έλ²•μ— λ§žλŠ” λͺ¨λ“  μžλ°”μ½”λ”©μ„ ν•˜μ‹€ 수 μžˆμŠ΅λ‹ˆλ‹€. λ¬Όλ‘  κ²°κ³ΌλŠ” Stringμ΄λΌλŠ” κ²ƒλ§Œ μœ μ˜ν•˜μ‹œλ©΄ 될 것 κ°™λ„€μš”. 

 

JSP ν‘œν˜„μ‹μ˜ 문법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

<%= expression %>

XMLμ—μ„œ μ‚¬μš©ν•˜μ‹€ λ•ŒλŠ” μ•„λž˜μ²˜λŸΌ μ‚¬μš©ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€.

<jsp:expression> expression </jsp:expression>

μ•„λž˜λŠ” JSP ν‘œν˜„λΆ€ μ‚¬μš© μ˜ˆμ œμž…λ‹ˆλ‹€. 

<html> <head><title>A Comment Test</title></head> <body> <p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>

μ›ΉλΈŒλΌμš°μ €μ—μ„œ 보면 μ•„λž˜μ²˜λŸΌ λ‚˜μ˜¬ κ²ƒμž…λ‹ˆλ‹€.

Today's date: 11-Sep-2010 21:24:25

 

JSP 주석:

JSP 주석을 λ‹¬κΈ°μœ„ν•œ 문법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

<%-- This is JSP comment --%>

λ‹€μŒμ€ JSP 주석 μ˜ˆμ œμž…λ‹ˆλ‹€. 

<html> <head><title>A Comment Test</title></head> <body> <h2>A Test of Comments</h2> <%-- This comment will not be visible in the page source --%> </body> </html>

μ›ΉλΈŒλΌμš°μ €μ—μ„œ μ‹€ν–‰ν•œ κ²°κ³ΌλŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

A Test of Comments

 

 

 

 

 

 

μ•„λž˜λŠ” 주석을 λ„£κ±°λ‚˜ νŠΉλ³„ν•œ 경우 문자λ₯Ό 넣을 λ•Œ μ‚¬μš©λ  수 μžˆλŠ” λ¬Έλ²•μž…λ‹ˆλ‹€. μ§€κΈˆ λ‹Ήμž₯은 κ·Έλƒ₯ 이런게 μžˆλ‹€λΌκ³  μƒκ°ν•˜μ‹œλ©΄ 될 것 κ°™λ„€μš”. 

 

Syntax

Purpose

<%-- comment --%>

A JSP comment. Ignored by the JSP engine.

<!-- comment -->

An HTML comment. Ignored by the browser.

<\%

Represents static <% literal.

%\>

Represents static %> literal.

\'

A single quote in an attribute that uses single quotes.

\"

A double quote in an attribute that uses double quotes.

 

 

 

μ΄μƒμœΌλ‘œ JSP의 κ°„λ‹¨ν•œ μ‚¬μš©λ²•μ„ 곡뢀해 λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

 

뭐 별거 μ—†λ„€μš”, κ·Έμ£ ? ^___^

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_quick_guide.htm