๐Ÿ’ป Programming (356)

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

Pipes


  • Conceptually, a pipe is a connection between two processes, such that the standard output from one process becomes the standard input of the other process
  • It is possible to have a series of processes arranged in a a pipeline, with a pipe between each pair of processes in the series.
  • Implementation: A pipe can be implemented as a 10k buffer in main memory with 2 pointers, one for the FROM process and one for TO process
  • One process cannot read from the buffer until another has written to it
  • The UNIX command-line interpreter (e.g., csh) provide a pipe facility.
    • % prog | more
    • This command runs the prog1 program and send its output to the more program.

Pipe System Call

  • pipe() is a system call that facilitates inter-process communication. It opens a pipe, which is an area of main memory that is treated as a "virtual file". The pipe can be used by the creating process, as well as all its child processes, for reading and writing.
  • One process can write to this "virtual file" or pipe and another related process can read from it.
  • If a process tries to read before something is written to the pipe, the process is suspended until something is written.
  • The pipe system call finds the first two available positions in the process's open file table and allocates them for the read and write ends of the pipe. Recall that the open system call allocates only one position in the open file table.

Syntax in a C/C++ program:

 #include <unistd.h>
 int pip[2];
 (void) pipe(pip); 

With error checking:

 #include <unistd.h>
 int pip[2];
 int result;
 result = pipe(pip); 
 if (result == -1)
 {
  perror("pipe");
  exit(1);
 }

Programming notes:

  • The pipe() system call is passed a pointer to the beginning of an array of two integers.
  • It appears in C/C++ as if pipe() is passed the name of the array without any brackets.
  • The system call places two integers into this array. These integers are the file descriptors of the first two available locations in the open file table.
  • pip[0] - the read end of the pipe - is a file descriptor used to read from the pipe
  • pip[1] - the write end of the pipe - is a file descriptor used to write to the pipe

The buffer size for the pipe is fixed. Once it is full all further writes are blocked. Pipes work on a first in first out basis.



source from: http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html


( Tested on Linux/Cent OS 6.2 64 bit )

 

Let's say we have main function below....

 

int main(){
 First *fp = new First[10];          // creating array of First object
 delete [] fp;
return 0;
}

 

{If we have one of First classes below }// ??bytes of heap memory allocated


case 1:
class First{ double db; }// 8bytes * 10
class First{ int size;  }// 4bytes * 10
class First{ int *pt;  }// 8bytes * 10

 

case 2:
class First{ int size;
  int *pt;  }// 16 bytes * 10


class First{ int *pt;
  int size;  }// 16 bytes * 10

 

class First{ int size;
  double db; }// 16 bytes * 10


class First{ double db;
  int size;  }// 16 bytes * 10

 

case 3: 
class First{ int size;
  int size2;
  double db; }// 16 bytes * 10



 



 

class First{ double db;
  int size;
  int size2; }// 16 bytes * 10

 

 

 

class First{ int size;  
  double db;
  int size2; }// 24 bytes * 10

 

 

 

case 4:
class First{ int size;
  double db;
  int size2;
  char ch1; }// 24 bytes * 10

 





class First{ int size;
  double db;
  int size2;
  char ch1;
  char ch2;
  char ch3;
  char ch4; }// 24 bytes * 10

 





class First{ int size;
  double db;
  int size2;
  char ch1;
  char ch2;
  char ch3;
  char ch4; 
  char ch5; }// 32 bytes * 10


 

๋žœ๋ค ์ˆซ์ž๋“ค, ์ถœ์ฒ˜ : https://openclipart.org/detail/254729/random-numbers-cloud

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Java๋กœ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ํš๋“ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

 

์ž๋ฐ”์—๋Š” Math ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

ํ•ด๋‹น ํด๋ž˜์Šค์—๋Š” ๋‹ค์–‘ํ•œ ์ˆ˜ํ•™์ ์ธ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” static ๋ฉ”์„œ๋“œ๋“ค์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ๋Š”๋ฐ,

๊ทธ ์ค‘์— ํ•˜๋‚˜๊ฐ€ random() ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

์ด ๋ฉ”์„œ๋“œ๋Š” 0์—์„œ 1์‚ฌ์ด์˜ ๋žœ๋คํ•œ double ํƒ€์ž… ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

javadoc ๋ฌธ์„œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

 

random

public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

 

์ฆ‰, 0.0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  1.0๋ณด๋‹ค ์ž‘์€ double(์‹ค์ˆ˜)ํ˜• ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

 

์ด๊ฒŒ 0~1 ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์–ด๋–ป๊ฒŒ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์„๊นŒ์š”?? 

 

์˜ˆ๋ฅผ ๋“ค์–ด 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  100๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

int max = 100;

int randomInt = (int) (Math.random() * max);

 

์œ„ ์ฝ”๋“œ์—์„œ (int) (Math.random() * max) ๋ถ€๋ถ„์€ 0 <= ๊ฒฐ๊ณผ๊ฐ’ < max (100) ์˜ ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Math.random()์˜ ์ตœ์†Œ๊ฐ’์ด 0์ด๊ณ  ์ตœ๋Œ€๊ฐ’์ด 1.0๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . 

 

์ด๋ฒˆ์—๋Š” 100์„ ํฌํ•จํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

int max = 100;

int randomInt = (int) Math.round(Math.random() * max);

 

Math.round() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด 0<= ๊ฒฐ๊ณผ๊ฐ’ <= max (100) ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ 0๊ณผ 100์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์€ 1~99๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ๋ณด๋‹ค 50% ์ ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

0์ด ๋‚˜์˜ค๋ ค๋ฉด Math.round()์˜ ๊ฐ’์ด 0~0.004999...๊ฐ€ ๋‚˜์™€์•ผ ํ•˜๊ณ 

100์ด ๋‚˜์˜ค๋ ค๋ฉด 0.95~0.9999...๊ฐ€ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ด ๊ฒฝ์šฐ 0.5%์˜ ํ™•๋ฅ ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

1์ด ๋‚˜์˜ค๋ ค๋ฉด 0.005~0.014999..๊ฐ€ ๋‚˜์˜ค๋ฉด ๋˜๊ณ , 2, 3, 4..., 99 ๊นŒ์ง€๋Š” ๋ชจ๋‘ ๋™์ผํ•œ ํ™•๋ฅ ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ด ๊ฒฝ์šฐ 1%์˜ ํ™•๋ฅ ์„ ๊ฐ–๊ฒŒ ๋˜์ฃ .

 

๋”ฐ๋ผ์„œ ๊ณ ๋ฅด๊ฒŒ ๋ถ„ํฌํ•˜๋Š” 0~100 ์‚ฌ์ด์˜ ๊ฐ’(๊ฒฝ๊ณ„ํฌํ•จ)์„ ์–ป์œผ๋ ค๋ฉด ์ข€ ๋” ๊ณ ๋ฏผ์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒํ•ด์•ผ 0~100 ์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ๋™์ผํ•œ ํ™•๋ฅ ๋กœ ์–ป์„ ์ˆ˜ ์žˆ์„๊นŒ์š”??

 

int max = 100;
int randomInt = (int) (Math.random() * (max + 1));

๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. max๋ฅผ ํฌํ•จํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ max + 1์„ ๊ณฑํ•ด์ฃผ๋ฉด ๋งŒ์‚ฌOK์ž…๋‹ˆ๋‹ค. 

 

์ž, ๊ทธ๋Ÿผ ์ตœ์†Œ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•ด์ฃผ๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”?

 

int min = 90, max = 100;
int randomInt = (int) (Math.random() * (max + 1 - min)) + min;

์œ„ ์ฒ˜๋Ÿผ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ฆ‰, ๋žœ๋คํ•˜๊ฒŒ ์ตœ๋Œ€๊ฐ’์—์„œ ์ตœ์†Œ๊ฐ’์„ ๋บ€ ๋งŒํผ์˜ ์ˆซ์ž๋ฅผ ๋งŒ๋“  ๋’ค์— ์ตœ์†Œ๊ฐ’์„ ๋”ํ•ด์ฃผ๋Š”๊ฑฐ์ฃ . 

 

์—ฌ๊ธฐ์„œ๋Š” ์ตœ๋Œ€๊ฐ’ 100์—์„œ 1์„ ๋”ํ•˜๊ณ  ์ตœ์†Œ๊ฐ’ 90์„ ๋นผ์„œ ์ตœ์†Œ 0~ ์ตœ๋Œ€ 10์ด ๋‚˜์˜ค๋„๋ก ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. 

 

๊ฑฐ๊ธฐ๋‹ค๊ฐ€ min(90)๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด 0+90 ~ 10+90 ์‚ฌ์ด์˜ ์ˆ˜, ์ฆ‰, 90~100 ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ๋˜์ฃ . 

 

ํ•˜๋‚˜ํ•˜๋‚˜ ๋”ฐ๋ผ๊ฐ€๋‹ˆ ์–ด๋ ต์ง€ ์•Š์ฃ ?

 

์˜ค๋Š˜๋„ ์ฆ๊ฑฐ์šด ์ฝ”๋”ฉํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค~

 

 

์ž๋ฐ”์—์„œ ์ˆซ์ž ํŒ๋ณ„ํ•˜๊ธฐ

 

Java์˜ ์ŠคํŠธ๋ง์ด ์ˆซ์ž(์ •์ˆ˜, int)์ธ์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ• ๋‘ ๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค

 

 

1) ์ŠคํŠธ๋ง์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆœํšŒํ•˜๋ฉด์„œ ascii ์ฝ”๋“œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•

 

์ฒซ ๋ฒˆ์งธ๋กœ ์†Œ๊ฐœํ•ด๋“œ๋ฆด ๋ฐฉ๋ฒ•์€ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด(์ŠคํŠธ๋ง)์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ 0~9 ascii ์ฝ”๋“œ๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š” char์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

0~9์˜ ascii ์ฝ”๋“œ๊ฐ’์€ ์‹ญ์ง„์ˆ˜๋กœ 48~57 ์ž…๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ String์„ char[]๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ loop๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ char๊ฐ€ 48~57์‚ฌ์ด์˜ ๊ฐ’์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜์—ฌ ์ˆซ์ž์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

 

์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

String str = "not a number";

boolean isNumber = true;

for(char c : str.toCharArray()) {

  if(c >= 48 && c<= 57) {
        continue;
  } else {
        isNumber = false;
        break;
  }
}
System.out.println("Is string a number ? answer: " + isNumber);

 

2) Integer.parseInt ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

 

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ Integer.parseInt ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

parseInt ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ javadoc ๋ฌธ์„œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

public static int parseInt(String s)
                    throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to theparseInt(java.lang.String, int) method.

 

Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.

 

 

์ฆ‰, ๋ฌธ์ž๋กœ ํ‘œํ˜„๋œ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ int๋กœ returnํ•˜๋Š”๋ฐ์š”

์ž…๋ ฅ๋ฐ›์€ ์ŠคํŠธ๋ง์ด ์ˆซ์ž๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ NumberFormatException์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ์œ„ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ NumberFormatException์˜ ๋ฐœ์ƒ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ํ•ด๋‹น ์ŠคํŠธ๋ง์ด ์ˆซ์ž์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

String str = "421303";

boolean isNumber = false;

try {

Integer.parseInt(str);

isNumber = true;

} catch (NumberFormatException e) {

// do nothing

}

System.out.println("Is string a number ? answer: " + isNumber);

 

Integer ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ wrapper ํด๋ž˜์Šค๋“ค์—๋„ Long.parseLong, Double.parseDouble ๋ฉ”์„œ๋“œ๋“ค์ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

 

 

์œ„ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์™ธ์— ๋˜ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์œผ์‹  ๋ถ„๋“ค ์ œ๋ณด๋ฐ›์Šต๋‹ˆ๋‹ค ใ…Ž

 

๋‚ด์šฉ์ด ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ ๊พน ๋ˆ„๋ฅด๊ณ  ๊ฐ€์ฃผ์„ธ์š”~

 

 

 

 

AWS ์ž๊ฒฉ ์ฆ๋ช… ๋งŒ๋“ค๊ธฐ



  Security Credentials ์„ค์ •ํ•˜๊ธฐ

AWS SDK๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ๋•Œ ์—…๋กœ๋“œ ๊ถŒํ•œ์„ ์–ป๊ธฐ ์œ„ํ•ด Security Credentials์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒด์ ์ธ ํ๋ฆ„์€ ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ถŒํ•œ์„ ์„ค์ •ํ•˜๊ณ  Access key์™€  Secret key๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.



 1  ๋‚ด ๋ณด์•ˆ ์ž๊ฒฉ ์ฆ๋ช…(My Security Credentials) ์„ ๋ˆŒ๋Ÿฌ Security Credentials ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.




 2  IAM ์‚ฌ์šฉ์ž ์‹œ์ž‘ํ•˜๊ธฐ(Get Started with IAM Users) ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ๋‹ค์Œ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. 



 3  ์‚ฌ์šฉ์ž ์ถ”๊ฐ€(Add User) ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ ์œ ์ €๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.



 4  ์‚ฌ์šฉ์ž ์ด๋ฆ„(User name)์„ ์ง€์ •ํ•˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ์•ก์„ธ์Šค(Programmatic access) ๋ฅผ ์„ ํƒํ•œ ๋’ค ๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ธฐ ์ „์— ์•„๋ž˜์ชฝ์œผ๋กœ ํ™”๋ฉด์„ ์Šคํฌ๋กคํ•˜๋ฉด ๊ถŒํ•œ๊ฒฝ๊ณ„์„ค์ • ํ•ญ๋ชฉ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ถŒํ•œ ๊ฒฝ๊ณ„์—†์ด ์ƒ์„ฑ์„ ์„ ํƒํ•˜๊ณ  ๋‹ค์Œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.




 5  ๊ธฐ์กด ์ •์ฑ… ์ง์ ‘ ์—ฐ๊ฒฐ์„ ์„ ํƒํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด S3๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•˜๋ฉด ๊ด€๋ จ ์ •์ฑ… ๋ชฉ๋ก์ด ์ถœ๋ ฅ๋˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” AmazonS3FullAccess๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.



 6  ๊ฒ€ํ† (Review)๋ฅผ ํ†ตํ•ด ์˜ฌ๋ฐ”๋ฅธ ์„ค์ •์„ ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‚ฌ์šฉ์ž ๋งŒ๋“ค๊ธฐ(Create user)๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.



 7  ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž์˜ ์•ก์„ธ์Šค ํ‚ค ID(Access key ID)์™€ ๋น„๋ฐ€ ์•ก์„ธ์Šค ํ‚ค(Secret access key)๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„๋ฐ€์•ก์„ธ์Šค ํ‚ค๋Š” "ํ‘œ์‹œ"๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ณด์—ฌ์ง‘๋‹ˆ๋‹ค.



AWS S3์— ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ๋•Œ ์—ฌ๊ธฐ์— ๋ณด์—ฌ์ง€๋Š” ์•ก์„ธ์Šค ํ‚ค ID์™€ ๋น„๋ฐ€ ์•ก์„ธ์Šค ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž๋Š” ์–ธ์ œ๋“ ์ง€ "๋‚ด ๋ณด์•ˆ ์ž๊ฒฉ ์ฆ๋ช…" ๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.




  AWS SDK Java jar ํŒŒ์ผ ๋‹ค์šด ๋ฐ›๊ธฐ

AWS-SDK for Java - https://aws.amazon.com/ko/sdk-for-java/

์œ„ ๋งํฌ๋ฅผ ๋”ฐ๋ผ ๋“ค์–ด๊ฐ€๋ฉด ์šฐ์ธก ์ƒ๋‹จ์— SDK๋‹ค์šด๋กœ๋“œ ๋˜๋Š” ์ดํด๋ฆฝ์Šค์šฉ ํˆดํ‚ท์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋ฉ”์ด๋ธ์„ ์‚ฌ์šฉํ• ๊ฑด๋ฐ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ฑฐ๋‚˜ ํˆดํ‚ท์„ ์จ๋ณด๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค์€ ์œ„ ๋งํฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”.


maven์„ ์ด์šฉํ•œ๋‹ค๋ฉด 

1
2
3
4
5
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.386</version>
</dependency>


์ด ๊ธ€์ด ํฌ์ŠคํŒ… ๋˜๋Š” ์‹œ์ ์— ์ตœ์‹ ๋ฒ„์ „์€ 1.11.388์ž…๋‹ˆ๋‹ค. minor ๋ฒ„์ „์ด ํ•˜๋ฃจ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ฆ๊ฐ€ํ•˜๊ณ ์žˆ์–ด์š” ใ…Ž



  AWS s3 ํŒŒ์ผ ์—…๋กœ๋“œ ํ•˜๊ธฐ(AWS S3 Upload for java )

์‹ค์ œ๋กœ S3์— ์—…๋กœ๋“œ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” S3์— ๊ถŒํ•œ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ถŒํ•œ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ACCESS_KEY, SECRET_KEY ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋Š” ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

# Access Key, Secret Key ์ƒ์„ฑ๋ฐฉ๋ฒ• - http://keichee.tistory.com/298



์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ ํŒŒ์ผ์„ ์ „๋‹ฌ๋ฐ›์•„ ์•„๋งˆ์กด S3์— accesskey์™€  secretkey๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ถŒํ•œ์„ ์–ป๊ณ  ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class AWSService {
    private static final String BUCKET_NAME = "bucket_name";
    private static final String ACCESS_KEY = "ACCESS_KEY";
    private static final String SECRET_KEY = "SECRET_KEY";
    private AmazonS3 amazonS3;
 
    public AWSService() {
        AWSCredentials awsCredentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        amazonS3 = new AmazonS3Client(awsCredentials);
    }
 
    public void uploadFile(File file) {
        if (amazonS3 != null) {
            try {
                PutObjectRequest putObjectRequest =
                        new PutObjectRequest(BUCKET_NAME + "/sub_dir_name"/*sub directory*/, file.getName(), file);
                putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead); // file permission
                amazonS3.putObject(putObjectRequest); // upload file
 
            } catch (AmazonServiceException ase) {
                ase.printStackTrace();
            } finally {
                amazonS3 = null;
            }
        }
    }
}
 




์ž๋ฐ”๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๊ฐ€ OOM์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํž™๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์–ด๋Š ์ง€์ ์—์„œ ์ฆ๊ฐ€ํ•˜๋Š”์ง€ ํŒŒ์•…์„ ํ•ด์•ผํ•œ๋‹ค. ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์™€ ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ, ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ์ž๋ฐ” ํž™ ๋คํ”„๋ฅผ ๋– ์„œ ํ™•์ธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์†Œ์Šค๋ ˆ๋ฒจ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ, ์ฆ‰, ๊ฐœ๋ฐœ ์‹œ์ ์—์„œ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์–˜๊ธฐํ•ด๋ณด๋ ค ํ•œ๋‹ค.


์ž๋ฐ”๋Š” ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๊ณ ์žˆ์œผ๋ฉฐ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๊ด€๋ จํ•˜์—ฌ Runtime ํด๋ž˜์Šค์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ ์žˆ๋‹ค.


Java 8 API ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…์ด ๋˜์–ด์žˆ๋‹ค.


Class Runtime



  • public class Runtime
    extends Object
    Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

    An application cannot create its own instance of this class.

์ฆ‰, ๋ชจ๋“  ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ๋“ค์€ ํ•˜๋‚˜์˜ Runtime ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ, ์ด Runtime์ธ์Šคํ„ด์Šค๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๊ณ ์žˆ๋Š” ํ™˜๊ฒฝ๊ณผ ์ธํ„ฐํŽ˜์ด์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. ์ฆ‰, ์ž๋ฐ”ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๊ณ ์žˆ๋Š” ์‹œ์Šคํ…œ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (๋ช…๋ น์–ด ์‹คํ–‰๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.)


๊ทธ๋Ÿผ Runtime ํด๋ž˜์Šค์—์„œ ์–ด๋–ค ๋ฉ”์„œ๋“œ๋“ค์„ ์ œ๊ณตํ•˜๋Š”์ง€ ํ•œ๋ฒˆ ๋ณด์ž.


Modifier and Type

Method and Description
voidaddShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook.
intavailableProcessors()
Returns the number of processors available to the Java virtual machine.
Processexec(String command)
Executes the specified string command in a separate process.
Processexec(String[] cmdarray)
Executes the specified command and arguments in a separate process.
Processexec(String[] cmdarray, String[] envp)
Executes the specified command and arguments in a separate process with the specified environment.
Processexec(String[] cmdarray, String[] envp, File dir)
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Processexec(String command, String[] envp)
Executes the specified string command in a separate process with the specified environment.
Processexec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory.
voidexit(int status)
Terminates the currently running Java virtual machine by initiating its shutdown sequence.
longfreeMemory()
Returns the amount of free memory in the Java Virtual Machine.
voidgc()
Runs the garbage collector.
InputStreamgetLocalizedInputStream(InputStream in)
Deprecated. 
As of JDK 1.1, the preferred way to translate a byte stream in the local encoding into a character stream in Unicode is via the InputStreamReader and BufferedReader classes.
OutputStreamgetLocalizedOutputStream(OutputStream out)
Deprecated. 
As of JDK 1.1, the preferred way to translate a Unicode character stream into a byte stream in the local encoding is via the OutputStreamWriterBufferedWriter, and PrintWriter classes.
static RuntimegetRuntime()
Returns the runtime object associated with the current Java application.
voidhalt(int status)
Forcibly terminates the currently running Java virtual machine.
voidload(String filename)
Loads the native library specified by the filename argument.
voidloadLibrary(String libname)
Loads the native library specified by the libname argument.
longmaxMemory()
Returns the maximum amount of memory that the Java virtual machine will attempt to use.
booleanremoveShutdownHook(Thread hook)
De-registers a previously-registered virtual-machine shutdown hook.
voidrunFinalization()
Runs the finalization methods of any objects pending finalization.
static voidrunFinalizersOnExit(boolean value)
Deprecated. 
This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.
longtotalMemory()
Returns the total amount of memory in the Java virtual machine.
voidtraceInstructions(boolean on)
Enables/Disables tracing of instructions.
voidtraceMethodCalls(boolean on)
Enables/Disables tracing of method calls.


์œ„ ๋ฉ”์„œ๋“œ๋“ค์ด Runtime ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๊ณ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด๋‹ค.

์ด ์ค‘์—์„œ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ จ ๋ฉ”์„œ๋“œ๋“ค์ด ๋ช‡ ๊ฐœ ๋ณด์ธ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋กœ freeMemory๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ๋ฉ”์„œ๋“œ๋Š” ํž™๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์—์„œ free๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฐ”์ดํŠธ๋‹จ์œ„๋กœ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  maxMemory์™€ totalMemory๊ฐ€ ์žˆ๋Š”๋ฐ totalMemory๋Š” ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‚ฌ์šฉํ•˜๊ณ ์žˆ๋Š” ์ด ํž™ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, maxMemory๋Š” OOM(OutOfMemory)์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ์—ญ์‹œ ๋ฐ”์ดํŠธ ๋‹จ์œ„์ด๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ด ๋ฉ”๋ชจ๋ฆฌ ํ•œ๊ณ„๋ฅผ ๋„˜์–ด์„œ๋Š” ์ˆœ๊ฐ„ OOM์ด ๋ฐœ์ƒํ•˜๋ฉด์„œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์ด ์ค‘๋‹จ๋œ๋‹ค.


์‚ฌ์šฉ์˜ˆ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.


// ํ˜„์žฌ ํž™ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜

long heapSize = Runtime.getRuntime().totalMemory(); 


// ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ• ๋‹น๋œ ํž™๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ. ์ด ์‚ฌ์ด์ฆˆ๋ฅผ ๋„˜์–ด์„œ๋ฉด OOM ๋ฐœ์ƒ

long heapMaxSize = Runtime.getRuntime().maxMemory();


// ํ˜„์žฌ ๋‚จ์•„์žˆ๋Š” free๋ฉ”๋ชจ๋ฆฌ. ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์ฆ๊ฐ์„ ๋ฐ˜๋ณต

long heapFreeSize = Runtime.getRuntime().freeMemory();


ํ•ด๋‹น ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์€ ๋’ค KB, MB๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ์ฝ๊ธฐ๊ฐ€ ํŽธํ•˜๋‹ค.

[๋ฉ”์ด๋ธ ํ”„๋กœ์ ํŠธ์— external jar ํŒŒ์ผ์„ ์˜์กด์„ฑ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•]

 

maven ํ”„๋กœ์ ํŠธ์˜ ์›น์•ฑ์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•˜๋˜ ์ค‘ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ๋ถ™์ผ ์ผ์ด ์žˆ์—ˆ๋‹ค. ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” ์ฝ”๋ชจ๋ž€์„ ์‚ฌ์šฉํ•˜์˜€๊ณ , ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ.

๋นŒ๋“œ ํ•  ๋•Œ ์ฝ”๋ชจ๋ž€ ๋ฉ”์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์ถ”๊ฐ€๋กœ ๋‘ ๊ฐœ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ war ์•ˆ์— ๋ฌถ์–ด ์ฃผ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์— ๋ณ„๋„์˜ lib ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์— ํ•„์š”ํ•œ jar ํŒŒ์ผ๋“ค์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

 

 

๊ทธ๋ฆฌ๊ณ  pom.xml ํŒŒ์ผ์— ์˜์กด์„ฑ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

๊ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์˜ groupId, artifactId, version ์ •๋ณด๋Š” ์ž„์˜๋กœ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๋„ฃ์–ด์ค˜๋„ ๋ฌด๊ด€ํ•œ๊ฒƒ ๊ฐ™๊ณ , systemPath๋งŒ ์ž˜ ์žก์•„์ฃผ๋ฉด ๋œ๋‹ค. 

 

maven์˜ systemPath๋Š” scope์ด system์ผ ๋•Œ์—๋งŒ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

MyBatis์—์„œ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ์ฒด(MyObject)์˜ ํ•„๋“œ์— ํŠน์ • ๊ฐ’์„ ์„ธํŒ…ํ•  ๋•Œ selectKey ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


์˜ˆ๋ฅผ๋“ค์–ด MyObject๊ฐ€ ์•„๋ž˜ ๋‘ ๊ฐœ์˜ ํ•„๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž.

String id;

String name;


MyBatis๋ฅผ ์ด์šฉํ•˜์—ฌ MyObject๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์ด๋ฆ„๊ฐ’๋งŒ ํ™”๋ฉด์—์„œ ์ „๋‹ฌ๋ฐ›๊ณ  ID๋ฅผ ์ž๋™์œผ๋กœ sequence๋ฅผ ๋”ฐ์„œ ์ž…๋ ฅ ํ•ด์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋•Œ  MyObject์˜ idํ•„๋“œ์— ๊ฐ’ ์„ธํŒ…


<insert id="insert" parameterType="MyObject">

        <selectKey keyProperty="id" resultType="int" order="BEFORE">

            select nextval('sequence_id')

        </selectKey>

        <![CDATA[

        INSERT INTO person(id, name)

        VALUES (#{id}, #{name});

        ]]>

    </insert>


order="BEFORE" ์˜ต์…˜์€ insert ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ์ „์— keyProperty์— ๊ฐ’์„ ์„ธํŒ…ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.


์ž๋ฐ” ์†Œ์Šค๋‹จ์—์„œ ๊ฐ์ฒด์˜ id์— ๊ฐ’์„ ์„ธํŒ…ํ•˜์ง€ ์•Š๊ณ ์žˆ๋Š”๋ฐ ์ฟผ๋ฆฌ ์‹คํ–‰ ํ›„์— id๊ฐ’์ด ์„ธํŒ…๋˜์–ด์ ธ์„œ ๋‚˜์˜ค๋Š” ์ผ€์ด์Šค๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ ์ด๊ฒŒ ๋ญ”๊ฐ€ ์‹ถ์–ด ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ์•Œ๊ฒŒ๋จ.


mybatis์„ค์ • ํŒŒ์ผ์—๋Š” userGeneratedKeys ์„ค์ •์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

<configuration>

    <settings>

        <setting name="cacheEnabled" value="false"/>

        <setting name="useGeneratedKeys" value="true"/>

        <setting name="defaultExecutorType" value="REUSE"/>

    </settings>

</configuration>




์œ„ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜ ์ €๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ์ผ€์ด์Šค๊ฐ€ ๋งŽ์ง€ ์•Š๋‹ค๋ฉด ์ฟผ๋ฆฌ์— ์ง์ ‘ ์˜ต์…˜์œผ๋กœ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.


<insert
  id="insertAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  keyProperty=""
  keyColumn=""
  useGeneratedKeys=""
  timeout="20">

์†์„ฑ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์†์„ฑ์„ค๋ช…
id๊ตฌ๋ฌธ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋‚ด ์œ ์ผํ•œ ๊ตฌ๋ถ„์ž
parameterType๊ตฌ๋ฌธ์— ์ „๋‹ฌ๋  ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•œ ์ „์ฒด ํด๋ž˜์Šค๋ช…์ด๋‚˜ ๋ณ„์นญ
parameterMap์™ธ๋ถ€ parameterMap ์„ ์ฐพ๊ธฐ ์œ„ํ•œ ๋น„๊ถŒ์žฅ๋œ ์ ‘๊ทผ๋ฐฉ๋ฒ•. ์ธ๋ผ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ ๋งคํ•‘๊ณผ parameterType์„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋ผ.
flushCache์ด ๊ฐ’์„ true ๋กœ ์…‹ํŒ…ํ•˜๋ฉด ๊ตฌ๋ฌธ์ด ํ˜ธ์ถœ๋ ๋•Œ๋งˆ๋‹ค ์บ์‹œ๊ฐ€ ์ง€์›์งˆ๊ฒƒ์ด๋‹ค(flush). ๋””ํดํŠธ๋Š” false ์ด๋‹ค.
timeout์˜ˆ์™ธ๊ฐ€ ๋˜์ ธ์ง€๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์š”์ฒญ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ตœ๋Œ€์‹œ๊ฐ„์„ ์„ค์ •ํ•œ๋‹ค. ๋””ํดํŠธ๋Š” ์…‹ํŒ…ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๊ณ  ๋“œ๋ผ์ด๋ฒ„์— ๋”ฐ๋ผ ๋‹ค์†Œ ์ง€์›๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.
statementTypeSTATEMENT, PREPARED ๋˜๋Š” CALLABLE์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค์—๊ฒŒ Statement, PreparedStatement ๋˜๋Š” CallableStatement๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•œ๋‹ค. ๋””ํดํŠธ๋Š” PREPARED ์ด๋‹ค.
useGeneratedKeys(์ž…๋ ฅ(insert, update)์—๋งŒ ์ ์šฉ) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑํ•œ ํ‚ค (์˜ˆ๋ฅผ๋“ค์–ด MySQL๋˜๋Š” SQL Server์™€ ๊ฐ™์€ RDBMS์˜ ์ž๋™ ์ฆ๊ฐ€ ํ•„๋“œ)๋ฅผ ๋ฐ›๋Š” JDBC getGeneratedKeys๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋‹ค. ๋””ํดํŠธ๋Š” false ์ด๋‹ค.
keyProperty(์ž…๋ ฅ(insert, update)์—๋งŒ ์ ์šฉ) getGeneratedKeys ๋ฉ”์†Œ๋“œ๋‚˜ insert ๊ตฌ๋ฌธ์˜ selectKey ํ•˜์œ„ ์—˜๋ฆฌ๋จผํŠธ์— ์˜ํ•ด ๋ฆฌํ„ด๋œ ํ‚ค๋ฅผ ์…‹ํŒ…ํ•  ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง€์ •. ๋””ํดํŠธ๋Š” ์…‹ํŒ…ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ”„๋กœํผํ‹ฐ๋ช…์— ์ฝค๋งˆ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜์—ดํ• ์ˆ˜ ์žˆ๋‹ค.
keyColumn(์ž…๋ ฅ(insert, update)์—๋งŒ ์ ์šฉ) ์ƒ์„ฑํ‚ค๋ฅผ ๊ฐ€์ง„ ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ๋ช…์„ ์…‹ํŒ…. ํ‚ค ์นผ๋Ÿผ์ด ํ…Œ์ด๋ธ”์ด ์ฒซ๋ฒˆ์งธ ์นผ๋Ÿผ์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(PostgreSQL ์ฒ˜๋Ÿผ)์—์„œ๋งŒ ํ•„์š”ํ•˜๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ”„๋กœํผํ‹ฐ๋ช…์— ์ฝค๋งˆ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜์—ดํ• ์ˆ˜ ์žˆ๋‹ค.
databaseId์„ค์ •๋œ databaseIdProvider๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋Š” databaseId ์†์„ฑ์ด ์—†๋Š” ๋ชจ๋“  ๊ตฌ๋ฌธ์„ ๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์ผ์น˜ํ•˜๋Š” databaseId์™€ ํ•จ๊ป˜ ๋กœ๋“œ๋  ๊ฒƒ์ด๋‹ค. ๊ฐ™์€ ๊ตฌ๋ฌธ์—์„œ databaseId๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ์—†๋Š” ๊ฒฝ์šฐ ๋ชจ๋‘ ์žˆ๋‹ค๋ฉด ๋’ค์— ๋‚˜์˜จ ๊ฒƒ์ด ๋ฌด์‹œ๋œ๋‹ค.


Git์—์„œ  ํƒœ๊ทธ ์‚ญ์ œํ•˜๊ธฐ


git tag -d {name of tag}

git push origin :refs/tags/{name of tag}


git tag -d 12345
git push origin :refs/tags/12345