From 45e1e78527e8434a1a6364776f83821ab2419f8c Mon Sep 17 00:00:00 2001 From: haerong22 Date: Sun, 28 Mar 2021 23:39:39 +0900 Subject: [PATCH] =?UTF-8?q?spring=20mvc=20:=20servlet=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/servlet/ServletApplication.java | 2 + .../example/servlet/basic/HelloServlet.java | 30 ++++++++++++++ .../src/main/resources/application.properties | 1 - spring-mvc/servlet/src/main/webapp/basic.html | 41 +++++++++++++++++++ spring-mvc/servlet/src/main/webapp/index.html | 12 ++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 spring-mvc/servlet/src/main/java/com/example/servlet/basic/HelloServlet.java create mode 100644 spring-mvc/servlet/src/main/webapp/basic.html create mode 100644 spring-mvc/servlet/src/main/webapp/index.html diff --git a/spring-mvc/servlet/src/main/java/com/example/servlet/ServletApplication.java b/spring-mvc/servlet/src/main/java/com/example/servlet/ServletApplication.java index 9e98f311..322cac10 100644 --- a/spring-mvc/servlet/src/main/java/com/example/servlet/ServletApplication.java +++ b/spring-mvc/servlet/src/main/java/com/example/servlet/ServletApplication.java @@ -2,7 +2,9 @@ package com.example.servlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; +@ServletComponentScan // 서블릿 자동 등록 @SpringBootApplication public class ServletApplication { diff --git a/spring-mvc/servlet/src/main/java/com/example/servlet/basic/HelloServlet.java b/spring-mvc/servlet/src/main/java/com/example/servlet/basic/HelloServlet.java new file mode 100644 index 00000000..b920fa3e --- /dev/null +++ b/spring-mvc/servlet/src/main/java/com/example/servlet/basic/HelloServlet.java @@ -0,0 +1,30 @@ +package com.example.servlet.basic; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet(name = "helloServlet", urlPatterns = "/hello") +public class HelloServlet extends HttpServlet { + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + System.out.println("HelloServlet.service"); + System.out.println("req = " + req); + System.out.println("resp = " + resp); + + String username = req.getParameter("username"); + System.out.println("username = " + username); + + // response header 정보 추가 + resp.setContentType("text/plain"); + resp.setCharacterEncoding("utf-8"); + + // response body + resp.getWriter().write("hello " + username); + } +} diff --git a/spring-mvc/servlet/src/main/resources/application.properties b/spring-mvc/servlet/src/main/resources/application.properties index 8b137891..e69de29b 100644 --- a/spring-mvc/servlet/src/main/resources/application.properties +++ b/spring-mvc/servlet/src/main/resources/application.properties @@ -1 +0,0 @@ - diff --git a/spring-mvc/servlet/src/main/webapp/basic.html b/spring-mvc/servlet/src/main/webapp/basic.html new file mode 100644 index 00000000..0adffbde --- /dev/null +++ b/spring-mvc/servlet/src/main/webapp/basic.html @@ -0,0 +1,41 @@ + + + + + Title + + + + + \ No newline at end of file diff --git a/spring-mvc/servlet/src/main/webapp/index.html b/spring-mvc/servlet/src/main/webapp/index.html new file mode 100644 index 00000000..bfac4bd3 --- /dev/null +++ b/spring-mvc/servlet/src/main/webapp/index.html @@ -0,0 +1,12 @@ + + + + + Title + + + + + \ No newline at end of file