๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

GenericServlet์„ ์ƒ์†ํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ ๋ฐ ๋ฐฐ์น˜

by Leica 2020. 2. 8.
๋ฐ˜์‘ํ˜•

GenericServlet์„ ์ƒ์†ํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ ๋ฐ ๋ฐฐ์น˜

 

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ์„œ๋ธ”๋ฆฟ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” Servlet ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Servlet์„ ๊ตฌํ˜„ํ•˜๋Š” GenericServlet ์ถ”์ƒ ํด๋ž˜์Šค์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

  • ์ง€๋‚œ ํฌ์ŠคํŒ…

- Servlet ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ธฐ๋ณธ ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ ๋ฐ ๋ฐฐ์น˜


Servlet ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๋ ค๋ฉด ๋‹ค์„ฏ ๊ฐœ์˜ ๋ฉ”์†Œ๋“œ์ธ init(), service(), destroy(), getServiceConfig(), getServiceInfo()๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์„œ๋ธ”๋ฆฟ์„ ๊ตฌํ˜„ํ• ๋•Œ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” service() ํ•˜๋‚˜ ๋ฟ์ด๋‹ค. ๋‚˜๋จธ์ง€ ๋ฉ”์†Œ๋“œ๋“ค์€ ํ•„์ˆ˜์ ์ด์ง€ ์•Š๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜๋จธ์ง€ ๋ฉ”์†Œ๋“œ๋“ค์—์„œ๋Š” ๋”ฑํžˆ ํ•  ์ž‘์—…์ด ์—†๋”๋ผ๋„ ๋นˆ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ๋‹ค. GenericServlet์€ service()๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ 4๊ฐœ ๋ฉ”์†Œ๋“œ์ธ init(), destroy(), getServiceConfig(), getServiceInfo() ๊ฐ€ ๋ฏธ๋ฆฌ ๊ตฌํ˜„๋˜์–ด ์žˆ์–ด ์ด๋Ÿฐ ๋ถˆํŽธํ•จ์„ ํ•ด์†Œํ•ด์ค€๋‹ค. service()๋งŒ ๋ณ„๋„๋กœ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

1. GenericServlet์„ ์ƒ์†ํ•˜๋Š” ๊ณ„์‚ฐ๊ธฐ ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class CalculatorServlet extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        
    }
}

javax.servlet.GenericServlet์„ ์ƒ์†ํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

GenericServlet์€ ์ถ”์ƒ ํด๋ž˜์Šค์ด๋ฏ€๋กœ service()๋ฅผ overrideํ•ด์•ผ ํ•œ๋‹ค.

service()๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ์‹ค์งˆ์ ์ธ ์„œ๋น„์Šค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

service()์— ๊ฐ„๋‹จํžˆ ํ”ผ์—ฐ์‚ฐ์ž 2๊ฐœ๋ฅผ ๋ฐ›์•„ ์‚ฌ์น™์—ฐ์‚ฐ๊ณผ % ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋‹ค.

 

@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    int operand1 = Integer.parseInt(servletRequest.getParameter("operand1"));
    int operand2 = Integer.parseInt(servletRequest.getParameter("operand2"));
}

service()์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ค‘ ServletRequest ๊ฐ์ฒด๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์ •๋ณด๋ฅผ ๋‹ค๋ฃฐ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

ServletRequest์˜ getParameter()๋Š” GET/POST ์š”์ฒญ์œผ๋กœ ๋“ค์–ด์˜จ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ๊บผ๋‚ผ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

์ธ์ž๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ช…์„ ๋„˜๊ธด๋‹ค.

 

@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    int operand1 = Integer.parseInt(servletRequest.getParameter("operand1"));
    int operand2 = Integer.parseInt(servletRequest.getParameter("operand2"));

    servletResponse.setContentType("text/plain");
    servletResponse.setCharacterEncoding("UTF-8");
}

ServletResponse ๊ฐ์ฒด๋Š” ์‘๋‹ต ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

ServletResponse์˜ setContentType()์€ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹๊ณผ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ์ง€์ •ํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” ์ด๋ฅผ ๋ณด๊ณ  ๊ทธ ํ˜•์‹์— ๋งž์ถฐ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ™”๋ฉด์— ์ถœ๋ ฅ(Rendering)ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด HTML ํ˜•์‹์ด๋ฉด ํƒœ๊ทธ ๊ทœ์น™์— ๋งž์ถฐ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ณ  XML ํ˜•์‹์ด๋ฉด ๊ฐ ํƒœ๊ทธ๋ฅผ ํŠธ๋ฆฌ ๋…ธ๋“œ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

 

setContentType("text/plain")์€ ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ„๋„์˜ ๋ฉ”ํƒ€์ •๋ณด๊ฐ€ ์—†๋Š” ์ˆœ์ˆ˜ํ•œ ํ…์ŠคํŠธ์ž„์„ ์ง€์ •ํ•œ๋‹ค.

 

setCharacterEncoding()์€ ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ์˜ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ์ง€์ •ํ•œ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ณ  setContentType()์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ๊ป˜ ๋„˜๊ฒจ๋„ ๋œ๋‹ค.

 

setContentType("text/plain;charset=UTF-8")

 

๋ฌธ์ž ์ง‘ํ•ฉ์„ ์ง€์ •ํ•˜์ง€ ์•Š์•„ ํ•œ๊ธ€์ด ๊นจ์ง„ HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€(Fiddler)

๊ธฐ๋ณธ ๋ฌธ์ž ์ง‘ํ•ฉ์€ ISO-8859-1์ด๋‹ค. ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด UTF-8๋กœ ์ง€์ •ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ• ๋•Œ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’์„ UTF-8 ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ๋ฌธ์ž ์ง‘ํ•ฉ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ํ•œ๊ธ€์ด ๊นจ์ ธ์„œ ์ถœ๋ ฅ๋œ๋‹ค.

 

@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    int operand1 = Integer.parseInt(servletRequest.getParameter("operand1"));
    int operand2 = Integer.parseInt(servletRequest.getParameter("operand2"));

    servletResponse.setContentType("text/plain");
    servletResponse.setCharacterEncoding("UTF-8");

    PrintWriter writer = servletResponse.getWriter();
    writer.println("operand1=" + operand1 + ", operand2" + operand2 + "์˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.");
    writer.println(operand1 + " + " + operand2 + " = " + (operand1 + operand2));
    writer.println(operand1 + " - " + operand2 + " = " + (operand1 - operand2));
    writer.println(operand1 + " * " + operand2 + " = " + (operand1 * operand2));
    writer.println(operand1 + " / " + operand2 + " = " + ((double) operand1 / (double) operand2));
    writer.println(operand1 + " % " + operand2 + " = " + (operand1 % operand2));
}

ServletResponse์˜ getWriter()๋Š” ํด๋ผ์ด์–ธํŠธ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž ๊ธฐ๋ฐ˜ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ง€๊ธˆ์€ ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•  ๊ฒƒ์ด๋ฏ€๋กœ getWriter()๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋ฉฐ ์ด๋ฏธ์ง€๋‚˜ ๋™์˜์ƒ๊ฐ™์€ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ๋ฐ”์ดํŠธ ๊ธฐ๋ฐ˜์˜ getOutputStream()์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

โ€ป ์ฃผ์˜
setContentType()๊ณผ setCharacterEncoding()์€ getWriter()๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— ํ˜ธ์ถœํ•ด์•ผ ์œ ๋‹ˆ์ฝ”๋“œ๊ฐ€ ์ง€์ •๋œ ๋ฌธ์ž ์ง‘ํ•ฉ์œผ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ ์ž‘์„ฑํ•œ CalculatorServlet์˜ ์ „์ฒด ํŒŒ์ผ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (import๋ฌธ ์ œ์™ธ)

 

public class CalculatorServlet extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        int operand1 = Integer.parseInt(servletRequest.getParameter("operand1"));
        int operand2 = Integer.parseInt(servletRequest.getParameter("operand2"));
    
        servletResponse.setContentType("text/plain");
        servletResponse.setCharacterEncoding("UTF-8");
    
        PrintWriter writer = servletResponse.getWriter();
        writer.println("operand1=" + operand1 + ", operand2" + operand2 + "์˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.");
        writer.println(operand1 + " + " + operand2 + " = " + (operand1 + operand2));
        writer.println(operand1 + " - " + operand2 + " = " + (operand1 - operand2));
        writer.println(operand1 + " * " + operand2 + " = " + (operand1 * operand2));
        writer.println(operand1 + " / " + operand2 + " = " + ((double) operand1 / (double) operand2));
        writer.println(operand1 + " % " + operand2 + " = " + (operand1 % operand2));
    }
}

2. ๋ฐฐ์น˜ ๋ฐ ํ…Œ์ŠคํŠธ

web.xml์— CalculatorServlet์˜ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

<servlet>
    <servlet-name>Calculator</servlet-name>
    <servlet-class>com.atoz_develop.servelets.CalculatorServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Calculator</servlet-name>
    <url-pattern>/calc</url-pattern>
</servlet-mapping>

 

์„œ๋ฒ„ ์žฌ๊ธฐ๋™ ํ›„ URL์— operand1๊ณผ operand2์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ถ™์—ฌ ์š”์ฒญํ•œ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

์ฐธ๊ณ  - @WebServlet ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด ์„ค์ •

์œ„์—์„œ๋Š” web.xml์— ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ Servlet 3.0๋ถ€ํ„ฐ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด์„œ ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

@WebServlet("/calc")
public class CalculatorServlet extends GenericServlet {
	//...
}

์œ„์™€ ๊ฐ™์ด ์„ค์ •ํ•˜๊ณ  web.xml์„ ์›๋ณตํ•œ ๋’ค ์žฌ์‹คํ–‰ํ•˜๋ฉด ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€