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

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

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

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

 

1. ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ

์ž๋ฐ” ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋Š” javax.servlet.Servlet ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. Servlet์€ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•ด ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ init(), service(), destroy(), getServiceConfig(), getServiceInfo()๋ฅผ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.

public class FirstServlet implements Servlet {
	ServletConfig config;

	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("init() called");
		this.config = config;
	}
	
	@Override
	public void destroy() {
		System.out.println("destroy() called");
	}
	
	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println("service() called");
	}

	@Override
	public ServletConfig getServletConfig() {
		System.out.println("getServiceConfig() called");
		return this.config;
	}

	@Override
	public String getServletInfo() {
		System.out.println("getServletInfo() called");
		return "version=1.0;author=butterfield;copyright=butterfield 2020";
	}
}

 

๋ฉ”์†Œ๋“œ ๊ตฌ๋ถ„
์„ค๋ช…
init() Lifecycle ๊ด€๋ จ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์„œ๋ธ”๋ฆฟ์„ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ
์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์ „์— ํ•ด์•ผํ•  ์ค€๋น„ ์ž‘์—…์„ ์ž‘์„ฑํ•œ๋‹ค.
์˜ˆ: DB ์—ฐ๊ฒฐ, ์›๊ฒฉ ์„œ๋ฒ„ ์—ฐ๊ฒฐ, ํ”„๋กœํผํ‹ฐ ๋กœ๋”ฉ ๋“ฑ
service() ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
์‹ค์งˆ์ ์ธ ์„œ๋น„์Šค ์ž‘์—…์„ ์ˆ˜ํ–‰
destroy() ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์„ ๋น„ํ™œ์„ฑํ™” ์‹œํ‚ฌ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
์ž์› ํ•ด์ œ, ๋ฐ์ดํ„ฐ ์ €์žฅ ๋“ฑ์˜ ๋งˆ๋ฌด๋ฆฌ ์ž‘์—…์„ ์ž‘์„ฑํ•œ๋‹ค.
getServiceConfig() ๋ณด์กฐ ๋ฉ”์†Œ๋“œ ์„œ๋ธ”๋ฆฟ ์„ค์ • ์ •๋ณด๋ฅผ ๋‹ค๋ฃจ๋Š” ServletConfig ๊ฐ์ฒด ๋ฐ˜ํ™˜
ServletConfig๋ฅผ ํ†ตํ•ด ์„œ๋ธ”๋ฆฟ ์ด๋ฆ„, ์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’, ์„œ๋ธ”๋ฆฟ ํ™˜๊ฒฝ ์ •๋ณด ๋“ฑ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
getServiceInfo() ์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ์ž, ๋ฒ„์ „, ์ €์ž‘๊ถŒ ๋“ฑ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜

 

2. ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด ์ž‘์„ฑ

โ€ป Servlet 3.0๋ถ€ํ„ฐ๋Š” @WebServlet ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•ด์„œ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

Deployment descriptor ํŒŒ์ผ(web.xml)์— ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>web03</display-name>
  
  <!-- ์„œ๋ธ”๋ฆฟ ์„ ์–ธ -->
  <servlet>
  	<servlet-name>FirstServlet</servlet-name>
  	<servlet-class>atoz_develop.com.FirstServlet</servlet-class>
  </servlet>
  
  <!-- ์„œ๋ธ”๋ฆฟ์„ URI๊ณผ ์—ฐ๊ฒฐ -->
  <servlet-mapping>
  	<servlet-name>FirstServlet</servlet-name>
  	<url-pattern>/FirstServlet</url-pattern>
  </servlet-mapping>
  
  <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>
</web-app>

 

web.xml์€ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ๊ฐ–๋Š”๋‹ค. web.xml์— ์„œ๋ธ”๋ฆฟ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค. ํด๋ผ์ด์–ธํŠธ์—์„œ ์‹คํ–‰์„ ์š”์ฒญํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

 

์„œ๋ธ”๋ฆฟ ์„ ์–ธ

<!-- ์„œ๋ธ”๋ฆฟ ์„ ์–ธ -->
<servlet>
	<servlet-name>FirstServlet</servlet-name>
	<servlet-class>atoz_develop.com.FirstServlet</servlet-class>
</servlet>

<servlet>์— ์„œ๋ธ”๋ฆฟ name๊ณผ class๋ฅผ ์ง€์ •ํ•œ๋‹ค.

์„œ๋ธ”๋ฆฟ name์€ ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ๋™์ผํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฉฐ ๊ณต๋ฐฑ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

์„œ๋ธ”๋ฆฟ class๋Š” ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ํฌํ•จํ•œ QName(Fully qualified name)์„ ์ž‘์„ฑํ•œ๋‹ค.

 

์„œ๋ธ”๋ฆฟ URL ๋งคํ•‘

<!-- ์„œ๋ธ”๋ฆฟ์„ URI๊ณผ ์—ฐ๊ฒฐ -->
<servlet-mapping>
	<servlet-name>FirstServlet</servlet-name>
	<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>

ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” URL๋กœ ์„œ๋ธ”๋ฆฟ์˜ ์‹คํ–‰์„ ์š”์ฒญํ•œ๋‹ค.

<servlet-mapping>์— ์„œ๋ธ”๋ฆฟ URL ๋งคํ•‘ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

<servlet-name>์— ์œ„์—์„œ ์ •์˜ํ•œ ์„œ๋ธ”๋ฆฟ name์„ ์ง€์ •ํ•œ๋‹ค.

<url-pattern>์— URL์„ ์ง€์ •ํ•˜๋Š”๋ฐ /FirstServlet๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด '/'๋Š” context root, ์ฆ‰ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฃจํŠธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

์ดํด๋ฆฝ์Šค Project Explorer์˜ ํ”„๋กœ์ ํŠธ Deployment Descriptor ๋…ธ๋“œ์— web.xml์— ์ž‘์„ฑํ•œ ๋ฐฐ์น˜ ์ •๋ณด๊ฐ€ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์‹œ๊ฐํ™”๋˜์–ด ํ‘œ์‹œ๋œ๋‹ค.

 

์„œ๋ธ”๋ฆฟ ์‹คํ–‰

์œ„์™€ ๊ฐ™์ด ์„œ๋ธ”๋ฆฟ๊ณผ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ–ˆ์œผ๋ฉด

์›น ๋ธŒ๋ผ์šฐ์ € ์ฃผ์†Œ์ฐฝ์— http://localhost:ํฌํŠธ๋ฒˆํ˜ธ/์ปจํ…์ŠคํŠธ ๋ฃจํŠธ/FirstServlet ์„ ์ž…๋ ฅํ•˜์—ฌ ์„œ๋ฒ„์— ์š”์ฒญํ•œ๋‹ค.

 

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

 

3. ์„œ๋ธ”๋ฆฟ ๊ตฌ๋™ ์ ˆ์ฐจ

1) ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ฐพ๋Š”๋‹ค.

2) ์„œ๋ธ”๋ฆฟ์ด ์—†์œผ๋ฉด ํด๋ž˜์Šค๋ฅผ ๋กœ๋”ฉํ•˜๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ์ธ init()์„ ํ˜ธ์ถœํ•œ๋‹ค.

3) ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” service() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

4) service() ๊ฒฐ๊ณผ๋ฅผ HTTP์— ๋งž์ถฐ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•˜์—ฌ ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ์™„๋ฃŒํ•œ๋‹ค.

5) ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋˜๊ฑฐ๋‚˜ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋˜๋ฉด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” ์ข…๋ฃŒ๋˜๊ธฐ ์ „์— ์„œ๋ธ”๋ฆฟ์ด ๋งˆ๋ฌด๋ฆฌ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒ์„ฑ๋œ ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•ด destroy() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

์„œ๋ธ”๋ฆฟ์„ ์ตœ์ดˆ๋กœ ์š”์ฒญํ•œ ํ›„ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ 3๋ฒˆ ์ƒˆ๋กœ๊ณ ์นจ์„ ํด๋ฆญํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด service()๋งŒ ํ˜ธ์ถœ๋œ๋‹ค. ์ด๋ฏธ FirstServlet ์ธ์Šคํ„ด์Šค๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ init()์€ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๊ฒƒ์ด๋‹ค.

 

์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋Š” ๋‹จ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋˜๋ฉฐ ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ์œ ์ง€๋˜๋ฉฐ ์‚ฌ์šฉ๋œ๋‹ค.

 

ํ†ฐ์บฃ์„ ์ข…๋ฃŒํ•˜๋ฉด destroy()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€