171114 Servlet - RYUDONGJIN/Memo_wiki GitHub Wiki

Servlet

  • ๋™์  ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปดํฌ๋„ŒํŠธ
  • .java ํ™•์žฅ์ž
  • ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋™์ ์œผ๋กœ ์ž‘๋™ํ•˜๊ณ , ์‘๋‹ต์€ html์„ ์ด์šฉ.
  • java thread ์ด์šฉํ•˜์—ฌ ๋™์ž‘
  • MVCํŒจํ„ด์—์„œ Controller๋กœ ์ด์šฉ.

1. web.xml์„ ์ด์šฉํ•œ ๋งคํ•‘
<servlet-name> : ์ž„์˜์˜ ์ด๋ฆ„์„ ๋งŒ๋“ค์–ด์ค€๋‹ค
<sevlet-class> : ๋งคํ•‘ํ•  ํด๋ž˜์Šค ํŒŒ์ผ๋ช…์„ ํŒจํ‚ค์ง€๋ช…์„ ํฌํ•จํ•˜์—ฌ ์ •ํ™•ํ•˜๊ฒŒ ์ž…๋ ฅํ•œ๋‹ค.
<url-pattern> : servlet-class์˜ ํด๋ž˜์Šค๋ฅผ ๋งคํ”ผํ•  ์ž„์˜์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•œ๋‹ค. '/'๋กœ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค.

2. ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ ๋งคํ•‘ 
@WebServlet("/")
<?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_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>hellojsp</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>helloworld</servlet-name>
  	<servlet-class>com.javalec.ex.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>helloworld</servlet-name>
  	<url-pattern>/hw</url-pattern>
  </servlet-mapping>
</web-app>

Servlet ๋ผ์ดํ”„์‚ฌ์ดํด(์ƒ๋ช…์ฃผ๊ธฐ)

  • Servlet์€ ์ตœ์ดˆ ์š”์ฒญ์‹œ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ ธ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋”ฉ๋˜๊ณ  ์ดํ›„ ์š”์ฒญ์‹œ์—๋Š” ๊ธฐ์กด์˜ ๊ฐ์ฒด๋ฅผ ์žฌํ™œ์šฉ.
  1. Servlet ๊ฐ์ฒด ์ƒ์„ฑ (์ตœ์ดˆ ํ•œ๋ฒˆ)
  • ์„ ์ฒ˜๋ฆฌ : @PostConstruct [init()์‹คํ–‰ ์ „]
  1. Init() ํ˜ธ์ถœ (์ตœ์ดˆ ํ•œ๋ฒˆ)
  2. service(), doGet(), doPost() ํ˜ธ์ถœ (์š”์ฒญ์‹œ ๋งค๋ฒˆ)
  3. destroy() ํ˜ธ์ถœ (๋งˆ์ง€๋ง‰ ํ•œ๋ฒˆ_servlet ์ˆ˜์ •, ์„œ๋ฒ„ ์žฌ๊ฐ€๋™ ๋“ฑ๋“ฑ)
  • ํ›„์ฒ˜๋ฆฌ : @PreDestroy [destroy()์‹คํ–‰ ํ›„]

โš ๏ธ **GitHub.com Fallback** โš ๏ธ