๐ณ ์๋ธ๋ฆฟ์ด๋
- ์๋ฐ๋ฅผ ์ฌ์ฉํ์ฌ ์น์ ๋ง๋ค๊ธฐ ์ํด ํ์ํ ๊ธฐ์ ์ด๋ค.
- ํด๋ผ์ด์ธํธ๊ฐ ์ด๋ ํ ์์ฒญ์ ํ๋ฉด ๊ทธ์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ์ ์กํด ์ฃผ์ด์ผ ํ๋๋ฐ ์ด ์ญํ ์ ํ๋ ์๋ฐ ํ๋ก๊ทธ๋จ์ด๋ค.
ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ Servlet ํด๋์ค์ ๊ตฌํ ๊ท์น์ ์งํจ ์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ ๊ธฐ์
- ์๋ธ๋ฆฟ์ ํฐ์บฃ๊ณผ ๊ฐ์ ์น ์ ํ๋ฆฌ์ผ์ด์ ์๋ฒ (WAS)๋ฅผ ์ง์ ์ค์นํ๊ณ
- ๊ทธ ์์ ์๋ธ๋ฆฟ ์ฝ๋๋ฅผ ํด๋์ค ํ์ผ๋ก ๋น๋ํด์ ์ฌ๋ฆฐ ๋ค์ WAS ์คํํ๋ฉด ๋์ง๋ง, ์ด๋ฌํ ๊ณผ์ ์ ๋ฒ๊ฑฐ๋กญ๋ค.
- ์คํ๋ง ๋ถํธ๋ ํฐ์บฃ ์๋ฒ๋ฅผ ๋ด์ฅํ๊ณ ์์ด์ ํฐ์บฃ ์๋ฒ ์ค์น ์์ด ํธ๋ฆฌํ๊ฒ ์๋ธ๋ฆฟ ์ฝ๋๋ฅผ ์คํํ ์ ์๋ค.
๐ข ์คํ๋ง ๋ถํธ์์ ์๋ธ๋ฆฟ ์ฌ์ฉํ๊ธฐ
- @ServletComponentScan์ ์ฌ์ฉํ๋ฉด ์คํ๋ง ๋ถํธ์์ ์๋ธ๋ฆฟ์ ์ง์ ๋ฑ๋กํด์ ์ฌ์ฉํ ์ ์๋ค.
@ServletComponentScan //์๋ธ๋ฆฟ ์๋ ๋ฑ๋ก
@SpringBootApplication
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
- ์ค์ ์๋ธ๋ฆฟ ๋ฑ๋กํ๊ธฐ
@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HelloServlet.service");
System.out.println("request = " + request);
System.out.println("response = " + response);
String username = request.getParameter("username");
System.out.println("username = " + username);
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
response.getWriter().write("hello " + username);
}
}
- ์๋ธ๋ฆฟ์ด ํธ์ถ๋๋ฉด service ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉฐ ์คํ๋๋ค.
๐ฟ ์๋ธ๋ฆฟ ์ปจํ ์ด๋ ๋์ ๋ฐฉ์ ์ค๋ช

- ์คํ๋ง ๋ถํธ๋ฅผ ์คํํ๋ฉด ์คํ๋ง ๋ถํธ๊ฐ ๋ด์ฅ ํฐ์บฃ ์๋ฒ๋ฅผ ๋์์ค๋ค.
- ์ด ํฐ์ผ ์๋ฒ๋ ๋ด๋ถ์ ์๋ธ๋ฆฟ ์ปจํ ์ด๋ ๊ธฐ๋ฅ์ ๊ฐ์ง๊ณ ์๊ณ , ๊ทธ ์๋ธ๋ฆฟ ์ปจํ ์ด๋๋ฅผ ํตํด ์๋ธ๋ฆฟ์ ์์ฑํด ์ค๋ค.
- helloServelt ์ด ์์ฑ๋๊ณ

- url ์ ๋ ฅ์ฐฝ์ localhost:8081/hello?username=kim์ด๋ผ๊ณ ์ ๋ ฅํ๋ฉด
- ์น ๋ธ๋ผ์ฐ์ ๊ฐ ์ผ์ชฝ๊ณผ ๊ฐ์ด ์์ฒญ ๋ฉ์์ง๋ฅผ ์์ฑํด์ ์๋ฒ์ ๋์ ธ ์ค๋ค.

- ์๋ฒ์ ์์ฒญ์ด ์ค๋ฉด http ์์ฒญ ๋ฉ์์ง๋ฅผ ๊ธฐ๋ฐ์ผ๋ก request, response ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋๊ณ
- ์ฑ๊ธํค์ผ๋ก ๋ ์๋ helloServlet์ ์๋ service( ) ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด์ request, response๋ฅผ ๋๊ฒจ์ฃผ๋ฉด์ helloServlet์ ํธ์ถํด ์ค๋ค.
- ๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๊ฐ response ๋ฐ์ดํฐ์๋ค๊ฐ ์ ๋ณด๋ฅผ ๋ฃ์ด์ฃผ๋ฉด helloServlet ์ด ์ข ๋ฃ๋๊ณ ๋๊ฐ๋ฉด์ WAS ์๋ฒ๊ฐ ์ด response ๊ฐ์ฒด ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ http ์๋ต ๋ฉ์์ง๋ฅผ ๋ง๋ค์ด ๋ฐํํด ์ค๋ค.
<๊น์ํ - '์คํ๋ง MVC 1ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์ ' ์ ์ฐธ๊ณ ํ์์ต๋๋ค.>
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [spring] HTTP ์์ฒญ ๋ฐ์ดํฐ (0) | 2023.02.22 |
|---|---|
| [spring] HttpServletRequest (0) | 2023.02.21 |
| [spring] ์น ์ ํ๋ฆฌ์ผ์ด์ ์ดํด (0) | 2023.02.18 |
| [spring] ๋น ์ค์ฝํ (2) (0) | 2023.02.12 |
| [spring] ๋น ์ค์ฝํ (1) (0) | 2023.02.07 |