JiwonDev

#16 Servlet ์ดํ•ด

by JiwonDev

# ์›น ๊ฐœ๋ฐœ์˜ ๊ณผ์ •

์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ์ƒํƒœ์—์„œ ์›น ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค๋Š” ๊ฑด, ์•„๋ž˜์˜ ๊ณผ์ •๋“ค์„ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•จ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋Š” ์–ด๋– ํ•œ ๋„คํŠธ์›Œํฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋˜ ๊ณตํ†ต์ ์œผ๋กœ ๋ฐ˜๋ณต๋˜๋Š” ๊ณผ์ •์ด์—ˆ๋‹ค.

์ •์ž‘ ์˜๋ฏธ์žˆ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฝ”๋“œ๋Š” ์–ผ๋งˆ ๋˜์ง€ ์•Š์•˜๋‹ค.

 


# Java์˜ Servlet ์ธํ„ฐํŽ˜์ด์Šค ๋“ฑ์žฅ.

'๋„คํŠธ์›Œํฌ ์ฝ”๋“œ์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋ฅผ ์ธํ„ฐํŽ˜์ด์Šคํ™” ์‹œํ‚ค๋ฉด ์•ˆ๋ ๊นŒ?'์—์„œ ๋‚˜์˜จ ๊ฒƒ์ด Servlet ํด๋ž˜์Šค์ด๋‹ค.

Server + let, ์„œ๋ฒ„์—์„œ ์‹คํ–‰ํ•˜๋Š” ์ž‘์€ ์‹คํ–‰๋‹จ์œ„๋ผ๋Š” ์˜๋ฏธ๋กœ TCP/IP ํ†ต์‹ ์€ Servlet ํด๋ž˜์Šค๊ฐ€ ๋‹ด๋‹นํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ๋ถ€๋ถ„๋งŒ ์ง์ ‘ ๊ตฌํ˜„๋„๋ก ์ธํ„ฐํŽ˜์ด์Šคํ™” ์‹œํ‚จ ๊ฒƒ์ด๋‹ค. (* Servlet์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์›น ์„œ๋ฒ„๋ฅผ WAS๋ผ๊ณ  ํ•œ๋‹ค.)

  • html์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ์— ์‘๋‹ตํ•œ๋‹ค.
  • Java Thread๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์ž‘ํ•œ๋‹ค.
  • MVC ํŒจํ„ด์—์„œ Controller๋กœ ์ด์šฉ๋œ๋‹ค.
  • HTTP ํ”„๋กœํ† ์ฝœ ์„œ๋น„์Šค๋ฅผ ์ง€์›ํ•˜๋Š” javax.servlet.http.HttpServlet ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
  • UDP๋ณด๋‹ค ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค.
  • HTML ๋ณ€๊ฒฝ ์‹œ Servlet์„ ์žฌ์ปดํŒŒ์ผํ•ด์•ผ ํ•˜๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

HttpServlet์„ ์ƒ์†๋ฐ›์•„ ์„œ๋ธ”๋ฆฟ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ HTTP ์‘๋‹ต/์ „์†ก์„ ํ•˜๊ธฐ์œ„ํ•ด ์„œ๋ธ”๋ฆฟ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” Request์™€ Response ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์–ด๋…ธํ…Œ์ด์…˜(@WebServlet)์„ ํ™œ์šฉํ•ด์„œ ์„œ๋ธ”๋ฆฟ์„ ๊ตฌํ˜„ํ•˜๋Š”๊ฑด ๋น„๊ต์  ์ตœ์‹ ์— ๋‚˜์˜จ ๊ธฐ๋Šฅ์ด๋‹ค.

์ฐธ๊ณ ๋กœ ์„œ๋ธ”๋ฆฟ์„ ์–ด๋…ธํ…Œ์ด์…˜(@WebServlet)์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๊ธฐ๋Šฅ์€ Servlet 3 ๋ฒ„์ „(Tomcat 7 ์ดํ›„)์— ๋‚˜์˜จ ๊ฒƒ์œผ๋กœ, ์ด์ „์—๋Š” web.xml ํŒŒ์ผ์— ์„œ๋ธ”๋ฆฟ์„ ์ง์ ‘ ๋“ฑ๋กํ•ด์ฃผ์–ด์•ผ ํ–ˆ๋‹ค. <servlet>์œผ๋กœ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ๋“ฑ๋กํ•˜๊ณ , <servlet-mapping>์œผ๋กœ ์š”์ฒญ URL์— ๋‹ด๋‹น ์„œ๋ธ”๋ฆฟ์„ ์ง์ ‘ ๋งคํ•‘ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

// web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JSP_1031</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>Hello</servlet-name> //servlet ์ด๋ฆ„ ๋ช…์‹œ
  <servlet-class>servlet.HelloServlet</servlet-class> //ํŒจํ‚ค์ง€.ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋“ฑ๋ก ํ•ด์คŒ
  </servlet>
  
  <servlet-mapping>
  <servlet-name>Hello</servlet-name> //servlet์˜ name์„ ๋งคํ•‘
  <url-pattern>/hello</url-pattern>  / ๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”!! (์ฃผ์†Œ ๋’ค์— ๊ฐ’์ด๋ผ์„œ)
  </servlet-mapping>
</web-app>

 


# Servlet์˜ ๋™์ž‘

์„œ๋ธ”๋ฆฟ์€ WAS (์›น ์ปจํ…Œ์ด๋„ˆ, ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ)์—์„œ ๋™์ž‘ํ•œ๋‹ค. WAS์— ์žˆ๋Š” ์›น ์ปจํ…Œ์ด๋„ˆ๋Š” ์„œ๋ธ”๋ฆฟ์˜ ์ƒ์„ฑ, ํ˜ธ์ถœ, ๊ด€๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

  1.  Servlet ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์„œ WAS์— ๋“ฑ๋กํ•ด๋‘๋ฉด, ์›น ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ Servlet ์œผ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    * Servlet์€ ์‹ฑ๊ธ€ํ†ค์„ ๊ตฌํ˜„ํ•œ๋‹ค. ์ฆ‰ .init() ๋ฉ”์„œ๋“œ๋กœ ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด์ง„ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋Š” ๊ณต์œ ํ•ด์„œ ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ์“ฐ์ธ๋‹ค. .init()๋Š” ์ฒ˜์Œ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋œ ํ›„ ์„œ๋ธ”๋ฆฟ์ด ์žฌ์‚ฌ์šฉ๋œ๋‹ค. ๋งŒ์•ฝ ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค๋ฉด, ์†Œ๋ฉธ์‹œํ‚ค๊ณ  ๋‹ค์‹œ init()ํ•˜์—ฌ ๋งŒ๋“ ๋‹ค.
  2. . ์ปจํ…Œ์ด๋„ˆ๋Š” Servlet ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด HTTP ์‘๋‹ต ๊ฐ์ฒด(Request,Response)๋ฅผ ๋งŒ๋“ค๊ณ  ์Šค๋ ˆ๋“œ๋ฅผ ํ• ๋‹น๋ฐ›์•„ Servlet ํด๋ž˜์Šค์˜ .service() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  3. ์ด๋ ‡๊ฒŒ ์Šค๋ ˆ๋“œ๋ฅผ ํ• ๋‹น๋ฐ›๊ณ  ์‹คํ–‰๋œ Servlet์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ณ  doGet()์ด๋‚˜ doPost() ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋Š” ๋™์  ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Response ๊ฐ์ฒด์— ์‹ค์–ด ์ปจํ…Œ์ด๋„ˆ์— ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  4. ์ปจํ…Œ์ด๋„ˆ๋Š” ์ „๋‹ฌ๋ฐ›์€ Response๋ฅผ ๋‹ค์‹œ HTTP Response๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์›น์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ณด๋‚ธ๋‹ค. ์ดํ›„ ์Šค๋ ˆ๋“œ๋ฅผ ํšŒ์ˆ˜ํ•˜๊ณ  ํ•„์š”์—†์–ด์ง„ ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ์†Œ๋ฉธ์‹œํ‚จ๋‹ค. ์„œ๋ธ”๋ฆฟ์€ ์†Œ๋ฉธ์‹œํ‚ค์ง€ ์•Š๊ณ  ์žฌ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ WAS ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ .destory() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์†Œ๋ฉธ์‹œํ‚จ๋‹ค.

@WebServlet("/myServlet/*")
public class MyServlet extends HttpServlet{
    
    private static final long serialVersionUID = 1L;
    String msg = "default";
    
    // The servlet is initialized by calling the init() method.
    public void init() throws ServletException {
        msg = "init ๋ฉ”์†Œ๋“œ Call";
    }
    
    // The servlet calls service() method to process a client's request.
    // service () ๋ฉ”์†Œ๋“œ๋Š” HTTP ์š”์ฒญ ์œ ํ˜• (GET, POST, PUT, DELETE ๋“ฑ)์„ ํ™•์ธํ•˜๊ณ  
    // ์ ์ ˆํ•˜๊ฒŒ doGet, doPost, doPut, doDelete ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ
    @Override
    public void service(ServletRequest request, ServletResponse response) 
            throws ServletException, IOException {
        String text = "Service ๋ฉ”์†Œ๋“œ Call";
        if(!"default".equals(msg)){
            text = msg +"<br>"+text;
            msg = "default";
        }
        // response ๊ฐ์ฒด์— ํ—ค๋”๊ฐ’์„ ๊ธฐ๋กํ•œ๋‹ค.
        response.setContentType("text/plain");  
        response.setCharacterEncoding("UTF-8");
        
        // response ๊ฐ์ฒด์— ๋ฐ”๋””์— ๋„ฃ์„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค.
        response.getWriter().write(text);       
    }
    
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String text = "doGet ๋ฉ”์†Œ๋“œ Call";
        commonMethod(text, response);
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String text = "doPost ๋ฉ”์†Œ๋“œ Call";
        commonMethod(text, response);
    }
 
    // The servlet is terminated by calling the destroy() method.
    @Override
    public void destroy() {
        // Finalization code...
        System.out.println("destroy method call");
    }
}

 


# WAS์™€ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ

๊ธฐ๋ณธ์ ์œผ๋กœ Servlet์„ ๋งŒ๋“ค ๋•Œ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๊ฐ€ ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ผ๋Š”๊ฑฐ๋งŒ ์œ ์˜ํ•˜๋ฉด ์‹ฑ๊ธ€์“ฐ๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋“ฏ์ด ํŽธํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ ์ด์œ ๋Š” WAS์—์„œ ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ์ฒ˜๋ฆฌ๋ฅผ ๋Œ€์‹  ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์›น ์„œ๋น„์Šค์—์„œ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์š”์ฒญ์ด ๋™์‹œ์— ๋“ค์–ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ๋‹จ ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์›น ์„œ๋ฒ„์˜ ์„ฑ๋Šฅ์€ ๋งค์šฐ ๊ตฌ์งˆ ๊ฒƒ์ด๊ณ , ๋งŒ์•ฝ ํ•˜๋‚˜์˜ ์š”์ฒญ์— ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด ๋‹ค๋ฅธ ์ž‘์—…์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ๋ชปํ•ด ์„œ๋ฒ„๊ฐ€ ์ฃฝ์–ด๋ฒ„๋ฆด ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ WAS์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๋‹จ์ผ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์š”์ฒญ1์„ ์ฒ˜๋ฆฌํ•˜๋‹ค๊ฐ€ ์ง€์—ฐ๋œ๋‹ค๋ฉด, ์š”์ฒญ2๋„ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•˜๊ณ  ์„œ๋ฒ„๊ฐ€ ์ฃฝ์–ด๋ฒ„๋ฆฐ๋‹ค.

 

์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ• ๋‹น๋ฐ›๋Š” ์ž‘์—…์€ ์šด์˜์ฒด์ œ์—์„œ ๋ฐ”๋กœ ๋ฐ›๋˜, ์ž๋ฐ”์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋˜ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๋น„์‹ผ ์ž‘์—…์ด๋‹ค. ๊ทธ๋ž˜์„œ WAS ์„œ๋ฒ„์—๋Š” ์“ฐ๋ ˆ๋“œ ํ’€(pool)์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•  ์“ฐ๋ ˆ๋“œ๋ฅผ ์“ฐ๋ ˆ๋“œ ํ’€์— ๋ฏธ๋ฆฌ ์ƒ์„ฑ์‹œ์ผœ ๋’€๋‹ค๊ฐ€, ํ•„์š”ํ•  ๋•Œ ์„œ๋ธ”๋ฆฟ์— ์“ฐ๋ ˆ๋“œ๋ฅผ ํ• ๋‹น์‹œ์ผœ์ค€๋‹ค.

๋งŒ์•ฝ ์“ฐ๋ ˆ๋“œ ํ’€์— ๋‚จ์€๊ฒŒ ์—†๋‹ค๋ฉด, ๋Œ€๊ธฐํ•˜๊ฑฐ๋‚˜ ํ•ด๋‹น ์š”์ฒญ์„ ๊ฑฐ์ ˆ(=์š”์ฒญ ๊ฑฐ๋ถ€)ํ•ด๋ฒ„๋ฆฌ๋ฉด ๋œ๋‹ค.

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

 

 

'๐ŸŒฑBackend > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

#18 JSP Model1, Model2 (MVC)  (0) 2021.07.18
#17 Servlet Filter  (0) 2021.07.18
#15 JSP ์ฟ ํ‚ค์™€ ์„ธ์…˜  (0) 2021.07.16
#14. JSP์™€ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ  (0) 2021.07.16
#13 JSP (Java Server Pages)  (0) 2021.07.14

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ