์ž๋ฐ”ํŒŒ์ผ์—์„œ javax.servlet.Servlet ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์ž‘์„ฑ

spring์ด servlet stack๊ธฐ๋ฐ˜ ๊ธฐ์ˆ ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ž˜ ์•Œ์•„๋†“์•„์•ผํ•œ๋‹ค.

servlet ์ŠคํŽ™์„ ๊ตฌํ˜„ํ•œ Tomcat ์›น ์„œ๋ฒ„๋ฅผ ์„ค์น˜ํ•˜์—ฌ ํ™œ์šฉ ๊ฐ€๋Šฅ

 

Servlet์ด๋ž€

 

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์–ด๋–ค ์š”์ฒญ์„ ํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์ „์†กํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์—์„œ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„์‹œ ์‚ฌ์šฉํ•œ๋‹ค.

์„œ๋ธ”๋ฆฟ์€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ์ž๋ฐ” ํŒŒ์ผ์ด๋‹ค. 

 

์„œ๋ธ”๋ฆฟ์„ ๋™์ž‘์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์€ ์„œ๋ธ”๋ฆฟ API๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, 

์„œ๋ธ”๋ฆฟ์„ ๊ตฌํ˜„ํ•  ๋•Œ ํ•ด๋‹น API์ธ  javax.servlet.http.HttpServlet ๋งŒ ์ƒ์†(extends) ๋ฐ›์œผ๋ฉด ๋จ

 

๋ชจ๋“  ์ž๋ฐ” ํŒŒ์ผ์— extends HttpServlet์„ ์ž‘์„ฑํ•˜์—ฌ ํ•ด๋‹น java ํŒŒ์ผ์„ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋™์ž‘ํ•˜๋„๋ก ๊ตฌํ˜„

 

 

 

 

HttpServlet

HttpServlet์„ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋ฅผ Servlet ์ด๋ผ๊ณ  ํ•จ

HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ์— ๋”ฐ๋ผ doGet(), doPost(), doPut() ๋“ฑ ์š”์ฒญ ๋ฉ”์„œ๋“œ์— ๋งž๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜(Overriding)ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผํ•จ

 

doGet() : ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด HTTP GET ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ

doPost() : ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด HTTP POST ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ

๊ทธ ์™ธ ์ˆ˜๋ช…์ฃผ๊ธฐ์™€ ๊ด€๋ จ๋œ init(), destory()

 

class HelloServlet extends HttpServlet { ... }

 

 

Servlet์„ ์™œ ์“ฐ๋Š”์ง€

์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ์˜ํ•ด ์ƒ์„ฑ๋œ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋Š” ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— ๋ณด๊ด€ํ•œ๋‹ค. ์ดํ›„ ๋™์ผํ•œ ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์ด ์˜ฌ ๊ฒฝ์šฐ, 

๋ณด๊ด€ํ•˜๊ณ  ์žˆ๋˜ ์„œ๋ธ”๋ฆฟ์„ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ ์‹œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. 

 

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋“ค์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ 

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— ๋ณด๊ด€ํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ์š”์ฒญ์— ๋”ฐ๋ผ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋ฅผ ์žฌ์‹คํ–‰ ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•จ

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ์˜ ์š”์ฒญ์ด ์ตœ์ดˆ ์š”์ฒญ์ธ์ง€ ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ ํ•ด๋‹น ์š”์ฒญ์— ๋งž๋Š” ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด์˜ ์ƒ์„ฑ ์—ฌ๋ถ€๊ฐ€ ๊ฒฐ์ •๋จ

์ด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ์ค‘ ๋Œ€ํ‘œ์ ์œผ๋กœ Apache Tomcat์ด ์žˆ๋‹ค.

 

@WebServlet(urlPatterns="/first-request-servlet")
public class FirstHttpRequestServlet extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("request" + req);
		System.out.println("response" + resp);
	}
}

 

 

pom.xml

Maven์˜ ๋นŒ๋“œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ํŒŒ์ผ. POM(Project Object Model)์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ, ํ”„๋กœ์ ํŠธ ๋‚ด ๋นŒ๋“œ ์˜ต์…˜์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ๋„ ๋ณต๋ถ™์ด ๊ฐ€๋Šฅํ•˜์—ฌ, ํ˜‘์—…ํ•  ๋•Œ ๋ฒ„์ „ ํ†ต์ผํ•  ๋•Œ ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ์„ ํ•ด์•ผํ•  ๋•Œ dependency๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค.

<dependency>
	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>8.0.13</version>
</dependency>

 

web.xml

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„œ๋น„์Šค ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ๋‚ด์šฉ์ด ์ •์˜๋œ ํ™˜๊ฒฝ ์„ค์ • ํŒŒ์ผ.

์›น ์„œ๋ฒ„๋Š” web.xml์— ์ •์˜๋œ ๋‚ด์šฉ๋Œ€๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์„ ์ˆ˜ํ–‰ํ•จ

์„œ๋ฒ„๊ฐ€ ๊ธฐ๋™๋  ๋•Œ ์ด ํŒŒ์ผ์„ ์ฝ๋Š”๋‹ค. node.js ํ™˜๊ฒฝ์—์„œ package.json๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.

๋ชจ๋“  Web Application์€ ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ web.xml ํŒŒ์ผ์„ ๊ฐ€์ ธ์•ผํ•˜๊ณ , ์œ„์น˜๋Š” WEB-INFํด๋” ํ•˜์œ„์— ์žˆ๋‹ค.

web.xmlํŒŒ์ผ์˜ ์„ค์ •๋“ค์€ Web Application ์‹œ์ž‘ ์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋”ฉ๋œ๋‹ค.

Application์— ๋Œ€ํ•œ ์š”์ฒญ์ด ์ˆ˜์‹ ๋˜๋ฉด web.xml์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ์˜ url์„ ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ์ฝ”๋“œ์— ๋งคํ•‘ํ•œ๋‹ค.

<web-app>
	<!-- alias -->
    <servlet>
    	<servlet-name>title</servlet-name>
    	<servlet-class>servlets.Servlet</servlet-class>
    </servlet>
    
    <!-- mapping -->
    <servlet-mapping>
    	<servlet-name>title</servlet-name>
    	<url-pattern>title</url-pattern>
    </servlet-mapping>
</web-app>

 

 

WebServlet

@WebServlet(urlPatterns="/first-request-servlet")
public class FirstHttpRequestServlet extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("request" + request);
		System.out.println("response" + response);
		
		// ์š”์ฒญ๋ฉ”์„ธ์ง€ ์•ˆ์— ๋ญ๊ฐ€ ๋‹ด๊ฒผ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Œ
		request.getContextPath();
		request.getRequestURI();
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		// Header ๋ฉ”์‹œ์ง€ ๋‹ด๊ธด ์ •๋ณด ์ถ”์ถœ
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Request Information Example</title>");
		out.println("</head>");
		
		out.println("<body>");
		
		out.println("<h3>์š”์ฒญ ๋ฉ”์‹œ์ง€์— ๋‹ด๊ธด ์ •๋ณด ํ™•์ธ</h3>");
		
		// Header ๋ฉ”์‹œ์ง€์— ๋‹ด๊ธด ์ •๋ณด ์ถ”์ถœ
		out.println("Context Path : " + request.getContextPath() + "<br/>");
		out.println("Request URL : " + request.getRequestURL() + "<br/>");
		out.println("Request URI : " + request.getRequestURI() + "<br/>");
		out.println("Server Port : " + request.getServerPort() + "<br/>");
		out.println("Request Protocol : " + request.getProtocol() + "<br/>");
		out.println("Request METHOD: " + request.getMethod() + "<br/>");
		out.println("Query String : " + request.getQueryString() + "<br/>");// http://localhost:8080/step02servletsyntax/first-request-servlet?name=jerry&age=15
		out.println("Parameter(name) : " + request.getParameter("name") + "<br/>");
		out.println("Parameter(age) : " + request.getParameter("age") + "<br/>");
		
		out.println("</body>");
        out.println("</html>");
        
		out.close();
	}
}

 

์š”์ฒญ์— ๋‹ด๊ธด ์ •๋ณด๋“ค์„ ์„œ๋ฒ„๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๊บผ๋‚ด์•ผํ•จ

HttpServlet ๊ฐ์ฒด๊ฐ€ ์ด ์ •๋ณด๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , request์™€ response ์ •๋ณด๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

/*
 * 2. Annotation ๊ธฐ๋ฐ˜ ๋งตํ•‘
 * 
 * XML์„ ํ†ตํ•œ ๊ด€๋ จ ๋ฐฉ์‹์˜ ๋ฒˆ๊ฑฐ๋กœ์›€
 * ์ถ”์ƒํ™”๋˜๊ณ  ์ง๊ด€์ ์ธ Annotation ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๋งตํ•‘ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ„์†Œํ™”
 * 
 *  * @WebServlet
 * https://github.com/javaee/servlet-spec/blob/master/src/main/java/javax/servlet/annotation/WebServlet.java
 */
@WebServlet(name = "MySecondServlet", urlPatterns = "/servlets/secondservlet")
/*
 * web.xml์˜ ์•„๋ž˜ ์„ค์ •๊ณผ ๋™์ผํ•จ
 * <servlet-name>SecondServlet </servlet-name>
 * <url-pattern>/servlets/secondservlet</url-pattern>
 */
public class SecondeServlet extends HttpServlet {
	// get์š”์ฒญ์„ ํ†ตํ•ด hello second world๊ฐ€ ์ถœ๋ ฅ๋˜๋„๋ก ํ…Œ์ŠคํŠธ
	// ์„œ๋ฒ„ ์‘๋‹ต์‹œ HTTP Header์— ์ธ์ฝ”๋”ฉ์€ UTF-8๋กœ ์ฒ˜๋ฆฌ๋  ์ˆ˜ ์žˆ๋„๋ก ํ—ค๋” ์ฝ”๋“œ๋„ ์ถ”๊ฐ€
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("UTF-8");
		
		PrintWriter out = resp.getWriter();
		out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello Second World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello Second World!</h1>");
        out.println("</body>");
        out.println("</html>");
	}
}

 

 

Url-patterns

 

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Tomcat  (0) 2024.01.22
Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๋„๋ก ํ•˜๋Š” ๋ฒ•  (0) 2024.01.14
Logging | SLF4J  (0) 2024.01.14
React | export์™€ export default์˜ ์ฐจ์ด  (0) 2023.08.12
OCP  (0) 2022.02.21

+ Recent posts