SpringBoot Part3 (1)
Web์ ๊ตฌ์ฑ

URI : ์น์ ๊ตฌ์ฑํ๋ ์ ๋ณด, ๋ฆฌ์์ค๋ค์ ์๋ณํ๊ธฐ์ํ ์๋ณ์

- ์๋ ๊ฒฝ๋ก, ์ ๋ ๊ฒฝ๋ก (์์์ /foo/bar)
์๋ ๊ฒฝ๋ก ๊ฒฐ๋ ๊ฒฝ๋ก(/๋ก ์์) hoge (./huge) /foo/bar/hoge hoge/fuga /foo/bar/hoge/fuga ../hoge /foo/hoge ../../foge /foge - ./ โ ํ์ฌ๊ฒฝ๋ก ../ โ ๋ถ๋ชจ๊ฒฝ๋ก
- ํน์ ์์น๋ก ์ฎ๊ธธ๋ ์๋๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค : ์ ๋๊ฒฝ๋ก๋ฅผ ์ด์ฉํ๋ฉด ๋๋ฉ์ธ๊น์ง ์ฌ๋ผ๊ฐ ์๋ชป๋ ๋ฆฌ์์ค์ ์ ๊ทผํ ์ํ์ด ์๋ค.
- ์ฌ์ฉํ ์ ์๋ ๋ฌธ์ (ASCII ๋ฌธ์) : ์์ด, ์ซ์, ํน์ ๊ธฐํธ
HTTP


- ํด๋ผ์ด์ธํธ โ ์น ๋ธ๋ผ์ฐ์ โ OS โ ํธ์คํธ ์๋ฒ
- ํน์ง
- TCP/IP ๊ธฐ๋ฐ
- ์์ฒญ/์๋ตํ ํ๋กํ ์ฝ
- ๋๊ธฐํ ํ๋กํ ์ฝ
- stateless

https://developer.mozilla.org/ko/docs/Web/HTTP/Overview
HTTP ๊ฐ์ - HTTP | MDN
HTTP๋ HTML ๋ฌธ์์ ๊ฐ์ ๋ฆฌ์์ค๋ค์ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํด์ฃผ๋ ํ๋กํ ์ฝ์ ๋๋ค. HTTP๋ ์น์์ ์ด๋ฃจ์ด์ง๋ ๋ชจ๋ ๋ฐ์ดํฐ ๊ตํ์ ๊ธฐ์ด์ด๋ฉฐ, ํด๋ผ์ด์ธํธ-์๋ฒ ํ๋กํ ์ฝ์ด๊ธฐ๋ ํฉ๋๋ค. ํด๋ผ์ด์ธํธ-์๋ฒ
developer.mozilla.org
HTML
Hyper Text : ํ ์คํธ ์์ ๋งํฌ๊ฐ ์์ด ์ฐ๊ฒฐ์ฑ์ด ์๋ ํ ์คํธ
Makup : ๊ตฌ์กฐ์ ํํ์ด ๊ฐ๋ฅํ ๊ท์น
์น์ ๊ธฐ์ ์ ํน์ง
- ํ์ดํผ๋ฏธ๋์ด ์์คํ : ๋ค์ํ ๋ฏธ๋์ด๊ฐ ํ์ดํผ ๋งํฌ๋ก ์ฐ๊ฒฐ๋์ด์๊ณ ๊ตฌ์ฑ๋ ์์คํ
- ๋ถ์ฐ ์์คํ
Web Application Archietcture


caching service โ ์์ฃผ ์ฐธ์กฐ๋๋ ๋ฐ์ดํฐ
cloud storage โ ๋๋ ๊ฐ๊น์ด chech server?
https://developer.mozilla.org/ko/docs/Web/HTTP/Overview
(http)์น ์๋ฒ ๐ ์น ์ดํ๋ฆฌ์ผ์ด์ ์๋ฒ (WAS)
๋์ ์ปจํ ์ธ ๋ฅผ ์ ๊ณตํ๋์ ์ ๋ฌด ์ฐจ์ด

: ์ ์ ํ์ผ์ ๋ก๋ํ๋ ๊ฒ ์ธ์ DB์กฐํ, ๋ก์ง ์ฒ๋ฆฌ ๋ฑ ๋์ ์ปจํ ์ธ ์ ๊ณต
: WAS โ ๋ณ๋ ํ๋ก์ธ์ค, ์คํ์์ค WAS๋ก ํ์บฃ,์ ํฐ ๋ฑ

: ์น ํด๋ผ์ด์ธํธ โ ์น์๋ฒ(์ํ์น, ์์ง์์ค ๋ฑ) โ ์น ์ดํ๋ฆฌ์ผ์ด์ โ DB
: ์น์๋ฒ๋ฅผ ํ๋ก์ ์๋ฒ๋ก ์ฌ์ฉํ๋ ํํ
Servlet

: http ์์ฒญ์ ๋ํด ํน์ ๋ก์ง์ ์ํํ์ฌ ์๋ต์ ํด์ฃผ๋ ์ปดํฌ๋ํธ
: WAS๊ฐ ํธ์ถํด์ค ์ ์๊ฒ ์ ํด๋ ์ผ์ข ์ format


: MVC ํจํด์์ Contoller์ ์ญํ
(JSP๋ view ์ญํ )
Servlet์ life cycle

- init() : ์๋ฒ๊ฐ ์ฐ๊ฒฐ๋๊ณ ํ๋ฒ๋ง ์ํ๋จ. ๋ฉํฐ์ค๋๋ ํ๊ฒฝ์์ ๋งค ์์ฒญ์ servlet์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ฒ์ด์๋ service ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ.
- service() : ํ๋์ ์์ฒญ thread์ ์ํด ์คํ๋๋ฉฐ, request๋ฅผ ํ์ธํ์ฌ doGet ํน์ doPost๋ฅผ ํธ์ถํ๋ค.
- doGet() / doPost()
์ค์ต
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
--
public class TestServlet extends HttpServlet {
private static final Logger logger = LoggerFactory.getLogger(TestServlet.class);
@Override
public void init() throws ServletException {
super.init();
logger.info("init Servlet");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
var requestURI = req.getRequestURI();
logger.info("Got Request form {}", requestURI);
// ์๋ต ์ ๋ฌ
var writer = resp.getWriter();
writer.println("Hello Servlet");
}
}
WAS(ํฐ์บฃ)์ servlet ๋ฑ๋ก
: ํฐ์บฃ ์๋ฒ ๊ตฌ๋ํ web container์ servlet์ด ์กด์ฌํ๋ค๋ ๊ฒ์ ์๋ ค์ฃผ๊ธฐ
- Web.xml์ ์ด์ฉํ์ฌ ๋ฑ๋ก (src > webapp > web.xml )
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.prgrms.kdt.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
- ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ์ฌ ๋ฑ๋ก
@WebServlet(value = "/*", loadOnStartup = -1) // 1์ด๋ฉด ์ด๋ฏธ init
public class TestServlet extends HttpServlet {
private static final Logger logger = LoggerFactory.getLogger(TestServlet.class);
@Override
public void init() throws ServletException {
super.init();
logger.info("init Servlet");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
var requestURI = req.getRequestURI();
logger.info("Got Request form {}", requestURI);
var writer = resp.getWriter();
writer.println("Hello Servlet");
}
}
- spring ์ธํฐํ์ด์ค WebApplicationInitializer ์ด์ฉ
public class KdtWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
var servletRegistration = servletContext.addServlet("test",new TestServlet());
servletRegistration.addMapping("/*");
servletRegistration.setLoadOnStartup(-1);//default
}
}
: ์ ํ ํํ ์ฝ๋๋ฅผ ์์ฑํ ๋ ์ฉ์ดํ๋ค.
: Spring MVC ํ๋ก์ ํธ์์๋ ๋ณดํต WebApplicationInitializer๋ฅผ ๊ตฌํํด์ค๋ค.
โ Spring์ Servlet์ (๋ค์ด๋ ํธ๋ก ์ฐ์ง์๊ณ ) ์ถ์ํ์ํจ Spring MVC๋ฅผ ์ ๊ณตํ๋ค. ํด๋น ๋ฐฉ์์ ํฐ์บฃ WAR์ ๋ํ๋ก์ดํ๋ ๋ฐฉ์์ผ๋ก ์ฌ๋ํ ๋ฐฉ์์ด๋ค.
SpringBoot์์ Embaded Tomcat์ ์ฌ์ฉํ ๊ฒ. ๊ทธ๋ was์ ๋ฐฐํฌํ ์ผ์ด ๊ฑฐ์ ์๋ค.
๋ชจ๋ ์ํคํ ์ฒ ํ๊ฒฝ์์๋ Embaded Tomcat์ผ๋ก ๋ง์์ jar๋ก ๋ฐฐํฌํ๋ ์ผ์ด์ค๊ฐ ๋๋ถ๋ถ.
IntelliJ ํฐ์บฃ ๋ฐฐํฌ
tomcat ์๋ฒ ์ถ๊ฐ

์ํฐํฉํธ ์ถ๊ฐ


ํฐ์บฃ์ ๋ฐฐํฌํ๋ฉด out ํด๋ ์์ฑ
์ถ์ฒ - ํด๋ฆฌ : SpringBoot Part3
๋๊ธ