CodeGym /Java Course /๋ชจ๋“ˆ 3 /์„œ๋ธ”๋ฆฟ ์†Œ๊ฐœ

์„œ๋ธ”๋ฆฟ ์†Œ๊ฐœ

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

1.1 ์ธํ„ฐํŽ˜์ด์Šค ์„œ๋ธ”๋ฆฟ

์˜ค๋Š˜ ์šฐ๋ฆฌ๋Š” ์ƒˆ๋กญ๊ณ  ํฅ๋ฏธ๋กœ์šด ์ฃผ์ œ์ธ ์„œ๋ธ”๋ฆฟ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค . Java์— ์„œ๋ธ”๋ฆฟ์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ Java๊ฐ€ ๋Œ€๊ทœ๋ชจ ์„œ๋ฒ„ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ์‚ฌ์‹ค์ƒ์˜ ํ‘œ์ค€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ „ ์„ธ๊ณ„ ๋ชจ๋“  ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ์†Œํ”„ํŠธ์›จ์–ด์˜ 80%๋Š” Java๋กœ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ค‘๊ตญ์—์„œ๋Š” ๋ชจ๋“  ๊ฒƒ์ด 100%์ž…๋‹ˆ๋‹ค. ์„œ๋ธ”๋ฆฟ์ด๋ž€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์„œ๋ธ”๋ฆฟ์€ ์ •ํ™•ํžˆ Java ํ”„๋กœ๊ทธ๋žจ์„ ์›น ์„œ๋น„์Šค๋กœ ์ „ํ™˜ํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋žฌ๋‹ค...

90๋…„๋Œ€์—๋Š” World Wide Web์ด ๋“ฑ์žฅํ•œ ์งํ›„ ์›น ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €)์™€ ์›น ์„œ๋ฒ„๊ฐ€ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์›น ์„œ๋ฒ„๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ์ €์žฅํ•œ ํŒŒ์ผ(html ํŽ˜์ด์ง€, ์Šคํฌ๋ฆฝํŠธ, ๊ทธ๋ฆผ ๋“ฑ)์„ ๋‹จ์ˆœํžˆ ๋ฐฐํฌํ–ˆ์Šต๋‹ˆ๋‹ค.

์–ด๋Š ์‹œ์ ์—์„œ ๋ชจ๋‘๊ฐ€ ์–‘์ชฝ ๋ชจ๋‘๋ฅผ ๋” ๋˜‘๋˜‘ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒฐ๋ก ์— ๋„๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค. JavaScript๊ฐ€ HTML ํŽ˜์ด์ง€์— ์ถ”๊ฐ€๋˜์—ˆ๊ณ  ํ”Œ๋Ÿฌ๊ทธ์ธ์ด ์„œ๋ฒ„์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ ํ˜ธ์ถœ๋˜์–ด ์„œ๋ฒ„์˜ ๋™์ž‘์„ ๋ณด๋‹ค ์œ ์—ฐํ•˜๊ณ  ์Šค๋งˆํŠธํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ํŠน์ˆ˜ ์Šคํฌ๋ฆฝํŠธ์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์„œ๋ธ”๋ฆฟ์€ ํŠน์ • ํŽ˜์ด์ง€์— ๋Œ€ํ•ด ์š”์ฒญ๋  ๋•Œ Java ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” Java ํ”Œ๋Ÿฌ๊ทธ์ธ ์ž…๋‹ˆ๋‹ค . Java web-server๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ Servlet ํด๋ž˜์Šค์—์„œ ์ƒ์†๋œ ํด๋ž˜์Šค๋กœ ํ‘œ์‹œ๋˜๋Š” ์ด Java ์ฝ”๋“œ๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์˜๋„ํ•œ ๋Œ€๋กœ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋“ฏ์ด ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” Java ์›น ์„œ๋ฒ„๋Š” Tomcat ์ž…๋‹ˆ๋‹ค . ๊ทธ๊ฑด ๊ทธ๋ ‡๊ณ , ๋งŒํ™” "Tom and Jerry"์˜ ๊ณ ์–‘์ด Tom์„ ๊ธฐ๋ฆฌ๊ธฐ ์œ„ํ•ด ๋ช…๋ช…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Tomcat์€ ์„œ๋ธ”๋ฆฟ๊ณผ ์–ด๋–ป๊ฒŒ ์ƒํ˜ธ ์ž‘์šฉํ•ฉ๋‹ˆ๊นŒ? ์‹ค์ œ๋กœ ์ด ํ”„๋กœ์„ธ์Šค๋Š” ํ‘œ์ค€ํ™”๋˜์–ด ์žˆ์œผ๋ฉฐ ์„œ๋ธ”๋ฆฟ ๋ผ์ดํ”„ ์‚ฌ์ดํด ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค . ์—ฌ๊ธฐ์—์„œ ์„œ๋ธ”๋ฆฟ์€ ๋กœ๋“œ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์ด๊ณ  ์›น ์„œ๋ฒ„๋Š” ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ์ž…๋‹ˆ๋‹ค .

์„œ๋ธ”๋ฆฟ์ด ์•„์ง ๋กœ๋“œ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  1. ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋Š” ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค.
  2. ์ปจํ…Œ์ด๋„ˆ๋Š” ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค(๊ฐ์ฒด)์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  3. init()์ปจํ…Œ์ด๋„ˆ๋Š” ์„œ๋ธ”๋ฆฟ ๊ฐœ์ฒด์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค . ๋ฉ”์„œ๋“œ๋Š” ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

ํ‘œ์ค€ ์ž‘์—… ์ฃผ๊ธฐ - ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์„œ๋น„์Šค :

  • ๊ฐ ์š”์ฒญ์€ ๋ณ„๋„์˜ ์Šค๋ ˆ๋“œ์—์„œ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
  • service()์ปจํ…Œ์ด๋„ˆ๋Š” ์„œ๋ธ”๋ฆฟ์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•˜๊ณ  ๊ทธ๊ณณ์—์„œ ServletRequest ๋ฐ ServletResponse ๊ฐœ์ฒด๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • destroy()์„œ๋ธ”๋ฆฟ์„ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธ”๋ฆฟ ๊ฐœ์ฒด์—์„œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค . ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

์„œ๋ธ”๋ฆฟ์ด ์ข…๋ฃŒ๋˜๋Š” ๋ฐ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ด์œ ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

์ค‘์š”ํ•œ ์ ์„ ๊ธฐ์–ตํ•˜์‹ญ์‹œ์˜ค. ์›น ์„œ๋ฒ„์™€ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์€ ์žฅ์•  ์—†์ด ์ž‘๋™ํ•˜๊ณ  ๋ถ„๋‹น ์ˆ˜์ฒœ ๊ฑด์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋ช‡ ๋‹ฌ ๋™์•ˆ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„œ๋ธ”๋ฆฟ์„ ๋กœ๋“œํ•˜๊ณ  ์ž‘์—…ํ•˜๊ณ  ์–ธ๋กœ๋“œํ•˜๋Š” ์ฝ”๋“œ๋Š” ํ•ญ์ƒ ๋งค์šฐ ๋†’์€ ํ’ˆ์งˆ๋กœ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

1.2 HttpServlet ํด๋ž˜์Šค

์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋Š” ์„œ๋ธ”๋ฆฟ๊ณผ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์„ ํ‘œ์ค€ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ด ํด๋ž˜์Šค์™€ ์ง์ ‘ ์ž‘์—…ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ธ€์Ž„, ๊ทธ๋“ค์€ ๊ฑฐ์˜ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋Š” HttpServletServlet์—์„œ ์ƒ์†๋ฉ๋‹ˆ๋‹ค.

์ด ํด๋ž˜์Šค์—๋Š” ์šฐ๋ฆฌ์—๊ฒŒ ์œ ์šฉํ•œ ๋ช‡ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž์ฃผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• ์„ค๋ช…
1 init() ์„œ๋ธ”๋ฆฟ์ด ๋กœ๋“œ๋  ๋•Œ ํ•œ ๋ฒˆ ํ˜ธ์ถœ๋จ
2 destroy() ์„œ๋ธ”๋ฆฟ์ด ์–ธ๋กœ๋“œ๋  ๋•Œ ํ•œ ๋ฒˆ ํ˜ธ์ถœ๋จ
์‚ผ service(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
4 doGet(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด GET ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
5 doPost(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด POST ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
6 doHead(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด HEAD ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
7 doDelete(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด DELETE ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
8 doPut(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ PUT ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ

init()๋ฐ ๋ฉ”์†Œ๋“œ๋Š” destroy()Servlet ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„œ๋ธ”๋ฆฟ์—์„œ ์žฌ์ •์˜ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ•œ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ํด๋ž˜์Šค์—์„œ ํ•ด๋‹น ๊ตฌํ˜„์„ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค super.method name().

์„œ๋ธ”๋ฆฟ ์˜ˆ:


public class FirstHttpServlet extends HttpServlet {
  
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        // Getting the parameter โ€œsecretโ€ from request
        String secret = request.getParameter("secret");
 
        // Put parameter โ€œsecretโ€ into Http-session
        HttpSession session = request.getSession(true);
        session.setAttribute("secret", secret);
 
        // Print HTML as response for browser
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Header</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet example "+ secret +"</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }
}

1.3 ์„œ๋น„์Šค(HttpServletRequest, HttpServletResponse) ๋ฉ”์†Œ๋“œ

์„œ๋ธ”๋ฆฟ์˜ ๊ด€์ ์—์„œ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๊ฐ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋Œ€ํ•ด ์ปจํ…Œ์ด๋„ˆ(์›น ์„œ๋ฒ„)๋Š” ๊ฐœ์ฒด๋ฅผ ์ƒ์„ฑํ•œ HttpServletRequest๋‹ค์Œ HttpServletResponseํ•ด๋‹น service(HttpServletRequest request, HttpServletResponse response)์„œ๋ธ”๋ฆฟ์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐœ์ฒด๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  request์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋„๋ก ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค response.

์ด ๋ฉ”์„œ๋“œ์—๋Š” service()๊ธฐ๋ณธ ๊ตฌํ˜„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์žฌ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์ด ๊ทธ๊ฐ€ ํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ๋Š” service()์š”์ฒญ(GET, POST, ...)์—์„œ HTTP ๋ฉ”์„œ๋“œ ์œ ํ˜•์„ ๊ฒฐ์ •ํ•˜๊ณ  ์š”์ฒญ์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• ์„ค๋ช…
1 service(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
2 doGet(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด GET ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
์‚ผ doPost(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด POST ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
4 doHead(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด HEAD ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
5 doDelete(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ๋กœ์šด DELETE ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ
6 doPut(HttpRequest, HttpResponse) ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ƒˆ PUT ์š”์ฒญ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋จ

ํด๋ž˜์Šค์—์„œ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜๊ฑฐ๋‚˜ service()๊ทธ๋Œ€๋กœ ๋‘๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ๋ฉ”์„œ๋“œ doGet(), , ...๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.doPost()

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