CodeGym /Java Course /๋ชจ๋“ˆ 3 /JSP ํŒŒ์ผ ๊ตฌ์กฐ

JSP ํŒŒ์ผ ๊ตฌ์กฐ

๋ชจ๋“ˆ 3
๋ ˆ๋ฒจ 13 , ๋ ˆ์Šจ 1
์‚ฌ์šฉ ๊ฐ€๋Šฅ

2.1 JSP ์ง€์‹œ์–ด

JSP ํŒŒ์ผ์ด ์ผ๋ฐ˜ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์œผ๋ฏ€๋กœ ์ •์ƒ์ ์ธ Java ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด Java ์ฝ”๋“œ์—์„œ ๋‹ค์–‘ํ•œ ํด๋ž˜์Šค(์˜ˆ์ œ์˜ Math)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ›จ์”ฌ ๋” ์ข‹์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ ์„œ๋ธ”๋ฆฟ ๋ฐฉ์‹์œผ๋กœ ์˜ฎ๊ฒจ์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค doGet(). ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ์€ ์ฆ‰์‹œ ๋ช‡ ๊ฐ€์ง€ ์งˆ๋ฌธ์„ ์ œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

  • ์ฝ”๋“œ๊ฐ€ ๋ฉ”์„œ๋“œ๋กœ ์ด๋™๋˜๋„๋ก ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ doPost()?
  • ๋ฐฉ๋ฒ•์— ์ž์‹ ์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ init()?
  • ๊ฒฐ๊ตญ ํด๋ž˜์Šค ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

ํ›Œ๋ฅญํ•œ ์งˆ๋ฌธ๊ณผ ๋ฌผ๋ก  ๋‹ต๋ณ€์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ JSP ์ง€์‹œ๋ฌธ์ž…๋‹ˆ๋‹ค. JSP ๋‚ด์˜ ๋ชจ๋“  ๋น„ HTML ์ฝ”๋“œ๋Š” ํŠน์ˆ˜ ๋Œ€๊ด„ํ˜ธ <%๋ฐ ๋กœ ๋ฌถ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค %>. ๋ชจ๋“  JSP ์ง€์‹œ๋ฌธ์€ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

<%@ directive %>

๋‹ค์Œ์€ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํŽ˜์ด์ง€ ์ง€์‹œ๋ฌธ ์†์„ฑ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

์˜ˆ ์„ค๋ช…
1 ์ˆ˜์ž… <%@ page import="java.util.Date" %> ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค
2 ์ปจํ…์ธ  ํƒ€์ž… <%@ page contentType=text/html %> ์ฝ˜ํ…์ธ  ์œ ํ˜• ์„ค์ •
์‚ผ ์—ฐ์žฅํ•˜๋‹ค <%@ page extends="Object" %> ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
4 ์ •๋ณด <%@ page info="Author: Peter Ivanovich; version:1.0" %> getServletInfo()์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์„ค์ •
5 ์™„์ถฉ๊ธฐ <%@ page buffer="16kb" %> ์‘๋‹ต ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
6 ์–ธ์–ด <%@ page language="java" %> ์–ธ์–ด๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ Java์ž…๋‹ˆ๋‹ค.
7 isELigned <%@ page isELIgnored="true" %> EL ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
8 isThreadSafe <%@ page isThreadSafe="false" %> ์Šค๋ ˆ๋“œ์„ธ์ดํ”„๋ฅผ ๊ฐ€๋ฆฌํ‚ด
9 ์ž๋™ ํ”Œ๋Ÿฌ์‹œ <%@ page autoFlush="false" %> ๋ฒ„ํผ ์“ฐ๊ธฐ ๊ด€๋ฆฌ
10 ์„ธ์…˜ <%@ page session="false" %> ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์„ธ์…˜์„ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์—ดํ•˜๋‚˜ ํŽ˜์ด์ง€์ธ์ฝ”๋”ฉ <%@ page pageEncoding="UTF-8"%> ํŽ˜์ด์ง€ ์ธ์ฝ”๋”ฉ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
12 errorPage <%@ page errorPage="errorpage.jsp" %> ์˜ค๋ฅ˜ ํŽ˜์ด์ง€๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2.2 ์ˆ˜์ž…ํ’ˆ

์žฌ๋ฏธ๋ฅผ ์œ„ํ•ด JSP ํŒŒ์ผ์— ๋ช‡ ๊ฐ€์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

JSP ํŒŒ์ผ ์˜ˆ:


    <%@ page import="java.util.Date" %> 
    <%@ page import="java.lang.Math" %> 
    <%@ page extends="com.codegym.MyHttpServlet" %> 
 
    <html> 
    <body> 
    <%
        double num = Math.random();
        if (num > 0.95) {
     %>
         <h2>You are lucky, user!</h2><p>(<%= num %>)</p>
    <%
    }
    %> 
  </body> 
   </html> 

๊ทธ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import java.util.Date;
import java.lang.Math;

public class HelloServlet extends com.codegym.MyHttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws Exception {
    PrintWriter out = response.getWriter();
    out.print("<html>  ");
    out.print("<body> ");
        double num = Math.random();
        if (num >  0.95) {
             out.print("<h2> You're lucky, user! </h2> <p> (" + num + ")</p> ");
        }
    out.print("</body> ");
    out.print("</html> ");
    }
}

๊ณต์žฅ. ์—„์ฒญ๋‚œ!

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION