๐Ÿ’ป Programming/JSP (20)

JSP์™€ Javascript์˜ ์ฐจ์ด๊ฐ€ ๋ญ˜๊นŒ์š”? ๋ผ๋Š” ์งˆ๋ฌธ์„ ๋ฐ›์€ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ดˆ๋ณด์ž ๋ถ„๋“ค์ด ์ถฉ๋ถ„ํžˆ ํ• ๋งŒํ•œ ์งˆ๋ฌธ์ด์ฃ . JSP๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋„ ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ ์ „์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ๋ญ๊ฐ€ ๋‹ค๋ฅธ์ง€ ์ž˜ ๋ชจ๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ดˆ๋ณด์ž ๋ถ„๋“ค์ด ์ดํ•ดํ•  ์ •๋„๋กœ ๊ฐ„๋žตํ•˜๊ณ  ์‰ฝ๊ฒŒ ์š”์ ๋งŒ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Javascript๋Š” ์›น๋ธŒ๋ผ์šฐ์ €(์‚ฌ์šฉ์ž์˜ ์ปดํ“จํ„ฐ)์—์„œ ์‹คํ–‰โ€‹์ด๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์€ .js ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์— ๋”ฐ๋ผ ์ž‘์„ฑ์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ์ค‘ ํ•˜๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

โ€‹

๋ฐ˜๋ฉด, JSP๋Š” Java Server Page์˜ ์•ฝ์ž๋กœ .jsp ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์–ธ์–ด์˜ ์ข…๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.

.jsp ํŒŒ์ผ์—๋Š” htmlํƒœ๊ทธ์™€ javascript ํ•จ์ˆ˜๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ถ”๊ฐ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด <% java code %> ํ˜•ํƒœ๋กœ ์ž๋ฐ”๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

<html>

<head><title>Hello World</title></head>

<body> Hello World!<br/>

<% out.println("Your IP address is " + request.getRemoteAddr()); %>  <!-- ์—ฌ๊ธฐ๊ฐ€ JSP ๋ฌธ๋ฒ• -->

</body>

</html>

 

JSP์— ์ž‘์„ฑ๋œ ๋‚ด์šฉ์€ ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์›น๋ธŒ๋ผ์šฐ์ €(์‚ฌ์šฉ์ž ์ปดํ“จํ„ฐ)๋กœ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.

 

์ด์ œ JSP์™€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ฐจ์ด์ ์ด ๋ญ”์ง€ ์•„์‹œ๊ฒ ์ฃ ?

 

๊ฐ€์‹œ๊ธฐ ์ „์— ๊ณต๊ฐ ๊พธ~~~~์šฑ ๋ˆŒ๋Ÿฌ์ฃผ๊ณ  ๊ฐ€์„ธ์š” ^-^

๐Ÿ’ป Programming/JSP

[JSP] Security ( ๋ณด์•ˆ )

JSP์™€ servlets ์€ Web ๊ฐœ๋ฐœ์ž๋“ค์„ ์œ„ํ•ด์„œ ๋ณด์•ˆ(์ธ์ฆ)์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.  

์˜ค๋Š˜์€ ๊ทธ ์ค‘ ๋‘๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ๋งŒ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

์—ญํ•  ๊ธฐ๋ฐ˜ ์ธ์ฆ

servlet ์—์„œ ์ œ๊ณตํ•˜๋Š” ์—ญํ•  ๊ธฐ๋ฐ˜ ์ธ์ฆ์€ ์‚ฌ์šฉ์ž ๋ ˆ๋ฒจ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์—ญํ• ์„ ๋ถ€์—ฌํ•˜๊ณ  ํŠน์ • ์—ญํ• ์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ œํ•œํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.  

ํ†ฐ์บฃ ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ์˜ conf๋””๋ ‰ํ† ๋ฆฌ์— tomcat-users.xml ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”.

<?xml version='1.0' encoding='utf-8'?> 
<tomcat-users> 
	<role rolename="tomcat"/> 
	<role rolename="role1"/> 
	<role rolename="manager"/> 
	<role rolename="admin"/> 
	<user username="tomcat" password="tomcat" roles="tomcat"/> 
	<user username="role1" password="tomcat" roles="role1"/> 
	<user username="both" password="tomcat" roles="tomcat,role1"/> 
	<user username="admin" password="secret" roles="admin,manager"/> 
</tomcat-users>

์œ„ ์ฝ”๋“œ๊ฐ€ ๋ญ˜ ํ•˜๊ณ ์žˆ๋Š”์ง€๋Š” ๋Œ€์ถฉ ๋ด๋„ ์•„์‹œ๊ฒ ์ฃ ? ์—ญํ• ์„ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉ์ž๋ฅผ ๋งŒ๋“ค๋•Œ ์—ญํ• ์„ ๋ถ€์—ฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ  web.xmlํŒŒ์ผ์— <security-constraint> ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ๋„ฃ์–ด๋ณด์„ธ์š”. ๋ฌผ๋ก  url-pattern์€ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š” url์ด์–ด์•ผ๊ฒŒ์ฃ ? ๊ทธ๋ž˜์•ผ ํ…Œ์ŠคํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค~ 

 

<web-app> 
    ... 
    <security-constraint> 
    	<web-resource-collection> 
        	<web-resource-name> SecuredBookSite </web-resource-name> 
            <url-pattern>/secured/*</url-pattern> 
            <http-method>GET</http-method> 
            <http-method>POST</http-method> 
        </web-resource-collection> 
        <auth-constraint> 
        	<description> Let only managers use this app </description> 
            <role-name>manager</role-name> 
        </auth-constraint> 
    </security-constraint> 
    <security-role> 
    	<role-name>manager</role-name> 
    </security-role> 
    <login-config> 
    	<auth-method>BASIC</auth-method> 
    </login-config> 
    ... 
</web-app>

์œ„ ์†Œ์Šค์ฝ”๋“œ๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์–ธ์ธ๊ฐ€ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

  • HTTP GET ์ด๋‚˜ POST ๋ฐฉ์‹์˜ ์š”์ฒญ์— ๋Œ€ํ•ด์„œ /secured/* URL์— ๋Œ€ํ•œ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ๋ณด์•ˆ์„ค์ •์˜ ์ œ์•ฝ์„ ๋ฐ›๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

  • ๋งค๋‹ˆ์ € ์—ญํ• ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด์„œ๋งŒ /secured/* ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • ๋งˆ์ง€๋ง‰์œผ๋กœ login-config ํƒœ๊ทธ๊ฐ€ ๊ธฐ๋ณธํ˜•ํƒœ์˜ ์ธ์ฆ์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 

์ด์ œ /security๋‚ด๋ถ€์˜ ํŽ˜์ด์ง€๋กœ ์ ‘๊ทผ์„ ํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž๋ช…๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ผ๋Š” ์ฐฝ์ด ๋œจ๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” ์—ญํ• ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๊ฑฐ๋‚˜ ์ธ์ฆ์— ์‹คํŒจํ•˜๋ฉด ์ ‘์†์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‚˜ ํŽ˜์ด์ง€๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์ฃ . 

 

Form์„ ์ด์šฉํ•œ ์ธ์ฆ

FORM ์ธ์ฆ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž์—๊ฒŒ ๋กœ๊ทธ์ธ ํผ์„ ์ œ๊ณตํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.  

<html>

<body bgcolor="#ffffff">
    <form method="POST" action="j_security_check">
        <table border="0">
            <tr>
                <td>Login</td>
                <td><input type="text" name="j_username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="j_password"></td>
            </tr>
        </table> <input type="submit" value="Login!"> </center>
    </form>
</body>

</html>

์œ„ ์ฝ”๋“œ๋ฅผ login.jsp์— ๋„ฃ์–ด์ฃผ์„ธ์š”. <form> ํƒœ๊ทธ ์•ˆ์—์žˆ๋Š” action์˜ ๊ฐ’์€  j_security_check ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. POST ๋ฐฉ์‹์ด ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ฃ ? ์ž ์ด์ œ web.xml์˜ <login-config> ํƒœ๊ทธ๋ฅผ FORM์„ ์ด์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

<web-app> 
    ... 
    <security-constraint>
        <web-resource-collection>
            <web-resource-name> SecuredBookSite </web-resource-name>
            <url-pattern>/secured/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description> Let only managers use this app </description>
            <role-name>manager</role-name>
        </auth-constraint>
    </security-constraint>
    <security-role>
        <role-name>manager</role-name>
    </security-role>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config> 
    ...
</web-app>

์ด์ œ  /secured/* ์ฃผ์†Œ๋กœ ์ ‘์†์„ ์‹œ๋„ํ•ด ๋ณด์„ธ์š”. ์‚ฌ์šฉ์ž ID์™€ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ผ๊ณ  ํ• ๊ฒ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๊ฐ€  "j_security_check" ์•ก์…˜์„ ๋งŒ๋‚˜๋ฉด์š”์ฒญ์„ ์ธ์ฆํ•˜๊ธฐ์œ„ํ•œ ๋‚ด๋ถ€ ๋ฉ”์นด๋‹ˆ์ฆ˜์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ์ด ์„ฑ๊ณตํ•˜๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋Š” ์—ญํ• ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ์ปจํ…Œ์ด๋„ˆ๋Š” ์‚ฌ์šฉ์ž๋ฅผ ํ™•์ธํ•˜๊ธฐ์œ„ํ•ด์„œ session-id ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๋Š” ์„ธ์…˜์•„์ด๋””๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ์ฟ ํ‚ค์™€ ํ•จ๊ป˜ ์„ธ์…˜์„ ์œ ์ง€ํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„๊ฐ€ ์ด ์ฟ ํ‚ค๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ๋˜๋Œ๋ ค๋ณด๋‚ด๊ณ  ์ดํ›„ ์š”์ฒญ๋•Œ๋งˆ๋‹ค ์ด ์ฟ ํ‚ค๋ฅผ ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๊ฒŒ๋˜๋ฉด ์ปจํ…Œ์ด๋„ˆ๋Š” ์š”์ฒญํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ์ด ์‹คํŒจํ•˜๋ฉด ์„œ๋ฒ„๋Š” ์—๋ŸฌํŽ˜์ด์ง€๋ฅผ ์ „์†กํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.  

 

j_security_check ๊ฐ€ ํ†ฐ์บฃ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ๋” ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ๋ณด์‹œ๋ ค๋ฉด Standard Realm Implementations ์ด๊ณณ์— ๊ฐ€๋ณด์„ธ์š”. 

 

Servlet/JSP์˜ ์ฝ”๋“œ๋กœ ๋ณด์•ˆ์ ์šฉํ•˜๊ธฐ

HttpServletRequest ๊ฐ์ฒด๊ฐ€ ๋ณด์•ˆ๊ด€๋ จํ•ด์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

SN Method and Description
1 String getAuthType()
The getAuthType() method returns a String object that represents the name of the authentication scheme used to protect the Servlet.
2 boolean isUserInRole(java.lang.String role)
The isUserInRole() method returns a boolean value: true if the user is in the given role or false if they are not.
3 String getProtocol()
The getProtocol() method returns a String object representing the protocol that was used to send the request. This value can be checked to determine if a secure protocol was used.
4 boolean isSecure()
The isSecure() method returns a boolean value representing if the request was made using HTTPS. A value of true means it was and the connection is secure. A value of false means the request was not.
5 Principle getUserPrinciple()
The getUserPrinciple() method returns a java.security.Principle object that contains the name of the current authenticated user.

 

์˜ˆ๋ฅผ๋“ค์–ด ๋งค๋‹ˆ์ € ๊ถŒํ•œ์„ ๊ฐ–๊ณ ์žˆ๋Š” ์‚ฌ์šฉ์ž๋“ค์„ ์œ„ํ•œ ๋งํฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” JavaServer Page ๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•ด์ฃผ์‹œ๋ฉด ๋˜๋Š”๊ฑฐ์ฃ  

<% if (request.isUserInRole("manager")) { %> 
    <a href="managers/mgrreport.jsp">Manager Report</a> 
    <a href="managers/personnel.jsp">Personnel Records</a> 
<% } %>

๊ทธ๋Ÿฌ๋ฉด ์‚ฌ์šฉ์ž ์—ญํ• ์„ ๊ฒ€์‚ฌํ•œ ๋’ค ๋งค๋‹ˆ์ €์—ญํ• ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ๋งŒ ๋งํฌ๋ฅผ ๋ณด์—ฌ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋กœ๊ทธ์ธ ํผ์—์„œ ์ž…๋ ฅํ•œ ์‚ฌ์šฉ์ž๋ช…์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” getRemoteUser ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

 

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

 

 

 

 

 

 

์˜ค๋Š˜์€ JSP๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํƒ€๋ฒ ์ด์Šค์— ์ ‘์†ํ•˜๊ณ   SELECT, INSERT, DELETE, ๊ทธ๋ฆฌ๊ณ  UPDATE ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ๋ฐ์ดํƒ€๋ฒ ์ด์Šค๋ฅผ ์„ค์น˜๊ฐ€๋˜์–ด์žˆ๊ณ  emp๊ณ„์ •์ด ๋งŒ๋“ค์–ด์ ธ ์žˆ์œผ๋ฉฐ Employeesํ…Œ์ด๋ธ”์ด id, first, last, age ์ปฌ๋Ÿผ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

SELECT Operation:

JTSL์„ ์ด์šฉํ•˜์—ฌ JSP ํŽ˜์ด์ง€๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>SELECT Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

์œ„ JSPํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒ ์ฃ ? 

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Khan

30

103

Sumit

Mittal

28

 

INSERT Operation:

์—ญ์‹œ๋‚˜ JSTL์„ ์ด์šฉํ•œ JSP ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. 

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>JINSERT Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <sql:update dataSource="${snapshot}" var="result"> INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ํ•œ์ค„์ด ์ถ”๊ฐ€๊ฐ€ ๋˜์–ด ๋‚˜์˜ค๊ฒ ์ฃ .

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Khan

30

103

Sumit

Mittal

28

104

Nuha

Ali

2

 

DELETE Operation:

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>DELETE Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <c:set var="empId" value="103" />
    <sql:update dataSource="${snapshot}" var="count"> DELETE FROM Employees WHERE Id = ?
        <sql:param value="${empId}" />
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Khan

30

104

Nuha

Ali

2

 

UPDATE Operation:

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>DELETE Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <c:set var="empId" value="102" />
    <sql:update dataSource="${snapshot}" var="count"> UPDATE Employees SET last = 'Ali'
        <sql:param value="${empId}" />
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ค๊ฒ ์ฃ ?

 

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Ali

30

104

Nuha

Ali

2

 

 

 

๐Ÿ’ป Programming/JSP

[JSP] JSTL ( JSP Standard Tag Library )

JSTL ๋Š” ์œ ์šฉํ•œ JSP ํƒœ๊ทธ๋“ค์„ ๋ชจ์•„๋†“์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ JSTL tags์™€ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋„ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์•Œ์•„๋‘์‹œ๋ฉด ์ข‹์„๊ฑฐ์—์š”. 

JSTLํƒœ๊ทธ๋Š” ๊ธฐ๋Šฅ๋ณ„๋กœ ๋ถ„๋ฅ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.  

  • Core Tags

  • Formatting tags

  • SQL tags

  • XML tags

  • JSTL Functions


JSTL ์„ค์น˜ํ•˜๊ธฐ

Apache Tomcat container ๋ฅผ ์‚ฌ์šฉ์ค‘์ด๋ฉด ์•„๋ž˜ ๋‘๊ฐ€์ง€ ์Šคํ…์œผ๋กœ ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

  • Apache Standard Taglib ์ด๊ณณ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ’€์–ด์ฃผ์„ธ์š”.

  • ์••์ถ•์ด ํ’€๋ฆฐ ๋””๋ ‰ํ† ๋ฆฌ๋‚ด์˜ lib๋””๋ ‰ํ† ๋ฆฌ์˜ JAR ํŒŒ์ผ๋“ค์„ ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๋ ค๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ webapps\ROOT\WEB-INF\lib ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. ( ์ด๊ฑฐ๋Š” ์ดํด๋ฆฝ์Šค์—์„œ ์ง์ ‘ ํ•˜์‹ค ์ˆ˜ ์žˆ๋Š”๊ฑด ์•„์‹œ์ฃ ? )  

ํƒœ๊ทธ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด <taglib> ๋””๋ ‰ํ‹ฐ๋ธŒ๋ฅผ JSP ํŽ˜์ด์ง€ ์ œ์ผ ์œ„์ชฝ์— ์„ ์–ธํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.  


Core Tags:

JSTL ํƒœ๊ทธ ์ค‘์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํƒœ๊ทธ์ž…๋‹ˆ๋‹ค.  

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

TagDescription
<c:out >Like <%= ... >, but for expressions.
<c:set >Sets the result of an expression evaluation in a 'scope'
<c:remove >Removes a scoped variable (from a particular scope, if specified).
<c:catch>Catches any Throwable that occurs in its body and optionally exposes it.
<c:if>Simple conditional tag which evalutes its body if the supplied condition is true.
<c:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<c:when>Subtag of <choose> that includes its body if its condition evalutes to 'true'.
<c:otherwise >Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'.
<c:import>Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.
<c:forEach >The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .
<c:forTokens>Iterates over tokens, separated by the supplied delimeters.
<c:param>Adds a parameter to a containing 'import' tag's URL.
<c:redirect >Redirects to a new URL.
<c:url>Creates a URL with optional query parameters


Formatting tags:

ํฌ๋งคํŒ… ๊ด€๋ จ ํƒœ๊ทธ๋“ค์ž…๋‹ˆ๋‹ค.๋‹ค๊ตญ์  Web ์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ค๋•Œ ์œ ์šฉํ•˜๋‹ค๊ณ  ํ•˜๋„ค์š”.

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

 

TagDescription
<fmt:formatNumber>To render numerical value with specific precision or format.
<fmt:parseNumber>Parses the string representation of a number, currency, or percentage.
<fmt:formatDate>Formats a date and/or time using the supplied styles and pattern
<fmt:parseDate>Parses the string representation of a date and/or time
<fmt:bundle>Loads a resource bundle to be used by its tag body.
<fmt:setLocale>Stores the given locale in the locale configuration variable.
<fmt:setBundle>Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
<fmt:timeZone>Specifies the time zone for any time formatting or parsing actions nested in its body.
<fmt:setTimeZone>Stores the given time zone in the time zone configuration variable
<fmt:message>To display an internationalized message.
<fmt:requestEncoding>Sets the request character encoding


SQL tags:

JSTL SQL ํƒœ๊ทธ๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํƒ€๋ฒ ์ด์Šค ์‹œ์Šคํ…œ์„ ํ•ธ๋“ค๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( Oracle, MySQL, Microsoft SQL Server )

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

 

TagDescription
<sql:setDataSource>Creates a simple DataSource suitable only for prototyping
<sql:query>Executes the SQL query defined in its body or through the sql attribute.
<sql:update>Executes the SQL update defined in its body or through the sql attribute.
<sql:param>Sets a parameter in an SQL statement to the specified value.
<sql:dateParam>Sets a parameter in an SQL statement to the specified java.util.Date value.
<sql:transaction >Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.


XML tags:

JSTL XML ํƒœ๊ทธ๋Š” JSP์—์„œ XML ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑ ๋ฐ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ XML์„ ํŒŒ์‹ฑํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํƒ€๋ฅผ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ XPath ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ถ”๊ฐ€์ ์ธ ์ปค์Šคํ…€ ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.  

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

์•„๋ž˜์˜ ๋‘ jarํŒŒ์ผ๋„ <Tomcat Installation Directory>\lib ์— ์ถ”๊ฐ€ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

 

TagDescription
<x:out>Like <%= ... >, but for XPath expressions.
<x:parse>Use to parse XML data specified either via an attribute or in the tag body.
<x:set >Sets a variable to the value of an XPath expression.
<x:if >Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.
<x:forEach>To loop over nodes in an XML document.
<x:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<x:when >Subtag of <choose> that includes its body if its expression evalutes to 'true'
<x:otherwise >Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
<x:transform >Applies an XSL transformation on a XML document
<x:param >Use along with the transform tag to set a parameter in the XSLT stylesheet


JSTL Functions:

JSTL ์€ ํ‘œ์ค€ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ์ŠคํŠธ๋ง๊ด€๋ จ ํ•จ์ˆ˜๋“ค์ด์ฃ .

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 

FunctionDescription
fn:contains()Tests if an input string contains the specified substring.
fn:containsIgnoreCase()Tests if an input string contains the specified substring in a case insensitive way.
fn:endsWith()Tests if an input string ends with the specified suffix.
fn:escapeXml()Escapes characters that could be interpreted as XML markup.
fn:indexOf()Returns the index withing a string of the first occurrence of a specified substring.
fn:join()Joins all elements of an array into a string.
fn:length()Returns the number of items in a collection, or the number of characters in a string.
fn:replace()Returns a string resulting from replacing in an input string all occurrences with a given string.
fn:split()Splits a string into an array of substrings.
fn:startsWith()Tests if an input string starts with the specified prefix.
fn:substring()Returns a subset of a string.
fn:substringAfter()Returns a subset of a string following a specific substring.
fn:substringBefore()Returns a subset of a string before a specific substring.
fn:toLowerCase()Converts all of the characters of a string to lower case.
fn:toUpperCase()Converts all of the characters of a string to upper case.
fn:trim()Removes white spaces from both ends of a string.

 

 

 

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

 

ํŽ˜์ด์ง€ ๋ฆฌ๋””๋ ‰์…˜์„ ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ response ๊ฐ์ฒด์˜ sendRedirect() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค. 

public void response.sendRedirect(String location) throws IOException

์ด ๋ฉ”์†Œ๋“œ๋Š” ์ƒํƒœ์ฝ”๋“œ์™€ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ์ฃผ์†Œ๋ฅผ ํ•จ๊ป˜ response๊ฐ์ฒด์— ๋‹ด์•„์„œ ๋ธŒ๋ผ์šฐ์ €๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.  setStatus() ์™€ setHeader() ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ธํŒ…์„ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ ๋ง์ด์ฃ . 

.... String site = "http://www.newpage.com" ; response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); ....

์˜ˆ์ œ

JSP ๋กœ ์–ด๋–ป๊ฒŒ ํŽ˜์ด์ง€ ๋ฆฌ๋””๋ ‰์…˜์„ ํ•˜๋Š”์ง€ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณผ๊นŒ์š”? 

<%@ page import="java.io.*,java.util.*" %> <html> <head> <title>Page Redirection</title> </head> <body> <center> <h1>Page Redirection</h1> </center> <% // New location to be redirected String site = new String("http://www.naver.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %> </body> </html>

์œ„ ์ฝ”๋“œ๋ฅผ PageRedirect.jsp ์— ๋„ฃ๊ณ  http://localhost:8080/PageRedirect.jsp ๋ฅผ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•ด๋ณด์„ธ์š”.   

 

http://www.naver.com ์‚ฌ์ดํŠธ๋กœ ๋ฆฌ๋””๋ ‰์…˜์ด ๋˜๋‚˜์š”?? ^____^ 

 

 

 

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

 

 

ํŒŒ์ผ ์—…๋กœ๋“œ ํผ ๋งŒ๋“ค๊ธฐ

 

๋‹ค์Œ์— ๋ณผ HTML ์ฝ”๋“œ๊ฐ€ ๋ฐ”๋กœ ํŒŒ์ผ ์—…๋กœ๋“œ ํผ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์„ ๋ช‡๊ฐ€์ง€ ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

  • <form>์˜ method ์†์„ฑ์€ POST ์ž…๋‹ˆ๋‹ค. 

  • <form>์˜ enctype ์†์„ฑ์€ multipart/form-data ์ž…๋‹ˆ๋‹ค. 

  • <form>์˜ action ์†์„ฑ์€ ๋ฐฑ์—”๋“œ ์ชฝ์—์„œ ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ํ•ธ๋“ค๋งํ•  JSP ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋Š” UploadFile.jsp๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ํ•œ๊ฐœ์˜ ํŒŒ์ผ๋งŒ ์—…๋กœ๋“œ ํ•  ๋•Œ๋Š”  <input .../> ํƒœ๊ทธ๋ฅผ type="file" ์†์„ฑ๊ณ  ํ•จ๊ป˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฉ€ํ‹ฐํ”Œ ํŒŒ์ผ์—…๋กœ๋”ฉ์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ํ•˜๋‚˜ ์ด์ƒ์˜ inputํƒœ๊ทธ๋ฅผ ๊ฐ๊ธฐ ๋‹ค๋ฅธ name์œผ๋กœ ์„ค์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.  

<html> <head> <title>File Uploading Form</title> </head> <body> <h3>File Upload:</h3> Select a file to upload: <br /> <form action="UploadServlet" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="50" /> <br /> <input type="submit" value="Upload File" /> </form> </body> </html>

์œ„ ์†Œ์Šค๋ฅผ UploadFile.htm์— ๋„ฃ์–ด์ฃผ์„ธ์š”. ์œ„ ํŽ˜์ด์ง€๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ฌ๊ฑฐ์—์š”. ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•ด๋ณด์„ธ์š” ^___^ ์ฐธ๊ณ ๋กœ ์œ„ ์ฝ”๋“œ๋Š” ๊ทธ๋ƒฅ dummy์ฝ”๋“œ๋ผ์„œ ์‹ค์ œ๋กœ ํŒŒ์ผ์„ ์–ด๋”˜๊ฐ€๋กœ ์—…๋กœ๋“œ ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. 

 
 

๋ฐฑ์—”๋“œ JSP ์Šคํฌ๋ฆฝํŠธ ๋งŒ๋“ค๊ธฐ

 

์šฐ์„  ํŒŒ์ผ๋“ค์ด ์—…๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ ์–ด๋””์— ์ €์žฅ๋ ์ง€ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด๋ณผ๊ฒŒ์š”. ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์†Œ์Šค์— ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ณ  web.xmlํŒŒ์ผ์— context-param ํƒœ๊ทธ๋‚ด์— ์ง€์ •ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

<web-app> .... <context-param> <description>Location to store uploaded file</description> <param-name>file-upload</param-name> <param-value> c:\apache-tomcat-5.5.29\webapps\data\ </param-value> </context-param> .... </web-app>

์•„๋ž˜๋Š” ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” UploadFile.jsp์˜ ์†Œ์Šค์ฝ”๋“œ์—์š”. ์†Œ์Šค๋ถ€ํ„ฐ ๋ณด๊ธฐ์ „์— ์ ๊ฒ€ํ•ด์•ผํ•  ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

  • ์•„๋ž˜ ์˜ˆ์ œ๋Š” FileUpload ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์‹ ๋ฒ„์ „์˜ commons-fileupload.x.x.jar ํŒŒ์ผ์„ ํด๋ž˜์ŠคํŒจ์Šค์— ๋„ฃ์–ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ http://commons.apache.org/fileupload/ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์‹ค ์ˆ˜ ์žˆ์–ด์š”. 

  • ๋˜ํ•œ Commons IO ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.  commons-io-x.x.jar ํŒŒ์ผ ์—ญ์‹œ ํด๋ž˜์Šค ํŒจ์Šค์— ์„ค์ •์„ ํ•ด์ฃผ์…”์•ผ ๋˜์š”. ๋‹ค์šด๋กœ๋“œ๋Š” http://commons.apache.org/io/.

  • ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ…Œ์ŠคํŠธํ•  ๋•Œ์—๋Š” maxFileSize ์— ์„ค์ •๋œ ํŒŒ์ผํฌ๊ธฐ๋ณด๋‹ค ์ž‘์€ ํŒŒ์ผ๋“ค๋งŒ ์—…๋กœ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค.

  • c:\temp ์™€ c:\apache-tomcat-5.5.29\webapps\data ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹œ๊ณ  ์—†์œผ๋ฉด ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”. 
     

<%@ page import="java.io.*,java.util.*, javax.servlet.*" %> <%@ page import="javax.servlet.http.*" %> <%@ page import="org.apache.commons.fileupload.*" %> <%@ page import="org.apache.commons.fileupload.disk.*" %> <%@ page import="org.apache.commons.fileupload.servlet.*" %> <%@ page import="org.apache.commons.io.output.*" %> <% File file ; int maxFileSize = 5000 * 1024; int maxMemSize = 5000 * 1024; ServletContext context = pageContext.getServletContext(); String filePath = context.getInitParameter("file-upload"); // Verify the content type String contentType = request.getContentType(); if ((contentType.indexOf("multipart/form-data") >= 0)) { DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File("c:\\temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax( maxFileSize ); try{ // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>JSP File upload</title>"); out.println("</head>"); out.println("<body>"); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); String fileName = fi.getName(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if( fileName.lastIndexOf("\\") >= 0 ){ file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ; }else{ file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + filePath + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); }catch(Exception ex) { System.out.println(ex); } }else{ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); } %>

์ด์ œ http://localhost:8080/UploadFile.htm ์— ์ ‘์†ํ•ด์„œ ํ…Œ์ŠคํŠธ ํ•ด๋ณผ๊นŒ์š”? 

 

์ œ๋Œ€๋กœ ์ž‘๋™ํ•œ๋‹ค๋ฉด  c:\apache-tomcat-5.5.29\webapps\data\ ๋””๋ ‰ํ† ๋ฆฌ์— ํŒŒ์ผ์ด ์—…๋กœ๋“œ ๋˜์–ด์žˆ์„ ๊ฑฐ์—์š”~ 

 

 

 

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

 

 

๐Ÿ’ป Programming/JSP

[JSP] Session ( ์„ธ์…˜ )

์ด๋ฒˆ์—๋Š” JSP๋กœ ์„ธ์…˜์„ ๋‹ค๋ฃจ๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. HTTP๋Š” "stateless" ํ”„๋กœํ† ์ฝœ์ž…๋‹ˆ๋‹ค.  ๋ฌด์Šจ ๋ง์ด๋ƒํ•˜๋ฉด ๋ง์ด์ฃ , ํด์•„์ด์–ธํŠธ(์›น๋ธŒ๋ผ์šฐ์ €)๊ฐ€ ์›นํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ๋•Œ๋งˆ๋‹ค ํด๋ผ์ด์–ธํŠธ๋Š” ์›น์„œ๋ฒ„๋กœ ๋ณ„๋„์˜ ์ปค๋„ฅ์…˜์„ ๋งบ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ „ ํด๋ผ์ด์–ธํŠธ์˜ ์ ‘์†์— ๋Œ€ํ•œ ์–ด๋– ํ•œ ์ •๋ณด๋„ ์ž๋™์œผ๋กœ ๊ธฐ๋กํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. 

์–ด์จŒ๋“  ์„ธ์…˜์ด๋ผ๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ ์›น๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์›น์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ์ด ๋ ๋•Œ ์ƒ๊ธฐ๋Š” ๊ฒƒ์ธ๋ฐ, ์ด ์„ธ์…˜์„ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ต๋‹ˆ๋‹ค.  

1. Cookies

์ฟ ํ‚ค์— ์„ธ์…˜ID๋ฅผ ์ €์žฅํ•˜๋Š” ์‹์œผ๋กœ ์„ธ์…˜์„ ๊ด€๋ฆฌํ•  ์ˆ˜๋Š” ์žˆ๊ฒ ์ง€๋งŒ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฟ ํ‚ค์‚ฌ์šฉ ์ œํ•œ ์„ค์ •์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ์„ธ์…˜์„ ์ฟ ํ‚ค๋ฅผ ์ด์šฉํ•ด์„œ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ข‹์€๋ฐฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค.

 

 

2. Hidden Form Fields

 

์›น์„œ๋ฒ„๋Š” hidden HTML form field์— unique session ID๋ฅผ ๋„ฃ์–ด์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<input type="hidden" name="sessionid" value="12345">

form์ด ์ „์†ก๋  ๋•Œ ์„ธ์…˜ID์™€ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š”๊ฑฐ์ฃ . ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ด ์ •๋ณด๋ฅผ ์›น์„œ๋ฒ„์— ๋ณด๋‚ด๋ฉด ์ด ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์›น์„œ๋ฒ„์—์„œ๋Š” ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ ‘์†ํ•œ ๊ฒƒ์ธ์ง€๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์—ญ์‹œ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ์ฃ . <a> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ๋งํฌ๋ฅผ ํƒ€๊ณ  ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ์—๋Š” ์ € form๋ฐ์ดํƒ€๋ฅผ ์ „์†กํ•˜์ง€ ์•Š๊ฑฐ๋“ ์š”.

3. URL Rewriting

URL ๋์— ์„ธ์…˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„œ๋ฒ„๋Š” ๊ทธ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์„ธ์…˜์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ .

์˜ˆ๋ฅผ๋“ค๋ฉด, http://tutorialspoint.com/file.htm;sessionid=12345 ์ด๋ ‡๊ฒŒ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์„œ๋ฒ„์—์„œ sessionid=12345๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์›น์„œ๋ฒ„์˜ ์„ธ์…˜์ •๋ณด์™€ ๋น„๊ต๋ฅผ ํ•˜๋Š”๊ฑฐ์ฃ .

URL rewriting ์€ ์„ธ์…˜์„ ๊ด€๋ฆฌํ•˜๊ธฐ์œ„ํ•œ ๋ฐฉ๋ฒ•๋“ค ์ค‘์—์„œ๋Š” ๊ทธ๋‚˜๋งˆ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๊ธด ํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ฟ ํ‚ค์‚ฌ์šฉ์„ ์•ˆํ• ๋•Œ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ฐฉ๋ฒ•์ด๋‹ˆ๊นŒ์š”. ํ•˜์ง€๋งŒ ์—ญ์‹œ ๊ฒฐ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ์…˜ID๋ฅผ ๋™์ ์œผ๋กœ ๋งค๋ฒˆ ์ƒ์„ฑํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ •์ ์ธ HTMLํŽ˜์ด์ง€๋ผ๋„ ๋ง์ด์ฃ .  


The session Object:

์œ„ ์„ธ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ด์™ธ์—๋„ JSP๋Š” HttpSession ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ JSPs๋Š” ์„ธ์…˜์„ ์ถ”์ ํ•˜๊ฒŒ ๋˜์–ด์žˆ์œผ๋ฉฐ ์ƒˆ๋กœ์šด HttpSession ๊ฐ์ฒด๊ฐ€ ์ƒˆ๋กœ์šด ํด๋ผ์ด์–ธํŠธ๋“ค์ด ์ ‘์†ํ•  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์„ธ์…˜ ์ถ”์  ๊ธฐ๋Šฅ์„ ๋„๋ ค๋ฉด page directive์˜ ์„ธ์…˜ ์†์„ฑ์„ false๋กœ ์„ธํŒ…ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฒ˜๋Ÿผ ๋ง์ด์ฃ .

<%@ page session="false" %>

JSP engine์€ JSP๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ HttpSession ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ session์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. session ๊ฐ์ฒด๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž๋“ค์€ ๊ถ‚์ด ์„ธ์…˜๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ getSession() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์–ป์–ด์˜ฌ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ทธ๋ƒฅ ์„ธ์…˜์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ ์“ฐ๋ฉด ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค. 

 

๋‹ค์Œ์€ ์„ธ์…˜ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ๋ฉ”์†Œ๋“œ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋“ค์ด ์žˆ๋Š”์ง€ ํ•œ๋ฒˆ ์‚ดํŽด๋ณผ๊นŒ์š”~

 

S.N.Method & Description
1public Object getAttribute(String name)
This method returns the object bound with the specified name in this session, or null if no object is bound under the name.
2public Enumeration getAttributeNames()
This method returns an Enumeration of String objects containing the names of all the objects bound to this session.
3public long getCreationTime()
This method returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
4public String getId()
This method returns a string containing the unique identifier assigned to this session.
5public long getLastAccessedTime()
This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.
6public int getMaxInactiveInterval()
This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.
7public void invalidate()
This method invalidates this session and unbinds any objects bound to it.
8public boolean isNew(
This method returns true if the client does not yet know about the session or if the client chooses not to join the session.
9public void removeAttribute(String name)
This method removes the object bound with the specified name from this session.
10public void setAttribute(String name, Object value) 
This method binds an object to this session, using the name specified.
11public void setMaxInactiveInterval(int interval)
This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.


Session Tracking Example:

๋‹ค์Œ ์˜ˆ์ œ๋Š” HttpSession ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ธ์…˜์˜ ์ƒ์„ฑ์‹œ๊ฐ„๊ณผ ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.  

<%@ page import="java.io.*,java.util.*" %> <% // Get session creation time. Date createTime = new Date(session.getCreationTime()); // Get last access time of this web page. Date lastAccessTime = new Date(session.getLastAccessedTime()); String title = "Welcome Back to my website"; Integer visitCount = new Integer(0); String visitCountKey = new String("visitCount"); String userIDKey = new String("userID"); String userID = new String("ABCD"); // ์ฒ˜์Œ ์ ‘์†ํ•œ ๋ฐฉ๋ฌธ์ž๋ผ๋ฉด ์„ธ์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. if (session.isNew()){ title = "Welcome to my website"; session.setAttribute(userIDKey, userID); session.setAttribute(visitCountKey, visitCount); } visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey, visitCount); %> <html> <head> <title>Session Tracking</title> </head> <body> <center> <h1>Session Tracking</h1> </center> <table border="1" align="center"> <tr bgcolor="#949494"> <th>Session info</th> <th>Value</th> </tr> <tr> <td>id</td> <td><% out.print( session.getId()); %></td> </tr> <tr> <td>Creation Time</td> <td><% out.print(createTime); %></td> </tr> <tr> <td>Time of Last Access</td> <td><% out.print(lastAccessTime); %></td> </tr> <tr> <td>User ID</td> <td><% out.print(userID); %></td> </tr> <tr> <td>Number of visits</td> <td><% out.print(visitCount); %></td> </tr> </table> </body> </html>

 main.jspโ€‹์— ์œ„ ์ฝ”๋“œ๋ฅผ ๋„ฃ๊ณ  http://localhost:8080/main.jsp ๋ฅผ ํ˜ธ์ถœํ•ด๋ณด์„ธ์š”. ์„œ๋ฒ„ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ฒƒ ์žŠ์–ด๋จน์ง€ ๋งˆ์‹œ๊ตฌ์š”~ ^___^ 

Welcome to my website

Session Infomation

Session infovalue
id0AE3EC93FF44E3C525B4351B77ABB2D5
Creation TimeTue Jun 08 17:26:40 GMT+04:00 2010
Time of Last AccessTue Jun 08 17:26:40 GMT+04:00 2010
User IDABCD
Number of visits0

 

 

์œ„์™€๊ฐ™์€ ํŽ˜์ด์ง€๊ฐ€ ๋‚˜์˜ค๋‚˜์š”??

 

๋‹ค์‹œํ•œ๋ฒˆ ํ˜ธ์ถœ ํ•ด ๋ณผ๊นŒ์š”??

 

Welcome Back to my website

Session Infomation

info typevalue
id0AE3EC93FF44E3C525B4351B77ABB2D5
Creation TimeTue Jun 08 17:26:40 GMT+04:00 2010
Time of Last AccessTue Jun 08 17:26:40 GMT+04:00 2010
User IDABCD
Number of visits1


์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋‚˜์š”? Welcome ๋ฉ”์‹œ์ง€๊ฐ€ Welcome Back ๋ฉ”์‹œ์ง€๋กœ ๋ฐ”๋€Œ์—ˆ๋„ค์š”. ^__^ 

 

 

์ด์ œ ์„ธ์…˜ ๋ฐ์ดํƒ€๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณผ๊นŒ์š”??โ€‹โ€‹

 

Deleting Session Data:

์‚ฌ์šฉ์ž์˜ ์„ธ์…˜ ๋ฐ์ดํƒ€๋กœ ํ•ด์•ผ๋˜๋Š” ์ž‘์—…์„ ๋ชจ๋‘ ์™„๋ฃŒํ•˜์˜€๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ž‘์—…์„ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ํŠน์ • ์†์„ฑ ์‚ญ์ œ : removeAttribute(String name) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์„ธ์…˜์˜ ํŠน์ • ์†์„ฑ๊ฐ’๋งŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

  • ์„ธ์…˜ ์‚ญ์ œ : invalidate() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ธ์…˜ ์ „์ฒด๋ฅผ ๋ฌดํšจํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ์…˜์„ ๋Š๋Š”๋‹ค๊ณ  ํ‘œํ˜„ํ•˜์ฃ . 

  • ์„ธ์…˜ ํƒ€์ž„์•„์›ƒ ์„ค์ • : setMaxInactiveInterval(int interval) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ธ์…˜๋ณ„๋กœ ํƒ€์ž„์•„์›ƒ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์‚ฌ์šฉ์ž ๋กœ๊ทธ์•„์›ƒ : servlets 2.4๋ฅผ ์ง€์›ํ•˜๋Š” ์„œ๋ฒ„๋ผ๋ฉด logout ์„ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉ์ž๋ฅผ ๋กœ๊ทธ์•„์›ƒ์‹œํ‚ค๊ณ  ๋ชจ๋“  ์„ธ์…˜์„ ๋ฌดํšจํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  

  • web.xml ์„ค์ • : Tomcat์„ ์‚ฌ์šฉ์ค‘์ด๋ผ๋ฉด web.xml ํŒŒ์ผ์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

<session-config> <session-timeout>15</session-timeout> </session-config>

์—ฌ๊ธฐ์„œ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์˜ ๋‹จ์œ„๋Š” ๋ถ„ ๋‹จ์œ„์ด๋ฉฐ ํ†ฐ์บฃ์˜ ๊ธฐ๋ณธ ํƒ€์ž„์•„์›ƒ์ธ 30 ๋ถ„์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

์„œ๋ธ”๋ฆฟ์˜ getMaxInactiveInterval( )๋ฉ”์†Œ๋“œ๋Š” ์ดˆ๋‹จ์œ„๋กœ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ web.xml ํŒŒ์ผ์— 15 ๋ถ„์œผ๋กœ ์„ค์ •๋˜์–ด์žˆ๋‹ค๋ฉด  getMaxInactiveInterval( ) ๋ฉ”์†Œ๋“œ๋Š” 900 ( 15๋ถ„ * 60์ดˆ )๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค .

 

 

 

 

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

 

Cookies ๋Š” ๊ทธ๋ƒฅ ์ผ๋ฐ˜ text files ์ด์ฃ . ์•„๋งˆ JSP๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด ์ด์ •๋„๋Š” ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ฃ ? 

JSP๋กœ HTTP cookies ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•œ๋ฒˆ ์•Œ์•„๋ณผํ…๋ฐ ๊ทธ ์ „์— ํ•˜๋‚˜๋งŒ ์งš๊ณ  ๋„˜์–ด๊ฐˆ๊นŒ์š”?

์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ์‚ฌ์ดํŠธ์— ์žฌ์ ‘์† ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์›น์‚ฌ์ดํŠธ์—์„œ ์–ด๋–ป๊ฒŒ ์•Œ๊นŒ์š”?

3์ดˆ๋งŒ์— ๋‹ต์ด ์•ˆ๋‚˜์˜จ๋‹ค๋ฉด ์•„๋ž˜๋ฅผ ์ฝ์–ด๋ด…์‹œ๋‹ค.

 

์žฌ์ ‘์†ํ•˜๋Š” ์‚ฌ์šฉ์ž๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ณผ์ •์€ ์„ธ ๋‹จ๊ณ„๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„์ชฝ์—์„œ ์ฟ ํ‚ค๋ฅผ ๋ธŒ๋ผ์šฐ์ €๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.  

  • Browser๋Š” ์„œ๋ฒ„์—์„œ ๋ณด๋‚ธ ์ฟ ํ‚ค๋ฅผ ๋กœ์ปฌ์— ์ €์žฅ์„ ํ•˜๊ฒ ์ฃ .

  • ์ด์ œ ์‚ฌ์šฉ์ž๊ฐ€ ์ด ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ด์šฉํ•ด์„œ ํŠน์ •์‚ฌ์ดํŠธ์— ์ ‘์†ํ•  ๋•Œ ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ฟ ํ‚ค์˜ ์ •๋ณด๊ฐ€ ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „๋‹ฌ๋˜๊ณ  ์„œ๋ฒ„์ชฝ์—์„œ๋Š” ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์ฃ .  

์ฟ ํ‚ค๋Š” ๋‹ค๋ฅธ ์ •๋ณด๋กœ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฟ ํ‚ค๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฒ•์„ ํ•œ๋ฒˆ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฟ ํ‚ค ์„ธํŒ…, ๋ฆฌ์…‹, ์ฝ๊ธฐ, ์‚ญ์ œํ•˜๊ธฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  


Cookie ํ•ด๋ถ€ํ•˜๊ธฐ

 ์ฟ ํ‚ค๋Š” ๋ณดํ†ต HTTP header ์•ˆ์— ์„ธํŒ…๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ( JavaScript ๋Š” ์ง์ ‘ ๋ธŒ๋ผ์šฐ์ €์— ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋„ค์š” ). ์ฟ ํ‚ค๋ฅผ ์„ธํŒ…ํ•˜๋Š” JSP ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ—ค๋”๋ฅผ ๋ณด๋‚ผ๊ฑฐ์—์š”. 

HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; path=/; domain=tutorialspoint.com Connection: close Content-Type: text/html

์ด๋ฏธ request ๊ฐ์ฒด์‹œ๊ฐ„์— ํ•œ๋ฒˆ ๋ณธ์ ์ด ์žˆ๋˜ ๋‚ด์šฉ์ด์ฃ ? ๊ทธ๋Ÿฐ๋ฐ ์ถ”๊ฐ€๋œ๊ฒŒ ํ•˜๋‚˜ ์žˆ๋„ค์š”. Set-Cookie header ์ž…๋‹ˆ๋‹ค. ๋ณด์•„ํ•˜๋‹ˆ ์ด๋ฆ„, ๋งŒ๋ฃŒ์ผ์‹œ, ๊ฒฝ๋กœ, ๊ทธ๋ฆฌ๊ณ  ๋„๋ฉ”์ธ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋„ค์š”.  

๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฟ ํ‚ค๋ฅผ ์ €์žฅํ•˜๋„๋ก ์„ค์ •์ด ๋˜์–ด์žˆ์œผ๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋Ÿฐ ์ •๋ณด๋“ค์„ ๋งŒ๋ฃŒ์ผ ์ „๊นŒ์ง€ ์ €์žฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.๋งŒ์•ฝ ์‚ฌ์šฉ์ž๊ฐ€ ์ฟ ํ‚ค์— ์„ค์ •๋œ ๊ฒฝ๋กœ๋กœ ์ ‘์†์„ ํ•˜๊ฒŒ๋˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ ๊ทธ ์ฟ ํ‚ค๋ฅผ ์žฌ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ธŒ๋ผ์šฐ์ €์˜ ํ—ค๋”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. 

GET / HTTP/1.0 Connection: Keep-Alive User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc) Host: zink.demon.co.uk:1126 Accept: image/gif, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz

์ด์ œ JSP script ๋Š” request method์ค‘ ํ•˜๋‚˜์ธ request.getCookies() ๋ฅผ ํ†ตํ•ด์„œ ์ฟ ํ‚ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

Servlet Cookies ๋ฉ”์†Œ๋“œ

์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋“ค์€ JSP์—์„œ ์ฟ ํ‚ค๋ฅผ ๋‹ค๋ฃฐ๋•Œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ชฉ๋ก ๋ฐ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

 

S.N.Method & Description
1public void setDomain(String pattern)
This method sets the domain to which cookie applies, for example tutorialspoint.com.
2public String getDomain()
This method gets the domain to which cookie applies, for example tutorialspoint.com.
3public void setMaxAge(int expiry)
This method sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will last only for the current session.
4public int getMaxAge()
This method returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown.
5public String getName()
This method returns the name of the cookie. The name cannot be changed after creation.
6public void setValue(String newValue)
This method sets the value associated with the cookie.
7public String getValue()
This method gets the value associated with the cookie.
8public void setPath(String uri)
This method sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories.
9public String getPath()
This method gets the path to which this cookie applies.
10public void setSecure(boolean flag)
This method sets the boolean value indicating whether the cookie should only be sent over encrypted (i.e. SSL) connections.
11public void setComment(String purpose)
This method specifies a comment that describes a cookie's purpose. The comment is useful if the browser presents the cookie to the user.
12public String getComment()
This method returns the comment describing the purpose of this cookie, or null if the cookie has no comment.


 JSP๋กœ ์ฟ ํ‚ค ์„ค์ •ํ•˜๊ธฐ

JSP๋กœ ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.

 

(1) Cookie ๊ฐ์ฒด ์ƒ์„ฑ : You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.

Cookie cookie = new Cookie("key","value");

์ž๋ฐ”์—์„œ ์ฟ ํ‚ค ์ƒ์„ฑํ•˜๋Š”๊ฑฐ๋ž‘ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ name๊ณผ value ( "key"๊ฐ€ name์ธ๊ฑฐ๊ณ  "value"๊ฐ€ value๊ฐ€ ๋˜๊ฒ ์ฃ )์—๋Š” ์ ˆ๋Œ€๋กœ ์ŠคํŽ˜์ด์Šค(๋นˆ์นธ)์ด๋‚˜ ์•„๋ž˜์— ๋‚˜์—ด๋œ ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.  

[ ] ( ) = , " / ? @ : ;

(2) Max age ์„ค์ • : ์ดˆ ๋‹จ์œ„๋กœ max age๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฟ ํ‚ค๊ฐ€ ์‚ด์•„์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์„ ์„ค์ •ํ•˜๋Š”๊ฑฐ์ฃ . ์•„๋ž˜๋Š” 24 ์‹œ๊ฐ„๋™์•ˆ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

cookie.setMaxAge(60*60*24);

(3) Cookie๋ฅผ HTTP response header์— ์ถ”๊ฐ€ํ•˜๊ธฐ : response.addCookie ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ถ”๊ฐ€ํ•˜ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  

response.addCookie(cookie);

์˜ˆ์ œ

<% // Create cookies for first and last names. Cookie firstName = new Cookie("first_name", request.getParameter("first_name")); Cookie lastName = new Cookie("last_name", request.getParameter("last_name")); // Set expiry date after 24 Hrs for both the cookies. firstName.setMaxAge(60*60*24); lastName.setMaxAge(60*60*24); // Add both the cookies in the response header. response.addCookie( firstName ); response.addCookie( lastName ); %> <html> <head> <title>Setting Cookies</title> </head> <body> <center> <h1>Setting Cookies</h1> </center> <ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html>

 main.jsp ํŒŒ์ผ์— ์œ„ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ์ฝ”๋“œ๋Š” hello.jspํŒŒ์ผ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. 

<html> <body> <form action="main.jsp" method="GET"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> </body> </html>

๋‘ jspํŒŒ์ผ์„ <Tomcat-installation-directory>/webapps/ROOT ๋””๋ ‰ํ† ๋ฆฌ์— ๋„ฃ์–ด์ฃผ์„ธ์š”.

์ด์ œ http://localhost:8080/hello.jsp ์— ์ ‘์†์„ ํ•ด๋ณด์„ธ์š”.

 

 

 

์ด๋Ÿฐ๊ฒŒ ๋‚˜์˜ค๋‚˜์š”? ์ด์ œ ์ด๋ฆ„์„ ๋„ฃ๊ณ  submit๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”. 

๊ทธ๋Ÿผ ์˜ˆ์ „์— ๋ดค๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ํผ์ŠคํŠธ ๋„ค์ž„๊ณผ ๋ž˜์ŠคํŠธ ๋„ค์ž„์ด ์ถœ๋ ฅ์ด ๋ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์ด๋…€์„์ด ๋ชฐ๋ž˜ํ•˜๋Š” ์ž‘์—…์ด ๋˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ˆˆ์—๋Š” ๋ณด์ด์ง€ ์•Š์•˜์ง€๋งŒ first_name๊ณผ last_name์ด๋ผ๋Š” ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•œ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  submit๋ฒ„ํŠผ์ด ๋˜ ๋ˆŒ๋ฆฌ๊ฒŒ ๋  ๊ฒฝ์šฐ ๋ฐฉ๊ธˆ ๋งŒ๋“ค์–ด์ง„ ์ฟ ํ‚ค๋“ค์ด ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „์†ก๋˜๊ฒŒ ๋˜๊ฒ ์ฃ . 

์ž, ๊ทธ๋Ÿผ ์ด์ œ ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „์†ก๋˜๋Š” ์ฟ ํ‚ค์ •๋ณด๋ฅผ JSP์—์„œ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์„ ํ•œ๋ฒˆ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 


JSP๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฟ ํ‚ค์ •๋ณด ์ฝ์–ด์˜ค๊ธฐ

์ฟ ํ‚ค๋ฅผ ์ฝ์œผ๋ ค๋ฉด HttpServletRequest ์˜ getCookies( ) ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” Cookie[]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ฟ ํ‚ค๊ฐ€ ์ „์†ก๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ๊ทธ๋Ÿผ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ ์ „์†ก๋œ ์ฟ ํ‚ค์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  


์˜ˆ์ œ

<html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println("<h2>No cookies founds</h2>"); } %> </body> </html>

์œ„ ์ฝ”๋“œ๋ฅผ main.jsp ํŒŒ์ผ์— ๋„ฃ๊ณ  ์ ‘์†ํ•ด๋ณด์„ธ์š”. ์ฟ ํ‚ค์— ์„ค์ •ํ•œ ๊ฐ’๋“ค์ด ๋‚˜์˜ค๊ฒ ์ฃ ? ์•„๋ž˜์ฒ˜๋Ÿผ ๋ง์ด์ฃ .


Found Cookies Name and Value

Name : first_name, Value: ํ™๊ธธ๋™์€
Name : last_name, Value: ํ”Œ๋ ˆ์ด๋ณด์ด


JSP๋ฅผ ์ด์šฉํ•œ ์ฟ ํ‚ค ์‚ญ์ œ

์ด๋ฒˆ์—๋Š” ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ญ ์ฟ ํ‚ค์‚ญ์ œ๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ๋„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๊ธด ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ•œ๋ฒˆ ์ฝ์–ด๋‚˜ ๋ณด์„ธ์š”. ์›น๊ฐœ๋ฐœ์ž๋ผ๋ฉด ์ฟ ํ‚ค ๋‹ค๋ฃจ๋Š” ๋ฒ•์€ ์ž˜ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•  ํ…Œ๋‹ˆ๊นŒ์š”. 

  • ์ฟ ํ‚ค๋ฅผ ์ฝ์–ด์™€์„œ ์ฟ ํ‚ค๊ฐ์ฒด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ฟ ํ‚ค์˜ ๋‚˜์ด๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฌ์„ธ์š”. setMaxAge() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ฑด ์•„์‹œ์ฃ ?

  • ์ด ์ฟ ํ‚ค๋ฅผ ๋‹ค์‹œ response์— ๋„ฃ์–ด ๋˜๋Œ๋ ค๋ณด๋‚ด์„ธ์š”.


์˜ˆ์ œ

์•„๋ž˜ ์˜ˆ์ œ๋Š” "first_name" ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋Š” main.jsp ์†Œ์Šค์ž…๋‹ˆ๋‹ค. ์‚ญ์ œ๋œ ํ›„์— ์ด ์ฟ ํ‚ค๋ฅผ ๋‹ค์‹œ ์ฝ์–ด์˜ค๋ ค๊ณ ํ•˜๋ฉด null์ด ๋‚˜์˜ฌ๊ฒ๋‹ˆ๋‹ค.

<html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; if((cookie.getName( )).compareTo("first_name") == 0 ){ cookie.setMaxAge(0); response.addCookie(cookie); out.print("Deleted cookie: " + cookie.getName( ) + "<br/>"); } out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println( "<h2>No cookies founds</h2>"); } %> </body> </html>

์ด์ œ main.jsp๋ฅผ ์š”์ฒญํ•ด๋ณด์„ธ์š”. http://localhost:8080/main.jsp ์ฃผ์†Œ๋กœ ์š”์ฒญํ•˜๋ฉด ๋˜๊ฒ ์ฃ ? ๊ทธ๋Ÿผ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฑฐ์—์š”.


Cookies Name and Value

Deleted cookie : first_name
Name : first_name, Value: John
Name : last_name, Value: Player

 

 http://localhost:8080/main.jsp ๋ฅผ ๋‹ค์‹œํ•œ๋ฒˆ ์š”์ฒญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒŒ ๋  ๊ฑฐ์—์š”.

 

Found Cookies Name and Value

Name : last_name, Value: Player

 

first_name ์ฟ ํ‚ค๋Š” ์‚ญ์ œ๋˜์–ด์„œ ์•ˆ๋‚˜์˜ค๋„ค์š”.

 

์–ด๋•Œ์š”? ์‰ฝ์ฃ ์ž‰?  

 




 

 

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

 

Servlet๊ณผ JSP ํ•„ํ„ฐ๋Š” ์„œ๋ธ”๋ฆฟ๊ณผ JSPํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” Java ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

  • ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ์˜ ์š”์ฒญ์ด ๋ฐฑ์—”๋“œ๋กœ ๊ฐ€๊ธฐ ์ „์— ๊ฐ€๋กœ์ฑ„๊ธฐ ์œ„ํ•ด์„œ

  • ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์‘๋‹ต์ด ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ด์ง€๊ธฐ ์ „์— ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ

์•„๋ž˜๋Š” ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํ•„ํ„ฐ๋“ค์˜ ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค.

  • Authentication Filters.

  • Data compression Filters

  • Encryption Filters .

  • Filters that trigger resource access events.

  • Image Conversion Filters .

  • Logging and Auditing Filters.

  • MIME-TYPE Chain Filters.

  • Tokenizing Filters .

  • XSL/T Filters That Transform XML Content.

Filter๋Š” deployment descriptor ํŒŒ์ผ์ธ web.xml์„ ํ†ตํ•ด์„œ ๋””ํ”Œ๋กœ์ด ๋˜๊ณ  servlet ์ด๋‚˜ JSP์— ๋งคํ•‘์ด ๋ฉ๋‹ˆ๋‹ค. web.xml ํŒŒ์ผ์€ <Tomcat-installation-directory>\conf ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ์ฐพ์œผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JSP ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ, ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ฐ ํ•„ํ„ฐ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹น์‹ ์ด deployment descriptor์— ์„ ์–ธํ•œ ํ•„ํ„ฐ๋“ค์„ ๋ง์ด์ฃ . ๊ทธ ํ•„ํ„ฐ๋“ค์€ ์ •์˜๋œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. 


Servlet Filter Methods:

ํ•„ํ„ฐ๋Š” ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ๊ทธ๋ƒฅ Java ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.  javax.servlet.Filter ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์ฃ . The javax.servlet.Filter interface ๋Š” ์•„๋ž˜์˜ ์„ธ๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค.

S.N.Method & Description
1public void doFilter (ServletRequest, ServletResponse, FilterChain)
This method is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
2public void init(FilterConfig filterConfig)
This method is called by the web container to indicate to a filter that it is being placed into service.
3public void destroy()
This method is called by the web container to indicate to a filter that it is being taken out of service.


JSP Filter ์˜ˆ์ œ

์•„๋ž˜๋Š” JSP Filter ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๋ฌด์—‡์„ ํ•˜๋Š”๊ณ  ํ•˜๋‹ˆ, ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ๋•Œ๋งˆ๋‹ค ํด๋ผ์ด์–ธํŠธ์˜ IP์ฃผ์†Œ๋ฅผ ํ”„๋ฆฐํŠธํ•˜๊ณ  ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฟŒ๋ ค์ค๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ JSP Filter์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์„ ์ตํž ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”.  

// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; // Implements Filter class public class LogFilter implements Filter { public void init(FilterConfig config) throws ServletException{ // Get init parameter String testParam = config.getInitParameter("test-param"); //Print the init parameter System.out.println("Test Param: " + testParam); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { // Get the IP address of client machine. String ipAddress = request.getRemoteAddr(); // Log the IP address and current timestamp. System.out.println("IP "+ ipAddress + ", Time " + new Date().toString()); // Pass request back down the filter chain chain.doFilter(request,response); } public void destroy( ){ /* Called before the Filter instance is removed from service by the web container*/ } }

์ด์ œ LogFilter.java๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ  LogFilter.class ํŒŒ์ผ์„ <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes ๋””๋ ‰ํ† ๋ฆฌ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. ๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์—์„œ ๋งคํ•‘์‹œ์ผœ์ฃผ๋Š” ์ž‘์—…์„ ๋” ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. 


JSP Filter Mapping in Web.xml:

web.xml ์—์„œ ํ•„ํ„ฐ๋ฅผ ๋งคํ•‘ํ•  ๋•Œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

<filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

์œ„ ์†Œ์Šค์ฒ˜๋Ÿผ ๋งคํ•‘ํ•˜๋ฉด ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ๊ณผ JSP์— ์ ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค.  url pattern์—์„œ /* ์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ๋ชจ๋“  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ด๊ณณ์— ํŠน์ • servlet ์ด๋‚˜ JSP๋ฅผ ๋„ฃ์–ด ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋ง์ด์ฃ .

์ด์ œ JSPํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•˜๋ฉด ์›น์„œ๋ฒ„ ๋กœ๊ทธ์—์„œ ์ƒ์„ฑ๋œ ๋กœ๊ทธ๋ฅผ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  Log4J ๋กœ๊ฑฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค๋ฅธ ํŒŒ์ผ์— ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š”์ ๋„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.


Filter ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉํ•˜๊ธฐ

์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ํ•„ํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ฐ๊ฐ ๋งคํ•‘์„ ํ•˜๋Š”๊ฑฐ์ฃ . 

<filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter> <filter-name>AuthenFilter</filter-name> <filter-class>AuthenFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>AuthenFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

Filters Application Order:

ํ•„ํ„ฐ์˜ ์ ์šฉ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ •์˜๋œ ํ•„ํ„ฐ๊ฐ€ ๋จผ์ € ์ ์šฉ์ด ๋˜๋Š”๊ฑฐ์ฃ . ์œ„ ์˜ˆ์ œ์—์„œ๋Š” LogFilter๊ฐ€ AuthenFilter๋ณด๋‹ค ์šฐ์„  ์ ์šฉ์ด ๋˜๋Š”๊ฑฐ๊ณ  ์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” AuthenFilter๊ฐ€ LogFilter๋ณด๋‹ค ์šฐ์„  ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

<filter-mapping> <filter-name>AuthenFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

 

 

 

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

 

์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์›น์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. GET๋ฐฉ์‹๊ณผ POST๋ฐฉ์‹์ด ๊ทธ๊ฒƒ์ด์ฃ .  

RESTFUL์—์„œ๋Š” ๋” ๋งŽ์€ ๋ฐฉ์‹์ด ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” GET ๊ณผ POST์— ๋Œ€ํ•ด์„œ๋งŒ JSP๋กœ ํ•ธ๋“ค๋ง ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

JSP๋ฅผ ์ด์šฉํ•œ FORM ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ

์›น๋ธŒ๋ผ์šฐ์ €์—์„œ form๋ฐ์ดํƒ€๋ฅผ ์ด์šฉํ•ด์„œ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด JSP์—์„œ ๊ทธ form ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

  • getParameter(): You call request.getParameter() method to get the value of a form parameter.

  • getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.

  • getParameterNames(): Call this method if you want a complete list of all parameters in the current request.

  • getInputStream(): Call this method to read binary data stream coming from the client.


URL์„ ์ด์šฉํ•œ GET๋ฐฉ์‹ ์˜ˆ์ œ

์•„๋ž˜ URL์€ get๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์›น์„œ๋ฒ„๋กœ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. first_name์ด๋ผ๋Š” ๋ณ€์ˆ˜์— "๊ธธ๋™"๋ผ๋Š” ๊ฐ’์ด ๋“ค์–ด์žˆ๊ณ  "ํ™"์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ALI๋ผ๋Š” ๊ฐ’์ด ๋“ค์–ด์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

http://localhost:8080/main.jsp?first_name=๊ธธ๋™&last_name=ํ™

 

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜๋Š” main.jsp ์˜ ์†Œ์Šค์ž…๋‹ˆ๋‹ค.  getParameter() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋„˜๊ฒจ์ค€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

<html> <head> <title>Using GET Method to Read Form Data</title> </head> <body> <center> <h1>Using GET Method to Read Form Data</h1> <ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html>

์ด์ œ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:8080/main.jsp?first_name=๊ธธ๋™&last_name=ํ™ ์„ ํƒ€์ดํ•‘ํ•ด์„œ ์›น์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ํ•ด๋ณด์„ธ์š”. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

 

Using GET Method to Read Form Data

  • First Name: ๊ธธ๋™

  • Last Name: ํ™


Form์„ ์ด์šฉํ•œ GET ๋ฐฉ์‹ ์˜ˆ์ œ

์•„๋ž˜๋Š” HTML ํƒœ๊ทธ ์ค‘์—์„œ formํƒœ๊ทธ๋ฅผ ์ด์šฉํ•œ get๋ฐฉ์‹ ์š”์ฒญ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. 

<html> <body> <form action="main.jsp" method="GET"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> </body> </html>

์œ„ ์†Œ์Šค๋ฅผ Hello.htmํŒŒ์ผ์— ์ž‘์„ฑํ•˜๊ณ  <Tomcat-installation-directory>/webapps/ROOT ๋””๋ ‰ํ† ๋ฆฌ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. 

http://localhost:8080/Hello.htm ๋ฅผ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ํ™”๋ฉด์— ๋ณด์—ฌ์ง‘๋‹ˆ๋‹ค.  


 

์ด์ œ ๋นˆ๋ฐ•์Šค์— ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  submit๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”. ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด main.jsp์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ณ  ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ main.jsp์—์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค 

์ฒ˜๋ฆฌ๋œ ๊ฒฐ๊ณผ๋Š” ์œ„์—์„œ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•˜๊ฒŒ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

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

 

Web server๊ฐ€ HTTP request์— ๋Œ€ํ•œ ์‘๋‹ต์„ ๋ธŒ๋ผ์šฐ์ €์— ๋ณด๋‚ผ๋•Œ ๊ทธ ์‘๋‹ต์€ ์ „ํ˜•์ ์œผ๋กœ ์ƒํƒœ์ •๋ณด์™€ ๋ช‡๋ช‡ ์‘๋‹ต ํ—ค๋”๋“ค ๊ทธ๋ฆฌ๊ณ  ๋นˆ ์ค„ ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์„œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๋ณดํ†ต ์•„๋ž˜์ฒ˜๋Ÿผ ์‘๋‹ต์ด ๊ตฌ์„ฑ๋œ๋‹ค.

HTTP/1.1 200 OK Content-Type: text/html Header2: ... ... HeaderN: ... ( ๋นˆ ์ค„ ) <!doctype ...> <html> <head>...</head> <body> ... </body> </html>

์ƒํƒœ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์ค„์€ HTTP ๋ฒ„์ „ (์œ„ ์˜ˆ์ œ์—์„œ๋Š” HTTP/1.1 ), ์ƒํƒœ์ฝ”๋“œ ( ์œ„ ์˜ˆ์ œ์—์„œ๋Š” 200 ), ๊ทธ๋ฆฌ๊ณ  ์ƒํƒœ์ฝ”๋“œ์— ๋งž๋Š” ์งง์€ ๋ฉ”์‹œ์ง€ (์œ„ ์˜ˆ์—์„œ๋Š” OK )๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ์€ HTTP 1.1 ์‘๋‹ตํ—ค๋”์˜ ์œ ์šฉํ•œ ์ •๋ณด๋“ค์ž…๋‹ˆ๋‹ค.

 

HeaderDescription
AllowThis header specifies the request methods (GET, POST, etc.) that the server supports.
Cache-ControlThis header specifies the circumstances in which the response document can safely be cached. It can have values public, private or no-cache etc. Public means document is cacheable, Private means document is for a single user and can only be stored in private (nonshared) caches and no-cache means document should never be cached.
ConnectionThis header instructs the browser whether to use persistent in HTTP connections or not. A value of close instructs the browser not to use persistent HTTP connections and keep-alive means using persistent connections.
Content-DispositionThis header lets you request that the browser ask the user to save the response to disk in a file of the given name.
Content-EncodingThis header specifies the way in which the page was encoded during transmission.
Content-LanguageThis header signifies the language in which the document is written. For example en, en-us, ru, etc.
Content-LengthThis header indicates the number of bytes in the response. This information is needed only if the browser is using a persistent (keep-alive) HTTP connection.
Content-TypeThis header gives the MIME (Multipurpose Internet Mail Extension) type of the response document.
ExpiresThis header specifies the time at which the content should be considered out-of-date and thus no longer be cached.
Last-ModifiedThis header indicates when the document was last changed. The client can then cache the document and supply a date by an If-Modified-Since request header in later requests.
LocationThis header should be included with all responses that have a status code in the 300s. This notifies the browser of the document address. The browser automatically reconnects to this location and retrieves the new document.
RefreshThis header specifies how soon the browser should ask for an updated page. You can specify time in number of seconds after which a page would be refreshed.
Retry-AfterThis header can be used in conjunction with a 503 (Service Unavailable) response to tell the client how soon it can repeat its request.
Set-CookieThis header specifies a cookie associated with the page.


HttpServletResponse

 HttpServletResponse ๊ฐ์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.  

 

S.N.Method & Description
1String encodeRedirectURL(String url)
Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.
2String encodeURL(String url)
Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.
3boolean containsHeader(String name)
Returns a boolean indicating whether the named response header has already been set.
4boolean isCommitted()
Returns a boolean indicating if the response has been committed.
5void addCookie(Cookie cookie)
Adds the specified cookie to the response.
6void addDateHeader(String name, long date)
Adds a response header with the given name and date-value.
7void addHeader(String name, String value)
Adds a response header with the given name and value.
8void addIntHeader(String name, int value)
Adds a response header with the given name and integer value.
9void flushBuffer()
Forces any content in the buffer to be written to the client.
10void reset()
Clears any data that exists in the buffer as well as the status code and headers.
11void resetBuffer()
Clears the content of the underlying buffer in the response without clearing headers or status code.
12void sendError(int sc)
Sends an error response to the client using the specified status code and clearing the buffer.
13void sendError(int sc, String msg)
Sends an error response to the client using the specified status.
14void sendRedirect(String location)
Sends a temporary redirect response to the client using the specified redirect location URL.
15void setBufferSize(int size)
Sets the preferred buffer size for the body of the response.
16void setCharacterEncoding(String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.
17void setContentLength(int len)
Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.
18void setContentType(String type)
Sets the content type of the response being sent to the client, if the response has not been committed yet.
19void setDateHeader(String name, long date)
Sets a response header with the given name and date-value.
20void setHeader(String name, String value)
Sets a response header with the given name and value.
21void setIntHeader(String name, int value)
Sets a response header with the given name and integer value.
22void setLocale(Locale loc)
Sets the locale of the response, if the response has not been committed yet.
23void setStatus(int sc)
Sets the status code for this response.


HTTP Header Response ์˜ˆ์ œ

์•„๋ž˜ ์†Œ์Šค๋Š” ๋””์ง€ํ„ธ ์‹œ๊ณ„๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด์„œ Refreshํ—ค๋”์— 5์ดˆ๋งˆ๋‹ค ์ƒˆ๋กœ๊ณ ์นจํ•˜๋„๋ก setIntHeader() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ์„ธํŒ…ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

<%@ page import="java.io.*,java.util.*" %> <html> <head> <title>Auto Refresh Header Example</title> </head> <body> <center> <h2>Auto Refresh Header Example</h2> <% // Set refresh, autoload time as 5 seconds response.setIntHeader("Refresh", 5); // Get current time Calendar calendar = new GregorianCalendar(); String am_pm; int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); if(calendar.get(Calendar.AM_PM) == 0) am_pm = "AM"; else am_pm = "PM"; String CT = hour+":"+ minute +":"+ second +" "+ am_pm; out.println("Current Time is: " + CT + "\n"); %> </center> </body> </html>

main.jsp ์— ์œ„ ์†Œ์Šค๋ฅผ ๋„ฃ๊ณ  ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ 5์ดˆ๋งˆ๋‹ค ์ƒˆ๋กœ๊ณ ์นจ๋˜๋Š” ๋””์ง€ํ„ธ ์‹œ๊ณ„๋ฅผ ๋ณด์‹ค ์ˆ˜ ์žˆ์œผ์‹ค๊ฑฐ์—์š”.  

Auto Refresh Header Example

Current Time is: 9:44:50 PM

 

 

 

 

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

 

๐Ÿ’ป Programming/JSP

[JSP] Request Object ( Client Request )

์‚ฌ์šฉ์ž๊ฐ€ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์›นํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•˜๋ฉด ์›น๋ธŒ๋ผ์šฐ์ €๋Š” ์›น์„œ๋ฒ„๋กœ ๋งŽ์€ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด ์ •๋ณด๋“ค์€ ๋ชจ๋‘ HTTP request ์˜ ํ—ค๋” ๋‚ด์— ํฌํ•จ๋˜์–ด ์ „๋‹ฌ๋œ๋‹ค. HTTP Protocol ์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.

 

์•„๋ž˜ ๋‚ด์šฉ์€ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์›น์„œ๋ฒ„์ชฝ์œผ๋กœ ๋ณด๋‚ด๋Š” ์ •๋ณด ์ค‘์— ์ค‘์š”ํ•œ ํ—ค๋” ์ •๋ณด๋ฅผ ๊ฐ„์ถ”๋ฆฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

HeaderDescription
AcceptThis header specifies the MIME types that the browser or other clients can handle. Values of image/png or image/jpeg are the two most common possibilities.
Accept-CharsetThis header specifies the character sets the browser can use to display the information. For example ISO-8859-1.
Accept-EncodingThis header specifies the types of encodings that the browser knows how to handle. Values of gzip or compress are the two most common possibilities.
Accept-LanguageThis header specifies the client's preferred languages in case the servlet can produce results in more than one language. For example en, en-us, ru, etc.
AuthorizationThis header is used by clients to identify themselves when accessing password-protected Web pages.
ConnectionThis header indicates whether the client can handle persistent HTTP connections. Persistent connections permit the client or other browser to retrieve multiple files with a single request. A value of Keep-Alive means that persistent connections should be used
Content-LengthThis header is applicable only to POST requests and gives the size of the POST data in bytes.
CookieThis header returns cookies to servers that previously sent them to the browser.
HostThis header specifies the host and port as given in the original URL.
If-Modified-SinceThis header indicates that the client wants the page only if it has been changed after the specified date. The server sends a code, 304 which means Not Modified header if no newer result is available.
If-Unmodified-SinceThis header is the reverse of If-Modified-Since; it specifies that the operation should succeed only if the document is older than the specified date.
RefererThis header indicates the URL of the referring Web page. For example, if you are at Web page 1 and click on a link to Web page 2, the URL of Web page 1 is included in the Referer header when the browser requests Web page 2.
User-AgentThis header identifies the browser or other client making the request and can be used to return different content to different types of browsers.



HttpServletRequest 

 HttpServletRequest ๊ฐ์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

S.N.Method & Description
1Cookie[] getCookies()
Returns an array containing all of the Cookie objects the client sent with this request.
2Enumeration getAttributeNames()
Returns an Enumeration containing the names of the attributes available to this request.
3Enumeration getHeaderNames()
Returns an enumeration of all the header names this request contains.
4Enumeration getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters contained in this request.
5HttpSession getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.
6HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
7Locale getLocale()
Returns the preferred Locale that the client will accept content in, based on the Accept-Language header
8Object getAttribute(String name)
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
9ServletInputStream getInputStream()
Retrieves the body of the request as binary data using a ServletInputStream.
10String getAuthType()
Returns the name of the authentication scheme used to protect the servlet, for example, "BASIC" or "SSL," or null if the JSP was not protected
11String getCharacterEncoding()
Returns the name of the character encoding used in the body of this request.
12String getContentType()
Returns the MIME type of the body of the request, or null if the type is not known.
13String getContextPath()
Returns the portion of the request URI that indicates the context of the request.
14String getHeader(String name)
Returns the value of the specified request header as a String.
15String getMethod()
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
16String getParameter(String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist.
17String getPathInfo()
Returns any extra path information associated with the URL the client sent when it made this request.
18String getProtocol()
Returns the name and version of the protocol the request.
19String getQueryString()
Returns the query string that is contained in the request URL after the path.
20String getRemoteAddr()
Returns the Internet Protocol (IP) address of the client that sent the request.
21String getRemoteHost()
Returns the fully qualified name of the client that sent the request.
22String getRemoteUser()
Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.
23String getRequestURI()
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
24String getRequestedSessionId()
Returns the session ID specified by the client.
25String getServletPath()
Returns the part of this request's URL that calls the JSP.
26String[] getParameterValues(String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
27boolean isSecure()
Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
28int getContentLength()
Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known.
29int getIntHeader(String name)
Returns the value of the specified request header as an int.
30int getServerPort()
Returns the port number on which this request was received.


HTTP Header Request ์˜ˆ์ œ

์•„๋ž˜๋Š” HttpServletRequest์˜ getHeaderNames()๋ฉ”์†Œ๋“œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ํ˜„์žฌ ์š”์ฒญ์— ๋Œ€ํ•œ HTTP header ์ •๋ณด๋ฅผ ์ฝ์–ด์™€์„œ Enumeration์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Enumeration์„ ์–ป์–ด์˜ค๋ฉด ์šฐ๋ฆฌ๋Š” hasMoreElements() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ nextElement() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ—ค๋”๋ช…์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋Š” ๊ทธ๋ ‡๊ฒŒ ์–ป์–ด์˜จ ํ—ค๋”๋ช…์„ ์ด์šฉํ•ด์„œ ํ•ด๋‹น ํ—ค๋”๋ช…์— ๋Œ€ํ•œ ์‹ค์ œ ๊ฐ’๊นŒ์ง€ ์–ป์–ด์™€์„œ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. 

<%@ page import="java.io.*,java.util.*" %> <html> <head> <title>HTTP Header Request Example</title> </head> <body> <center> <h2>HTTP Header Request Example</h2> <table width="100%" border="1" align="center"> <tr bgcolor="#949494"> <th>Header Name</th><th>Header Value(s)</th> </tr> <% Enumeration headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String paramName = (String)headerNames.nextElement(); out.print("<tr><td>" + paramName + "</td>\n"); String paramValue = request.getHeader(paramName); out.println("<td> " + paramValue + "</td></tr>\n"); } %> </table> </center> </body> </html>

์œ„ ์†Œ์Šค์ฝ”๋“œ๋ฅผ main.jsp ํŒŒ์ผ์— ๋„ฃ๊ณ  ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•ด๋ณด์„ธ์š”. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์„๊ฒƒ์ž…๋‹ˆ๋‹ค.


HTTP Header Request Example

Header NameHeader Value(s)
accept*/*
accept-languageen-us
user-agentMozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; InfoPath.2; MS-RTC LM 8)
accept-encodinggzip, deflate
hostlocalhost:8080

connection

Keep-Alive
cache-control

no-cache

 

 

 

 

 

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

 

JSP Implicit Objects๋Š” Java ๊ฐ์ฒด๋“ค์ž…๋‹ˆ๋‹ค. ๋ช…์‹œ์ ์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ์„ ์–ธํ•˜์ง€ ์•Š์•„๋„ JSP Container์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด๋“ค์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ JSP Implicit Objects ๋ฅผ pre-defined ๋ณ€์ˆ˜๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

JSP ๋Š” ์•„ํ™‰๊ฐ€์ง€์˜ Implicit Objects๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ €๋„ ์•„๋Š” ๊ฐ์ฒด๋“ค์ด ๋ˆˆ์— ๋ณด์ด๋„ค์š”.

 

ObjectDescription
requestThis is the HttpServletRequest object associated with the request.
responseThis is the HttpServletResponse object associated with the response to the client.
outThis is the PrintWriter object used to send output to the client.
sessionThis is the HttpSession object associated with the request.
applicationThis is the ServletContext object associated with application context.
configThis is the ServletConfig object associated with the page.
pageContextThis encapsulates use of server-specific features like higher performance JspWriters.
pageThis is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
ExceptionThe Exception object allows the exception data to be accessed by designated JSP.


The request Object:

request ๊ฐ์ฒด๋Š” javax.servlet.http.HttpServletRequest ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค JSP engine ์ด ํ•ด๋‹น ์š”์ฒญ์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด request๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. request ๊ฐ์ฒด๋Š” HTTP header ์ •๋ณด๋ฅผ ์–ป์–ด์˜ค๊ธฐ์œ„ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. form data, cookies, HTTP methods๊ฐ™์€ ์ •๋ณด๋“ค์ด ํ—ค๋”์ •๋ณด์— ์žˆ๊ฒ ์ฃ .

request ๊ฐ์ฒด์™€ ๊ด€๋ จ๋œ ๋ฉ”์†Œ๋“œ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด ๋ฐ ์‚ฌ์šฉ๋ฒ•์€ ์˜ค๋ฅธ์ชฝ ๋งํฌ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  JSP - Client Request.


The response Object:

response ๊ฐ์ฒด๋Š” javax.servlet.http.HttpServletResponse ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ์„œ๋ฒ„๊ฐ€ request ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ด๊ธฐ์œ„ํ•œ response ๊ฐ์ฒด๋„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

response ๊ฐ์ฒด๋Š” ๋˜ํ•œ ์ƒˆ๋กœ์šด HTTPํ—ค๋” ์ƒ์„ฑ์„ ๋‹ค๋ฃจ๊ธฐ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ JSP ๊ฐœ๋ฐœ์ž๋“ค์€ ์ฟ ํ‚ค, ๋‚ ์งœ ์Šคํƒฌํ”„,  HTTP ์ƒํƒœ ์ฝ”๋“œ๋“ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

response ๊ฐ์ฒด์™€ ๊ด€๋ จ๋œ ๋ฉ”์†Œ๋“œ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด ๋ฐ ์‚ฌ์šฉ๋ฒ•์€ ์šฐ์ธก ๋งํฌ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  JSP - Server Response.


The out Object:

out ๊ฐ์ฒด๋Š” javax.servlet.jsp.JspWriter ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์ด๋ฉฐ content๋ฅผ response์•ˆ์— ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์ฒ˜์Œ์˜ JspWriter ๊ฐ์ฒด๋Š” ํŽ˜์ด์ง€์˜ ๋ฒ„ํผ ์‚ฌ์šฉ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.  Buffering ๊ธฐ๋Šฅ์€ page ๋””๋ ‰ํ‹ฐ๋ธŒ์˜ buffered='false' ์†์„ฑ์„ ํ†ตํ•ด์„œ ์‰ฝ๊ฒŒ off ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JspWriter ๊ฐ์ฒด๋Š” java.io.PrintWriter ํด๋ž˜์Šค์™€ ๊ฑฐ์˜ ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ JspWriter๋Š” ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฒ„ํผ๋ง์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. PrintWriter ๊ฐ์ฒด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ JspWriter ๋Š” IOExceptions์„ ๋˜์ง‘๋‹ˆ๋‹ค.

 

 boolean, char, int, double, object, String, etc. ์„ ์“ฐ๊ณ ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

MethodDescription
out.print(dataType dt)Print a data type value
out.println(dataType dt)Print a data type value then terminate the line with new line character.
out.flush()Flush the stream.


The session Object:

session๊ฐ์ฒด๋Š” javax.servlet.http.HttpSession์˜ ์ธ์Šคํ„ด์Šค์ฃ .  Java Servlets์˜ ์„ธ์…˜๊ณผ ๋™์ผํ•œ ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

session ๊ฐ์ฒด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ์š”์ฒญ์„ ํ•˜๊ฒŒ ๋  ๋•Œ ๊ทธ ์š”์ฒญ๋“ค ์‚ฌ์ด์— ์„ธ์…˜์„ ์ถ”์  ๋ฐ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.  

session ๊ฐ์ฒด์˜ ์‚ฌ์šฉ๋ฒ•์€ ์šฐ์ธก ๋งํฌ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  JSP - Session Tracking.


The application Object:

application ๊ฐ์ฒด๋Š” ServletContext๊ฐ์ฒด์˜ wrapper๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” javax.servlet.ServletContext ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค.

 ์ด ๊ฐ์ฒด๋Š” JSP page ์ƒ๋ช…์ฃผ๊ธฐ ์ „์ฒด๋ฅผ ๋Œ€ํ‘œํ•œ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€ ์‚ด์•„์žˆ๋Š” ๊ฐ์ฒด์ฃ . JSP ํŽ˜์ด์ง€๊ฐ€ ์ดˆ๊ธฐํ™”๋  ๋•Œ ์ƒ์„ฑ๋˜๊ณ   jspDestroy()๋ฉ”์†Œ๋“œ์— ์˜ํ•ด JSPํŽ˜์ด์ง€๊ฐ€ ์†Œ๋ฉธ๋  ๋•Œ ์ด ๊ฐ์ฒด๋„ ์†Œ๋ฉธ๋ฉ๋‹ˆ๋‹ค.

 application ๊ฐ์ฒด์— ์†์„ฑ์„ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  JSP ํŒŒ์ผ๋“ค์ด ์ด๊ณณ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์ด ์ƒ๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Application ๊ฐ์ฒด์˜ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฒ•์€ ์šฐ์ธก ๋งํฌ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  JSP - Hits Counter


The config Object:

config ๊ฐ์ฒด๋Š” javax.servlet.ServletConfig ์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค.  

์ด ๊ฐ์ฒด๋Š” JSP ๊ฐœ๋ฐœ์ž๊ฐ€ Servlet ์ด๋‚˜ JSP ์—”์ง„ ์ดˆ๊ธฐํ™” ํŒŒ๋ผ๋ฏธํ„ฐ ( paths ๋˜๋Š” file ์œ„์น˜ ๋“ฑ )์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

๋งŽ์ด ์“ฐ์ด์ง€ ์•Š๋Š” ๊ฐ์ฒด์ด๋ฉฐ ์•„๋งˆ ์“ฐ๊ฒŒ ๋œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์“ฐ๋Š” ๊ฒƒ ๋ง๊ณ ๋Š” ์ •๋ง ์“ธ์ผ์ด ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 


config.getServletName();


์ด๊ฑด ์„œ๋ธ”๋ฆฟ๋ช…์„ ์–ป์–ด์˜ค๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. WEB-INF\web.xml ํŒŒ์ผ์˜   <servlet-name> ์š”์†Œ์— ๋“ค์–ด์žˆ๋Š” ์ด๋ฆ„์„ ์–ป์–ด์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 


The pageContext Object:

pageContext ๊ฐ์ฒด๋Š”  javax.servlet.jsp.PageContext ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค.   pageContext ๊ฐ์ฒด๋Š” JSP page ์ „์ฒด๋ฅผ ๋Œ€ํ‘œํ•ฉ๋‹ˆ๋‹ค.

 ์ด ๊ฐ์ฒด๋Š” ํŽ˜์ด์ง€ ์ •๋ณด๋ฅผ ์–ป์–ด์˜ค๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.  

์ด ๊ฐ์ฒด๋Š” ๊ฐ ์š”์ฒญ๋งˆ๋‹ค request ์™€ response๊ฐ์ฒด๋กœ์˜ ๋ž˜ํผ๋Ÿฐ์Šค๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. application, config, session, ๊ทธ๋ฆฌ๊ณ  out ๊ฐ์ฒด๋“ค์€ ์ด ๊ฐ์ฒด์˜ ์†์„ฑ์— ์ ‘๊ทผํ•ด์„œ ์–ป์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  pageContext ๊ฐ์ฒด๋Š” ๋˜ํ•œ ํ•ด๋‹น JSP page์—์„œ ๋ฐœ์ƒ๋œ directives์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ( buffering ์ •๋ณด,  errorPageURL, ๊ทธ๋ฆฌ๊ณ  page scope๊ฐ™์€ ์ •๋ณด๋“ค ๋ง์ด์ฃ  ).

 PageContext ํด๋ž˜์Šค๋Š” PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, ๊ทธ๋ฆฌ๊ณ  APPLICATION_SCOPE ํ•„๋“œ๋“ค์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. 40๊ฐœ ์ด์ƒ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ ๊ทธ ์ค‘ ๋ฐ˜์€ javax.servlet.jsp.JspContext ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์€ ๋ฉ”์†Œ๋“œ๋“ค์ž…๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ฉ”์†Œ๋“œ ์ค‘ ํ•˜๋‚˜๋Š” removeAttribute์ธ๋ฐ ์ด ๋ฉ”์†Œ๋“œ๋Š”  ํ•˜๋‚˜ ๋˜๋Š” ๋‘๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. pageContext.removeAttribute ("attrName") ๋Š” ๋ชจ๋“  scope์—์„œ attrName ์†์„ฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์— ๋‚˜์˜จ ๋ฌธ์žฅ์€ page scope์—์„œ๋งŒ ํ•ด๋‹น ์†์„ฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. 

 

  pageContext.removeAttribute("attrName", PAGE_SCOPE);


pageContext ๊ฐ์ฒด์— ๋Œ€ํ•œ ์‚ฌ์šฉ๋ฒ•์€ ์šฐ์ธก ๋งํฌ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JSP - File Uploading.


The page Object:

์‹ค์ œ ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. JSP page์ „์ฒด๋ฅผ ๋Œ€ํ‘œํ•˜๋Š” ๊ฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค..

์ด ํŽ˜์ด์ง€ ๊ฐ์ฒด๋Š” ์‹ค์ œ๋กœ ์ž๋ฐ”์—์„œ ๋‚˜์˜ค๋Š” this ๊ฐ์ฒด์˜ ๋™์˜์–ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.  


The exception Object:

exception ๊ฐ์ฒด๋Š” wrapper ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์ด์ „ ํŽ˜์ด์ง€์—์„œ ๋˜์ ธ์ง„ exception์„ ๊ฐ€์ง€๊ณ  ์žˆ์ฃ .  ๋ญ ์ด๊ฑฐ์•ผ ์ž๋ฐ”๋ฅผ ํ•˜์‹  ๋ถ„๋“ค์ด๋ผ๋ฉด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ์ฒด๋ผ๊ณ  ๊ธˆ๋ฐฉ ์•„์‹ค ๊ฒ๋‹ˆ๋‹ค. ๋ญ ๋” ์„ค๋ช…ํ• ๊ฒŒ ์—†๋„ค์š”.

์ด ๊ฐ์ฒด์— ๋Œ€ํ•œ ์‚ฌ์šฉ๋ฒ•์€ ์šฐ์ธก ๋งํฌ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JSP - Exception Handling.

 

 

 

 

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

๐Ÿ’ป Programming/JSP

[JSP] Actions ( ์•ก์…˜ )

JSP actions์„ ์ด์šฉํ•˜๋ฉด ๋™์ ์œผ๋กœ ํŒŒ์ผ์„ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์ž๋ฐ”๋นˆ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋ณด๊ณ ์žˆ๋Š” ์‚ฌ์šฉ์ž๋ฅผ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ํฌ์›Œ๋”ฉ์‹œํ‚ค๊ฑฐ๋‚˜ ์ž๋ฐ”ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์œ„ํ•œ HTML์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

<jsp:action_name attribute="value" />

Action ์š”์†Œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฏธ๋ฆฌ ์ •์˜๋˜์–ด์žˆ๋Š” ๊ธฐ๋Šฅ๋“ค์ž…๋‹ˆ๋‹ค.

SyntaxPurpose
jsp:includeIncludes a file at the time the page is requested
jsp:useBeanFinds or instantiates a JavaBean
jsp:setPropertySets the property of a JavaBean
jsp:getPropertyInserts the property of a JavaBean into the output
jsp:forwardForwards the requester to a new page
jsp:pluginGenerates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin
jsp:elementDefines XML elements dynamically.
jsp:attributeDefines dynamically defined XML element's attribute.
jsp:bodyDefines dynamically defined XML element's body.

 

jsp:text

Use to write template text in JSP pages and documents.


Common Attributes ( ๊ณตํ†ต ์†์„ฑ )

๋ชจ๋“  ์•ก์…˜ ์š”์†Œ๋“ค์— ์ ์šฉ๋˜๋Š” ๊ณตํ†ต์†์„ฑ์ด ๋‘๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.  id ์†์„ฑ๊ณผ scope ์†์„ฑ์ž…๋‹ˆ๋‹ค.

  • Id :  id ์†์„ฑ์€ ๋ญ ๋‘๋งํ•  ํ•„์š”๋„ ์—†๋Š” ์†์„ฑ์ด์ฃ . Action ์š”์†Œ์˜ ์œ ๋‹ˆํฌํ•œ id๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋ฏผ๋ฒˆํ˜ธ๊ฐ™์€๊ฑฐ์ฃ . ๊ทธ๋ฆฌ๊ณ  JSP page๋‚ด์—์„œ ์ด id๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Action ์–ด๋–ค ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค๋ฉด ๋ฌต์‹œ์ ์œผ๋กœ ์ •์˜๋˜์–ด์žˆ๋Š” ๊ฐ์ฒด์ธ PageContext๋ฅผ ํ†ตํ•ด์„œ ์•ก์…˜ id ๊ฐ’์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

  • scope : Action ์š”์†Œ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.  id ์†์„ฑ๊ณผ scope ์†์„ฑ์€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น id ๋ฅผ ๊ฐ–๋Š” ๊ฐ์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . scope ์†์„ฑ์€ ๋‹ค์Œ ๋„ค๊ฐ€์ง€ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (a) page, (b)request, (c)session, and (d) application.
     

The <jsp:include> Action

ํŽ˜์ด์ง€์— ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์•ก์…˜์ด์ฃ . 

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

<jsp:include page="relative URL" flush="true" />

 include directive์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ํŽ˜์ด์ง€๊ฐ€ ์š”์ฒญ์ด ๋  ๋•Œ ํŒŒ์ผ์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. include directive์˜ ๊ฒฝ์šฐ์—๋Š” ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ(๋ฒˆ์—ญ)๋  ๋•Œ ํŒŒ์ผ์„ ์‚ฝ์ž…ํ•˜์ฃ . 

 

include action๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

AttributeDescription
pageThe relative URL of the page to be included.
flushThe boolean attribute determines whether the included resource has its buffer flushed before it is included.

Example:

date.jsp  

<p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p>

main.jsp  

<html> <head> <title>The include Action Example</title> </head> <body> <center> <h2>The include action Example</h2> <jsp:include page="date.jsp" flush="true" /> </center> </body> </html>

 

 

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

The include action Example

Today's date: 12-Sep-2010 14:54:22

The <jsp:useBean> Action

useBean ์•ก์…˜์€ ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. ํŠน์ • ๊ฐ์ฒด๋ฅผ ๊ฒ€์ƒ‰์„ ํ•ด์„œ ์–ป์–ด์˜ค๋Š”๋ฐ ๋งŒ์•ฝ ๊ฒ€์ƒ‰์ด ์‹คํŒจํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์„ ํ•ด์ค๋‹ˆ๋‹ค.  

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

<jsp:useBean id="name" class="package.class" />

๋นˆ ํด๋ž˜์Šค๊ฐ€ ํ•œ๋ฒˆ ๋กœ๋“œ๋˜๋ฉด ๋นˆ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์ฝ์–ด์˜ค๊ธฐ ์œ„ํ•ด์„œ jsp:setProperty ์™€ jsp:getProperty ์•ก์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

useBean action๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

AttributeDescription
classDesignates the full package name of the bean.
typeSpecifies the type of the variable that will refer to the object.
beanNameGives the name of the bean as specified by the instantiate () method of the java.beans.Beans class.

 

The <jsp:setProperty> Action

setProperty ์•ก์…˜์€ ๋นˆ์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. Bean ์ด ๋‹น์—ฐํžˆ ์ •์˜๊ฐ€ ๋˜์–ด์žˆ์–ด์•ผ ๊ฒ ์ฃ .  

์ด ์•ก์…˜์€ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 

1.  jsp:useBean ์•ก์…˜ ์ดํ›„์—, ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•

<jsp:useBean id="myName" ... /> ... <jsp:setProperty name="myName" property="someProperty" .../>

 

2. jsp:useBean ์•ก์…˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•

<jsp:useBean id="myName" ... > ... <jsp:setProperty name="myName" property="someProperty" .../> </jsp:useBean>

1๋ฒˆ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” Bean์ด ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ ๊ธฐ์กด์˜ Bean์ด ๊ฒ€์ƒ‰๋˜๊ฑฐ๋‚˜ ์ƒ๊ด€์—†์ด ๋ชจ๋‘ jsp:setProperty ๊ฐ€ ์‹คํ–‰์ด ๋˜์ง€๋งŒ 2๋ฒˆ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์ƒˆ๋กœ์šด Bean์ด ์ƒ์„ฑ๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋งŒ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.  

 

setProperty ์•ก์…˜์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

AttributeDescription
nameDesignates the bean whose property will be set. The Bean must have been previously defined.
propertyIndicates the property you want to set. A value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
valueThe value that is to be assigned to the given property. The the parameter's value is null, or the parameter does not exist, the setProperty action is ignored.
paramThe param attribute is the name of the request parameter whose value the property is to receive. You can't use both value and param, but it is permissible to use neither.

The <jsp:getProperty> Action

getProperty ์•ก์…˜์€ Bean์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฝ์–ด์™€์„œ String์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.  

getProperty ์•ก์…˜์€ ๋‘๊ฐœ์˜ ์†์„ฑ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋ฒ•๊ณผ ์†์„ฑ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.  

<jsp:useBean id="myName" ... /> ... <jsp:getProperty name="myName" property="someProperty" .../>

getProperty ์•ก์…˜์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ๋‘๊ฐœ์˜ ์†์„ฑ ๋ชจ๋‘ ํ•„์ˆ˜๋กœ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

AttributeDescription
nameThe name of the Bean that has a property to be retrieved. The Bean must have been previously defined.
propertyThe property attribute is the name of the Bean property to be retrieved.

Example:

ํ…Œ์ŠคํŠธ ๋นˆ์„ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.

/* File: TestBean.java */ package action; public class TestBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } }

๊ทธ๋ฆฌ๊ณ  ์ปดํŒŒ์ผํ•ด์„œ TestBean.class ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  C:\apache-tomcat-7.0.2\webapps\WEB-INF\classes\action ํด๋”์— ๋ณต์‚ฌํ•ด์„œ ๋„ฃ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  CLASSPATH ๋ณ€์ˆ˜์— ํด๋”๊ฒฝ๋กœ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

์ด์ œ main.jsp ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ  set/getProperty์•ก์…˜์„ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. 

<html> <head> <title>Using JavaBeans in JSP</title> </head> <body> <center> <h2>Using JavaBeans in JSP</h2> <jsp:useBean id="test" class="action.TestBean" /> <jsp:setProperty name="test" property="message" value="Hello JSP..." /> <p>Got message....</p> <jsp:getProperty name="test" property="message" /> </center> </body> </html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์™€์•ผ ๊ฒ ์ฃ . 

Using JavaBeans in JSP

Got message....
Hello JSP...

The <jsp:forward> Action

forward ์•ก์…˜์€ ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ์•ก์…˜์„ ์ข…๋ฃŒํ•˜๊ณ  ์š”์ฒญ์„ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋‚˜ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ํฌ์›Œ๋”ฉํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<jsp:forward page="Relative URL" />

์ด ์•ก์…˜์˜ ์†์„ฑ์€ ๋‹ฌ๋ž‘ ํ•œ๊ฐœ๋„ค์š”.

AttributeDescription
pageShould consist of a relative URL of another resource such as a static page, another JSP page, or a Java Servlet.

Example:

 date.jsp

<p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p>

main.jsp

<html> <head> <title>The include Action Example</title> </head> <body> <center> <h2>The include action Example</h2> <jsp:forward page="date.jsp" /> </center> </body> </html>

root ๋””๋ ‰ํ† ๋ฆฌ์— ์ด ํŒŒ์ผ๋“ค์„ ๋„ฃ๊ณ  main.jsp๋ฅผ ์š”์ฒญํ•ด๋ณด์„ธ์š”.

๋ฉ”์ธํŽ˜์ด์ง€์˜ ์ปจํ…ํŠธ๋ฅผ ์ œ์™ธํ•œ ํฌ์›Œ๋”ฉ๋œ ํŽ˜์ด์ง€์—์„œ์˜์ถœ๋ ฅ์€ ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Today's date: 12-Sep-2010 14:54:22

The <jsp:plugin> Action

plugin ์•ก์…˜์€ JSPํŽ˜์ด์ง€์— ์ž๋ฐ” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ € ํƒ€์ž…์„ ๊ฒฐ์ •ํ•˜๊ณ , ํ•„์š”ํ•œ๊ฒฝ์šฐ <object> ๋˜๋Š” <embed> ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

ํ•„์š”ํ•œ ํ”Œ๋Ÿฌ๊ทธ์ธ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์ž๋ฐ”์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ Java component๋Š” Applet์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ   JavaBean์ด ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.  

plugin action ์€ Java components๋ฅผ formattingํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณตํ†ต HTML ํƒœ๊ทธ์— ์ƒ์‘ํ•˜๋Š” ์—ฌ๋Ÿฌ ์†์„ฑ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.  <param> ์š”์†Œ ๋˜ํ•œ Applet ์ด๋‚˜ Bean์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

<jsp:plugin type="applet" codebase="dirname" code="MyApplet.class" width="60" height="80"> <jsp:param name="fontcolor" value="red" /> <jsp:param name="background" value="black" /> <jsp:fallback> Unable to initialize Java Plugin </jsp:fallback> </jsp:plugin>

์—ฌ๊ธฐ์„œ <fallback> ์š”์†Œ๋Š” ์ž๋ฐ”์ปดํฌ๋„ŒํŠธ ์‹คํ–‰์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. 

 <jsp:element>,  <jsp:attribute>,  <jsp:body> Action

<jsp:element>, <jsp:attribute> and <jsp:body> ์•ก์…˜๋“ค์€ XML ์š”์†Œ๋ฅผ ๋™์ ์œผ๋กœ ์ •์˜ํ•˜๊ธฐ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋™์ ์ด๋ผ๋Š” ๊ฒƒ์€ ์ปดํŒŒ์ผ ์‹œ๊ฐ€ ์•„๋‹ˆ๋ผ ๋Ÿฐํƒ€์ž„์— ์š”์ฒญ์— ์˜ํ•ด์„œ ๋งŒ๋“ค์–ด์ง„ ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 

์•„๋ž˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. 

<%@page language="java" contentType="text/html"%> <html xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page"> <head><title>Generate XML Element</title></head> <body> <jsp:element name="xmlElement"> <jsp:attribute name="xmlElementAttr"> Value for the attribute </jsp:attribute> <jsp:body> Body for XML element </jsp:body> </jsp:element> </body> </html>

์‹คํ–‰์‹œํ‚ค๋ฉด ๋™์ ์œผ๋กœ ์•„๋ž˜์™€๊ฐ™์€ HTMLํŒŒ์ผ์ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. 

<html xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page"> <head><title>Generate XML Element</title></head> <body> <xmlElement xmlElementAttr="Value for the attribute"> Body for XML element </xmlElement> </body> </html>

The <jsp:text> Action

<jsp:text> ์•ก์…˜์€ JSP ํŽ˜์ด์ง€์™€ ๋ฌธ์„œ๋ฅผ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ ํ…์ŠคํŠธ๋ฅผ ์“ฐ๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

<jsp:text>Template data</jsp:text>

๋ฐ”๋””์—๋Š” ๋‹ค๋ฅธ ์š”์†Œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ค๋กœ์ง€ ํ…์ŠคํŠธ์™€ EL ํ‘œํ˜„์‹๋งŒ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. XML files์—์„œ๋Š” ${whatever > 0}์™€ ๊ฐ™์€ ํ‘œํ˜„์‹์€ ์“ธ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค ๋น„๊ต์—ฐ์‚ฐ์ž > ๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋Œ€์‹  ${whatever gt 0} ์™€ ๊ฐ™์ด ์“ฐ๊ฑฐ๋‚˜ CDATA ๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

<jsp:text><![CDATA[<br>]]></jsp:text>

 XHTML์„ ์œ„ํ•œ DOCTYPE ์„ ์–ธ์„ ์ถ”๊ฐ€ํ•  ๋•Œ์—๋„ ์•„๋ž˜์ฒ˜๋Ÿผ  <jsp:text> ์š”์†Œ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

<jsp:text><![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">]]> </jsp:text> <head><title>jsp:text action</title></head> <body> <books><book><jsp:text> Welcome to JSP Programming </jsp:text></book></books> </body> </html>

 <jsp:text> ์•ก์…˜ ์—†์ด ํ•œ๋ฒˆ ์‹คํ–‰ํ•ด๋ณด์„ธ์š”.

 

 

 

 

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

์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” JSPํŽ˜์ด์ง€๊ฐ€ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜๋  ๋•Œ, ๊ทธ ํƒœ๊ทธ๋Š” tag handler๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฐ์ฒด์˜ operation์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ JSPํŽ˜์ด์ง€์˜ ์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋  ๋•Œ ์›น์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ทธ opreation์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋Š” ๊ฑฐ์ฃ .

JSP tag extensions ๋Š” JSP์— ์ง์ ‘ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. JSP 2.0 specification์—๋Š” ์ด๋Ÿฐ ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์“ฐ๊ธฐ์œ„ํ•œ Simple Tag Handlers ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

customer tag๋ฅผ ์“ฐ๋ ค๋ฉด ๋‹จ์ˆœํžˆ SimpleTagSupport ๋ฅผ ์ƒ์†ํ•˜๊ณ   doTag()๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.  

 

Create "Hello" Tag ( ์‚ฌ์šฉ์ž ์ •์˜ ํƒœ๊ทธ ์ƒ์„ฑํ•˜๊ธฐ )

์•„๋ž˜์ฒ˜๋Ÿผ  <ex:Hello> ๋ฅผ ์“ฐ๊ณ ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. body ํƒœ๊ทธ๋„ ์—†์ด ๋ง์ž…๋‹ˆ๋‹ค. 

<ex:Hello />

custom JSP tag๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ํƒœ๊ทธ ํ•ธ๋“ค๋Ÿฌ Java class ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

์•„๋ž˜์ฒ˜๋Ÿผ ์ž๋ฐ” ์ฝ”๋”ฉ์„ ํ•ด๋ด…์‹œ๋‹ค. 

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!"); } }

์œ„ ์ฝ”๋“œ๋Š” doTag() ๋ฉ”์†Œ๋“œ์—์„œ getJspContext() method๋ฅผ ์ด์šฉํ•ด์„œ ํ˜„์žฌ JspContext object๋ฅผ ์–ป์–ด์˜ค๊ณ  "Hello Custom Tag!"๋ฉ”์‹œ์ง€๋ฅผ  JspWriter ๊ฐ์ฒด๋กœ ๋ณด๋‚ด๋Š” ๊ธฐ๋Šฅ์„ ๋„ฃ์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์ด์ œ ์ด ์†Œ์ŠคํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•ด์„œ classํŒŒ์ผ๋กœ ๋งŒ๋“ค๊ณ  ์ด ํด๋ž˜์Šค ํŒŒ์ผ์„ ํ™˜๊ฒฝ๋ณ€์ˆ˜ CLASSPATH์— ์ง€์ •๋˜์–ด์žˆ๋Š” ๊ณณ์— ๋ณต์‚ฌํ•ด์„œ ๋„ฃ์–ด์ฃผ์„ธ์š”. ๋งˆ์ง€๋ง‰์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ํƒœ๊ทธ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„์น˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 <Tomcat-Installation-Directory>webapps\ROOT\WEB-INF\custom.tld

<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>

์ด์ œ JSPํŒŒ์ผ์—์„œ ์ด Hello ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค.  

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello/> </body> </html>

์ด์ œ ๋งŒ๋“ค์–ด์ง„ JSPํŒŒ์ผ์„ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•ด ๋ณด์„ธ์š”. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

Hello Custom Tag!


Accessing the Tag Body ( ํƒœ๊ทธ ๋ฐ”๋””์— ์ ‘๊ทผํ•˜๊ธฐ )

์ปค์Šคํ…€ ํƒœ๊ทธ๋„ ์ผ๋ฐ˜ HTMLํƒœ๊ทธ์ฒ˜๋Ÿผ ์‹œ์ž‘ํƒœ๊ทธ์™€ ์ข…๋ฃŒํƒœ๊ทธ ์‚ฌ์ด์— ๋ฐ”๋””๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<ex:Hello> This is message body </ex:Hello>

์œ„์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ž๋ฐ”์†Œ์Šค๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. 

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } }

์—ฌ๊ธฐ์„œ๋Š” StringWriter๋กœ ์ฝ์–ด๋“ค์ธ ํ›„์— ์ฝ์–ด๋“ค์ธ ๊ฐ’์„ JspWriter์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

TLD ํŒŒ์ผ๋„ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>

JSPํŒŒ์ผ์€ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello> This is message body </ex:Hello> </body> </html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

This is message body


Custom Tag Attributes ( ์ปค์Šคํ…€ ํƒœ๊ทธ์˜ ์†์„ฑ )

 custom tags๋Š” ์†์„ฑ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.  ์†์„ฑ๊ฐ’์„ ๋ฐ›์œผ๋ ค๋ฉด custom tag class๋Š” setter methods๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. JavaBean setter methods์™€ ๋™์ผํ•˜๊ฒŒ ๋ง์ด์ฃ . 

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { private String message; public void setMessage(String msg) { this.message = msg; } StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { if (message != null) { /* Use message from attribute */ JspWriter out = getJspContext().getOut(); out.println( message ); } else { /* use message from the body */ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } } }

์†์„ฑ๋ช…์€ "message"์ด๊ณ  ์„ธํ„ฐ๋ฉ”์†Œ๋“œ๋Š” ๋‹น์—ฐํžˆ setMessage()๊ฐ€ ๋˜๊ฒ ์ฃ . ์ด์ œ ์ด ์†์„ฑ์„ TLD file์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. <attribute> ์š”์†Œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ง์ด์ฃ .

<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>message</name> </attribute> </tag> </taglib>

JSPํŒŒ์ผ์— ๋ฉ”์‹œ์ง€ ์†์„ฑ์„ ์ด์šฉํ•œ ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. 

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello message="This is custom tag" /> </body> </html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

This is custom tag

 

 

์†์„ฑ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ”„๋กœํผํ‹ฐ๋“ค์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

PropertyPurpose
nameThe name element defines the name of an attribute. Each attribute name must be unique for a particular tag.
requiredThis specifies if this attribute is required or optional. It would be false for optional.
rtexprvalueDeclares if a runtime expression value for a tag attribute is valid
typeDefines the Java class-type of this attribute. By default it is assumed as String
descriptionInformational description can be provided.
fragmentDeclares if this attribute value should be treated as a JspFragment.

 

์†์„ฑ ๊ด€๋ จ ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

.....
    <attribute>
      <name>attribute_name</name>
      <required>false</required>
      <type>java.util.Date</type>
      <fragment>false</fragment>
    </attribute>
.....

๋‘ ๊ฐœ ์ด์ƒ์˜ ์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด TLDํŒŒ์ผ์— ์•„๋ž˜์ฒ˜๋Ÿผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

..... <attribute> <name>attribute_name1</name> <required>false</required> <type>java.util.Boolean</type> <fragment>false</fragment> </attribute> <attribute> <name>attribute_name2</name> <required>true</required> <type>java.util.Date</type> </attribute>

.....





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


JSP directives ๋Š” container์— ๋ฐฉํ–ฅ์ด๋‚˜ ๋ช…๋ น์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ง์ด์ฃ 

JSP directive๋Š” servlet class ์ „๋ฐ˜์— ๊ฑธ์ณ์„œ ์˜ํ–ฅ์„ ๋ฏธ์นฉ๋‹ˆ๋‹ค.  

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

 

<%@ directive attribute="value" %>

Directives ๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ์†์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์†์„ฑ๊ณผ ์†์„ฑ ์‚ฌ์ด๋Š” ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„์„ ํ•˜๊ณ  key-value ํŽ˜์–ด๋กœ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 @ ์™€ directive๋ช… ์‚ฌ์ด์˜ ๋นˆ์นธ ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰ ์†์„ฑ๊ณผ %> ์‚ฌ์ด์˜ ๋นˆ์นธ์€ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ์žˆ์–ด๋„ ๋˜๊ณ  ์—†์–ด๋„ ๋œ๋‹ค๋Š” ๊ฑฐ์ฃ . 

 

๋””๋ ‰ํ‹ฐ๋ธŒ ํƒœ๊ทธ๋Š” ์ด ์„ธ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.  

DirectiveDescription
<%@ page ... %>ํŽ˜์ด์ง€ ์ข…์†์ ์ธ ์†์„ฑ๋“ค์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. scripting language, error page, and buffering requirements ๊ฐ™์€ ๊ฒƒ๋“ค์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
<%@ include ... %>๋กœ๋“œ๋  ๋•Œ ๋™์ ์œผ๋กœ ํŒŒ์ผ์„ includeํ•ฉ๋‹ˆ๋‹ค.
<%@ taglib ... %>ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉ๋  ์‚ฌ์šฉ์ž์ •์˜ ์•ก์…˜์„ ํฌํ•จํ•˜๋Š” ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. 


The page Directive

page directive ๋Š” ํ˜„์žฌ JSP ํŽ˜์ด์ง€์™€ ๊ด€๋ จ๋œ ๋ช…๋ น์–ด๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์— ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํŽ˜์ด์ง€ ๋””๋ ‰ํ‹ฐ๋ธŒ๋Š” JSPํŽ˜์ด์ง€ ์•„๋ฌด๋ฐ๋‚˜ ๋„ฃ์–ด๋„ ๊ด€๊ณ„๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ JSPํŽ˜์ด์ง€์˜ ๊ฐ€์žฅ ์œ„์ชฝ์— ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<%@ page attribute="value" %>

XMLํ˜•์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<jsp:directive.page attribute="value" />

Attributes ( ์†์„ฑ )

ํŽ˜์ด์ง€ ๋””๋ ‰ํ‹ฐ๋ธŒ์™€ ๊ด€๋ จ๋œ ์†์„ฑ๊ณผ ๋ชฉ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

AttributePurpose
bufferSpecifies a buffering model for the output stream.
autoFlushControls the behavior of the servlet output buffer.
contentTypeDefines the character encoding scheme.
errorPageDefines the URL of another JSP that reports on Java unchecked runtime exceptions.
isErrorPageIndicates if this JSP page is a URL specified by another JSP page's errorPage attribute.
extendsSpecifies a superclass that the generated servlet must extend
importSpecifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes.
infoDefines a string that can be accessed with the servlet's getServletInfo() method.
isThreadSafeDefines the threading model for the generated servlet.
languageDefines the programming language used in the JSP page.
sessionSpecifies whether or not the JSP page participates in HTTP sessions
isELIgnoredSpecifies whether or not EL expression within the JSP page will be ignored.
isScriptingEnabledDetermines if scripting elements are allowed for use.

์†์„ฑ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด๊ณณ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 


The include Directive:

include directive๋Š” ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ๋™์ ์œผ๋กœ ๋‹ค๋ฅธ ํŒŒ์ผ์„ include์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ๋งํ•˜๋ฉด ๋‹ค๋ฅธ ํŽ˜์ด์ง€์— ๋”ฐ๋กœ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด๋†“๊ณ  ์ด๊ฒƒ์„ ํ•„์š”๋กœ ํ•˜๋Š” JSPํŽ˜์ด์ง€์—์„œ includeํ•˜์—ฌ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์žˆ๋Š”๊ฒ๋‹ˆ๋‹ค. 

์—ญ์‹œ ์œ„์น˜๋Š” JSPํŽ˜์ด์ง€ ๋‚ด ์•„๋ฌด๊ณณ์—๋‚˜ ๊ฐ€์ ธ๋‹ค ๋†“์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

<%@ include file="relative url" >

file์†์„ฑ์˜ value๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์€ ํŒŒ์ผ์˜ ์ƒ๋Œ€๊ฒฝ๋กœ๋ผ๋Š” ์ ์„ ์ฃผ์˜ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.  

 

XMLํ˜•์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<jsp:directive.include file="relative url" />

Example:

์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”? ROOT ๋””๋ ‰ํ† ๋ฆฌ์— ์•„๋ž˜์ฒ˜๋Ÿผ header, footer, main JSPํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.

 

header.jsp

<%! int pageCount = 0; void addCount() { pageCount++; } %> <% addCount(); %> <html> <head> <title>The include Directive Example</title> </head> <body> <center> <h2>The include Directive Example</h2> <p>This site has been visited <%= pageCount %> times.</p> </center> <br/><br/>

footer.jsp

<br/><br/> <center> <p>Copyright ยฉ 2010</p> </center> </body> </html>

main.jsp

<%@ include file="header.jsp" %> <center> <p>Thanks for visiting my page.</p> </center> <%@ include file="footer.jsp" %>

๊ทธ๋ฆฌ๊ณ  main.jsp๋ฅผ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ๊ฒƒ์ž…๋‹ˆ๋‹ค. main.jsp์—์„œ๋Š” include๋กœ ๋‹ค๋ฅธ jspํŒŒ์ผ๋งŒ ์ถ”๊ฐ€๋ฅผ ํ–ˆ์„ ๋ฟ์ธ๋ฐ ๋ˆˆ์— ๋ณด์ด๋Š”๊ฒŒ ๋งŽ์ฃ ? include๋œ jspํŒŒ์ผ๋“ค์—์„œ ๋ฟŒ๋ฆฌ๋Š” ๊ฒฐ๊ณผ๊ฐ€ main์— ๋ชจ๋‘ ํ•ฉ์ณ์ ธ์„œ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

The include Directive Example

This site has been visited 1 times.



Thanks for visiting my page.



Copyright ยฉ 2010

 

์ƒˆ๋กœ๊ณ ์นจํ•  ๋•Œ๋งˆ๋‹ค ๋ฐฉ๋ฌธ ์นด์šดํŠธ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š”๊ฒŒ ๋ณด์ด์‹œ๋‚˜์š”?? ^___^ 

 


The taglib Directive:

ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ํƒœ๊ทธ์ž…๋‹ˆ๋‹ค.  

taglib directive๋Š” JSPํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉ์ž ์ •์˜ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<%@ taglib uri="uri" prefix="prefixOfTag" >

XMLํ˜•์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<jsp:directive.taglib uri="uri" prefix="prefixOfTag" />

Example:

์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ด…์‹œ๋‹ค. 

 custlib tag library ๊ฐ€ hello ํƒœ๊ทธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. hello tag ๋ฅผ mytagโ€‹๋ผ๋Š” prefix์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ(  <mytag:hello> ) ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<%@ taglib uri="http://www.example.com/custlib" prefix="mytag" %>
<html>
<body>
<mytag:hello/>
</body>
</html>

 <mytag:hello> ๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ hello ํƒœ๊ทธ์— ๋ฏธ๋ฆฌ ์ •์˜๋˜์–ด์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์จ๋จน์„ ์ˆ˜ ์žˆ๋Š”๊ฑฐ์ฃ .

 

๊ทธ๋Ÿผ hello ํƒœ๊ทธ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ •์˜๋˜์–ด์žˆ๋Š”์ง€ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”?

 

์ง€๊ธˆ ๋ง๊ณ  ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ๋ณด๋„๋ก ํ•˜์ฃ . ์ƒ๊ฐ๋ณด๋‹ค ๋‚ด์šฉ์ด ๋งŽ๊ฑฐ๋“ ์š” ใ…‹ใ…‹ ์ฐธ๊ณ ๋กœ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋œ hello ํƒœ๊ทธ๋ฅผ ์ปค์Šคํ…€ ํƒœ๊ทธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

๊ทธ๋Ÿผ ์ข‹์€ ํ•˜๋ฃจ ๋˜์„ธ์š” ^___^

 

 

 

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

 JSP ์ƒ๋ช… ์ฃผ๊ธฐ๋ผ ํ•จ์€ JSP ์ƒ์„ฑ์—์„œ๋ถ€ํ„ฐ ์†Œ๋ฉธ๊นŒ์ง€์˜ ๊ณผ์ •์„ ๋งํ•˜๋Š”๋ฐ ์ด๋Š” ์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜๊ณ  ์ถ”๊ฐ€์ ์œผ๋กœ JSP๋ฅผ ์ปดํŒŒ์ผ ํ•˜๋Š” ๊ณผ์ •๋งŒ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

ํฐ ๊ณผ์ •๋งŒ ๊ฐ„์ถ”๋ฆฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

  • Compilation ( ์ปดํŒŒ์ผ ) 

  • Initialization ( ์ดˆ๊ธฐํ™” ) 

  • Execution ( ์‹คํ–‰ ) 

  • Cleanup ( ๋’ท์ •๋ฆฌ ) 

๊ทธ๋ฆผ์œผ๋กœ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐ์—์„œ ์ปดํŒŒ์ผ๋งŒ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด์ฃ . ์•„๋ž˜ ๊ทธ๋ฆผ์—๋Š” ์ปดํŒŒ์ผ ๊ณผ์ •์€ ์ƒ๋žต๋˜์–ด์žˆ๋„ค์š”. 

JSP Compilation:

์›น๋ธŒ๋ผ์šฐ์ €๊ฐ€ JSP๋ฅผ ์š”์ฒญํ•˜๋ฉด JSP engine ์€ ์šฐ์„  ์š”์ฒญํ•œ JSPํŒŒ์ผ์ด ์ปดํŒŒ์ผ์ด ํ•„์š”ํ•œ์ง€๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•œ๋ฒˆ๋„ ์ปดํŒŒ์ผ ๋œ์ ์ด ์—†๊ฑฐ๋‚˜ ์ˆ˜์ •๋œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด JSP engine ์ด ์ปดํŒŒ์ผ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.  

์ปดํŒŒ์ผ ๊ณผ์ •์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • Parsing the JSP. ( JSP ํŒŒ์ผ ํŒŒ์‹ฑ ) 

  • Turning the JSP into a servlet. ( JSP๋ฅผ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜ ) 

  • Compiling the servlet. ( ์„œ๋ธ”๋ฆฟ ์ปดํŒŒ์ผ ) 

JSP Initialization ( ์ดˆ๊ธฐํ™” )

์ปจํ…Œ์ด๋„ˆ๊ฐ€ JSP๋ฅผ ๋กœ๋“œํ•  ๋•Œ jspInit() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์ „์— ๋ง์ด์ฃ . ๋งŒ์•ฝ ํŠน์ • JSP์—์„œ ํŠน์ˆ˜ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ์ด jspInit() ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

public void jspInit(){ // Initialization code... }

์ผ๋ฐ˜์ ์œผ๋กœ ์ดˆ๊ธฐํ™”๋Š” ์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™”์ฒ˜๋Ÿผ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค, ์ด ์ดˆ๊ธฐํ™” ๊ณผ์ •์—์„œ ๊ฐœ๋ฐœ์ž๋“ค์€ ๋ณดํ†ต ๋ฐ์ดํƒ€ ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜์„ ๋งบ๊ณ  ํŒŒ์ผ์„ ์—ด๊ณ  ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์„ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•˜๋„ค์š”.  

JSP Execution ( ์‹คํ–‰ )

๋ธŒ๋ผ์šฐ์ €๊ฐ€ JSP ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•˜๊ณ  ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋˜๊ณ  ์ดˆ๊ธฐํ™”๊ฐ€ ๋๋‹ค๋ฉด JSP engine์€ _jspService() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 _jspService() ๋ฉ”์†Œ๋“œ๋Š” HttpServletRequest์™€ HttpServletResponse ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

void _jspService(HttpServletRequest request, HttpServletResponse response) { // Service handling code... }

JSP์˜ _jspService()๋ฉ”์†Œ๋“œ๋Š” ์š”์ฒญ์ด ์˜ฌ๋•Œ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ƒ์„ฑํ•˜๋ฉฐ ๋ชจ๋“  ์ข…๋ฅ˜์˜ HttpRequest๋ฐฉ๋ฒ• ( ie. GET, POST, DELETE, PUT, etc.) ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 

JSP Cleanup ( ๋’ท์ •๋ฆฌ )

 jspDestroy() ๋ฉ”์†Œ๋“œ๋Š” ์„œ๋ธ”๋ฆฟ์„ ์†Œ๋ฉธ์‹œํ‚ค๋Š” ๋ฉ”์†Œ๋“œ์™€ ๋™์ผํ•œ ๊ฐœ๋…์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•ด์„œ ํ•„์š”ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํƒ€ ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜์„ ๋Š๋Š”๋‹ค๋˜์ง€ ํŒŒ์ผ์„ ๋‹ซ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ผ๋“ค์„ ๋ง์ด์ฃ .

 

 jspDestroy() ๋ฉ”์†Œ๋“œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

public void jspDestroy() { // Your cleanup code goes here. }

 

 

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

 



์ž, ์˜ค๋Š˜์€ ์ปจํŠธ๋กค ํ”Œ๋กœ์šฐ ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  

์ปจํŠธ๋กค ํ”Œ๋กœ์šฐ ๋ฌธ๋ฒ•์ด๋ผํ•จ์€ if์™€ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ๊ณผ for, while๊ณผ ๊ฐ™์€ ๋ฃจํ”„๋ฌธ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. 

1. If-Else

์šฐ์„  if...else ๋ฌธ์— ๋Œ€ํ•ด์„œ ๋ณด๋„๋ก ํ•˜์ฃ .

<%! int day = 3; %>
<html> 
<head><title>IF...ELSE Example</title></head> 
<body>
<% if (day == 1 | day == 7) { %>
      <p> Today is weekend</p>
<% } else { %>
      <p> Today is not weekend</p>
<% } %>
</body> 
</html>

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

Today is not weekend

์™œ ์ด๋ ‡๊ฒŒ ๋‚˜์˜ค๋Š”๊ฑด์ง€ ํ•œ์ค„ ํ•œ์ค„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

<%! int day = 3; %>

day๋ผ๋Š” int ํƒ€์ž… ๋ณ€์ˆ˜์— 3์ด๋ผ๋Š” ๊ฐ’์„ ํ• ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  body ํƒœ๊ทธ ๋‚ด์—์„œ if-else ๋ฌธ์„ ์ด์šฉํ•˜์˜€๋Š”๋ฐ day ๊ฐ’์ด 1 (์ผ์š”์ผ) ์ด๊ฑฐ๋‚˜ 7 (ํ† ์š”์ผ)์ด๋ฉด "Today is weekend"๋ฅผ ์ถœ๋ ฅํ–ˆ๊ฒ ์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” day ๊ฐ’์— 3์„ ํ• ๋‹นํ•ด์ฃผ์—ˆ์œผ๋‹ˆ "Today is not weekend"๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ if-else ๋ฌธ ๋‚ด์—์„œ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” ํƒœ๊ทธ ๋ถ€๋ถ„์€ <% %> ์•ˆ์— ๊ฐ์‹ธ์ง€ ์•Š์•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

2. Switch - Case

์ด์ œ switch - case ๋ฌธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ฃ . 

<%! int day = 3; %> 
<html>
	<head><title>SWITCH...CASE Example</title></head>
    <body> 
    	<% 
        	switch(day) { 
            	case 0: 
                	out.println("It\'s Sunday."); 
                	break; 
                case 1: 
                	out.println("It\'s Monday."); 
                    break; 
                case 2: 
                	out.println("It\'s Tuesday."); 
                    break; 
                case 3: 
                	out.println("It\'s Wednesday."); 
                	break;
                case 4:
                	out.println("It\'s Thursday.");
                    break;
                case 5:
                	out.println("It\'s Friday.");
                    break;
                default:
                	out.println("It's Saturday.");
            } 
        %>
    </body>
</html>

 

์—ญ์‹œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

It's Wednesday.

3. For - Loop

์ด๋ฒˆ์—๋Š” Loop ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

<%! int fontSize; %> 
<html> 
	<head><title>FOR LOOP Example</title></head> 
    <body> 
    	<%for ( fontSize = 1; fontSize <= 3; fontSize++){ %> 
        <p style="font-size:<%= fontSize %>rem"> JSP Tutorial </p> <%}%> 
	</body>
</html>

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

JSP Tutorial

JSP Tutorial

JSP Tutorial

 

4. While - Loop

3๋ฒˆ์—์„œ for ๋ฃจํ”„๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ์„ ์ด๋ฒˆ์—๋Š” while๋ฃจํ”„๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

<%! int fontSize; %> 
<html> 
	<head><title>WHILE LOOP Example</title></head> 
    <body> 
    	<%while ( fontSize <= 3){ %> 
            <p style="font-size:<%= fontSize %>rem"> JSP Tutorial </p>
            <%fontSize++;%>
        <%}%> 
	</body> 
</html>

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

JSP Tutorial

JSP Tutorial

JSP Tutorial

JSP Tutorial

 

์ž ์ด์ œ ์–ด๋Š์ •๋„ ๊ฐ์ด ์žกํžˆ์‹œ์ฃ ?

๊ทธ๋ƒฅ ์ž๋ฐ” ๋ฌธ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์˜ฎ๊ฒจ๋†“๊ธฐ๋งŒ ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค.

JSP ์ •๋ง ๋ณ„๊ฑฐ ์•„๋‹ˆ๋„ค์š”~ ใ…Žใ…Ž

while ๋ฃจํ”„๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์„ ์ด๋ฒˆ์—๋Š” do - while์„ ์ด์šฉํ•ด์„œ ํ•œ๋ฒˆ ํ•ด๋ณด์„ธ์š”~ ์ˆ™์ œ์ž…๋‹ˆ๋‹ค~ ใ…‹ใ…‹

 

 

 

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

๐Ÿ’ป 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 

๐Ÿ’ป Programming/JSP

[JSP] JSP๋ž€?

JSP๋ž€ ๋ฌด์—‡์ธ๊ฐ€ ?

JSP๋Š” Java Server Pages์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค. ์ž๋ฐ” ์„œ๋ฒ„ ํŽ˜์ด์ง€๋Š” ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ๋•Œ HTMLํŽ˜์ด์ง€ ๋‚ด์— ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ๋™์ ์ธ ์›นํŽ˜์ด์ง€๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต <% ์ž๋ฐ”์ฝ”๋“œ %> ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉ์ด ๋˜์ฃ .

 

์™œ JSP๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€ ?

์ž๋ฐ” ์„œ๋ฒ„ ํŽ˜์ด์ง€๋Š” ์ข…์ข… ์˜ˆ์ „์˜ CGI( Common Gateway Interface )์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜์ง€๋งŒ CGI์™€ ๋น„๊ตํ•ด์„œ ์ข€ ๋” ๋‚˜์€ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ์งธ, ์„ฑ๋Šฅ์ด ๊ต‰์žฅํžˆ ์ข‹์Šต๋‹ˆ๋‹ค. CGIํŒŒ์ผ์ฒ˜๋Ÿผ ๋ณ„๋„์˜ ํŒŒ์ผ์ด ํ•„์š”ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ HTMLํŽ˜์ด์ง€ ๋‚ด์— ์‚ฝ์ž…๋˜์–ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋‘˜์งธ, CGI / Perl ์˜ ๊ฒฝ์šฐ ํŽ˜์ด์ง€ ์š”์ฒญ์‹œ๋งˆ๋‹ค ์„œ๋ฒ„์ชฝ์—์„œ interpreter์™€ ๋Œ€์ƒ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋กœ๋“œํ•ด์•ผํ•˜์ง€๋งŒ JSP๋Š” ์ฒ˜๋ฆฌ๋˜๊ธฐ์ „์— ํ•ญ์ƒ ์ปดํŒŒ์ผ์ด ๋ฉ๋‹ˆ๋‹ค.

์…‹์งธ, JSP๋Š” ์ž๋ฐ” ์„œ๋ธ”๋ฆฟ API์œ„์— ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„œ๋ธ”๋ฆฟ์ฒ˜๋Ÿผ JSP ๋˜ํ•œ JDBC, JNDI, EJB, JAXP ๋“ฑ๋“ฑ ๋ชจ๋“  ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ์ž๋ฐ” API๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋„ท์งธ, JSP๋Š” ์ž๋ฐ” ์„œ๋ธ”๋ฆฟ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด ์ง€์›ํ•˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋ธ”๋ฆฟ๊ณผ ๊ฒฐํ•ฉ๋˜์„œ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


JSP ์ฒ˜๋ฆฌ ๊ณผ์ •

์•„๋ž˜ ๊ณผ์ •์€ ์›น์„œ๋ฒ„๊ฐ€ JSP๋ฅผ ์ด์šฉํ•ด์„œ ์–ด๋–ป๊ฒŒ ์›นํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

1. ์‚ฌ์šฉ์ž๊ฐ€ ์›นํŽ˜์ด์ง€์—์„œ ์š”์ฒญ์„ ํ•ฉ๋‹ˆ๋‹ค.

2. ์›น์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ๋ฐ›๊ณ  JSPํŽ˜์ด์ง€๋ฅผ ์œ„ํ•œ ์š”์ฒญ์ด๋ฉด JSP ์—”์ง„์œผ๋กœ ์š”์ฒญ์„ ํฌ์›Œ๋”ฉํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ URL์ด๋‚˜ JSPํŽ˜์ด์ง€๋ฅผ ํ†ตํ•ด์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

3. ์š”์ฒญ์„ ๋ฐ›์€ JSP ์—”์ง„์ด JSPํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์„œ๋ธ”๋ฆฟ ์ปจํ…ํŠธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ณ€ํ™˜์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ํ…œํ”Œ๋ฆฟ ํ…์ŠคํŠธ๋Š” ๋ชจ๋‘ println()๋ฌธ์œผ๋กœ ๋ณ€ํ™˜๋˜๊ณ  ๋ชจ๋“  JSP ์š”์†Œ๋“ค์€ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋˜๋Š” ๊ฑฐ์ฃ .

4. JSP ์—”์ง„์ด ์„œ๋ธ”๋ฆฟ์„ ์ปดํŒŒ์ผํ•˜์—ฌ ์‹คํ–‰๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค ํŒŒ์ผ๋กœ ๋ณ€ํ™˜์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  2๋ฒˆ์—์„œ ๋ฐ›์•˜๋˜ ์š”์ฒญ์„ ์„œ๋ธ”๋ฆฟ ์—”์ง„์œผ๋กœ ๋„˜๊น๋‹ˆ๋‹ค.

5. ์š”์ฒญ์„ ๋ฐ›์€ ์„œ๋ธ”๋ฆฟ ์—”์ง„( ์›น์„œ๋ฒ„์˜ ์ผ๋ถ€๋ถ„ )์€ ํด๋ž˜์Šค ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์„œ๋ธ”๋ฆฟ ์—”์ง„์€ HTMLํ˜•ํƒœ์˜ output์„ ์›น์„œ๋ฒ„( Http Response )๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

6. ์›น์„œ๋ฒ„๊ฐ€ Http Response๋ฅผ ์›น๋ธŒ๋ผ์šฐ์ €๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

7. ๋ธŒ๋ผ์šฐ์ €๋Š” HTML Response์— ์žˆ๋Š” ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ HTML ํŽ˜์ด์ง€๋ฅผ ํ•ด์„ํ•˜๊ณ  ํ™”๋ฉด์— ๋ฟŒ๋ ค์ค๋‹ˆ๋‹ค.



์œ„ ๊ณผ์ •์€ ์•„๋ž˜ ๊ทธ๋ฆผ์„ ๋ณด์‹œ๋ฉด ์ดํ•ด๊ฐ€ ๋” ์‰ฌ์šธ๊ฒ๋‹ˆ๋‹ค.

 

 

์ž, ์–ด๋Š์ •๋„ ์ดํ•ด๊ฐ€ ๋˜์‹œ๋‚˜์š”?

๋‹ค์Œ ์‹œ๊ฐ„์—๋Š” JSP์‹ค์Šต์„ ์‹œ์ž‘ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

JSP ํ™˜๊ฒฝ๊ตฌ์ถ•์€ ๋‹จ์ˆœํžˆ ์›น์„œ๋ฒ„๋ฅผ ์„ค์น˜ํ•˜๊ณ , ์ž๋ฐ”๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋Š” ํŠน๋ณ„ํ•œ ๊ฒƒ์ด ์—†์œผ๋ฏ€๋กœ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

 

 

 

 

 

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