Search

Servlet

โ€ข
Servlet
โ€ข
์›น ์ปจํ…Œ์ด๋„ˆ - Tomcat
โ€ข
์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐ
โ€ข
์š”์ฒญ ๊ฒฝ๋กœ ๋งคํ•‘
โ€ข
์š”์ฒญ ์ฒ˜๋ฆฌ
โ€ข
์‘๋‹ต ์ฒ˜๋ฆฌ
โ€ข
ํŽ˜์ด์ง€ ์ด๋™
โ—ฆ
RequestDispatcher
โ—ฆ
HttpServletResponse

Servlet (์„œ๋ธ”๋ฆฟ)

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ์›น ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๊ธฐ์ˆ 
โ€ข
์›น ์„œ๋ฒ„์™€ ์ƒํ˜ธ ์ž‘์šฉํ•˜์—ฌ ๋™์  ์›น ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
โ€ข
์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„œ๋ฒ„ ์ธก ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
โ€ข
ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ด๊ณ , ๊ทธ ์š”์ฒญ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ ์ž‘์šฉํ•˜๊ฑฐ๋‚˜ ๋‹ค์–‘ํ•œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋™์ ์œผ๋กœ HTML์„ ์ƒ์„ฑ
HttpServlet ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•œ ์ž๋ฐ” ํด๋ž˜์Šค
์„œ๋ธ”๋ฆฟ์€ ๋ณดํ†ต Java ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„(์˜ˆ: Apache Tomcat, Jetty)์—์„œ ์‹คํ–‰๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‘๋‹ต์„ ์ƒ์„ฑํ•˜์—ฌ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋™์ž‘์„ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค.

์›น ์ปจํ…Œ์ด๋„ˆ(์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ)

์›น ์ปจํ…Œ์ด๋„ˆ๋Š” ์„œ๋ธ”๋ฆฟ๊ณผ JSP(JavaServer Pages) ๊ฐ™์€ ์›น ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ๊ตฌ์„ฑ ์š”์†Œ
โ€ข
์ฃผ๋กœ ๋™์  ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„œ๋ฒ„ ์ธก ๋กœ์ง์„ ์ฒ˜๋ฆฌ
โ€ข
Servlet๊ณผ JSP ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  HTTP ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ด๊ณ  ์‘๋‹ต์„ ์ƒ์„ฑ
ex) Tomcat

Tomcat

์›น ์„œ๋ฒ„ ๋ฐ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ
Java ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋†’์€ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด

์ฃผ์š” ๊ธฐ๋Šฅ

โ€ข
์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ
โ€ข
์ •์  ๋ฐ ๋™์  ์›น ์ฝ˜ํ…์ธ  ์ œ๊ณต
โ€ข
์›น ์„œ๋ฒ„
โ€ข
์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ˜ธ์ŠคํŒ…

์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐ

โ€ข
๋กœ๋”ฉ ๋‹จ๊ณ„
โ€ข
์ดˆ๊ธฐํ™” ๋‹จ๊ณ„
โ€ข
์‹คํ–‰ ๋‹จ๊ณ„
โ€ข
์†Œ๋ฉธ ๋‹จ๊ณ„

๋กœ๋”ฉ ๋‹จ๊ณ„

์ตœ์ดˆ ์š”์ฒญ ์‹œ, ์ƒˆ๋กœ์šด ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ 2๋ฒˆ์งธ ์š”์ฒญ๋ถ€ํ„ฐ๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•œ๋‹ค.

์ดˆ๊ธฐํ™” ๋‹จ๊ณ„

init() ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๋‹จ๊ณ„๋กœ, ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๊ฐ€ ์ตœ์ดˆ๋กœ ํ•œ ๋ฒˆ ์ƒ์„ฑ๋  ๋•Œ ์‹คํ–‰๋œ๋‹ค. ๋งค ์š”์ฒญ๋งˆ๋‹ค ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์—๋งŒ ์‹คํ–‰๋œ๋‹ค. ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

์‹คํ–‰ ๋‹จ๊ณ„

โ€ข
doGet( )
โ€ข
doPost( )

doGet( )

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ GET ์š”์ฒญ ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์ด ์™”์„ ๋•Œ, ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ

doPost( )

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ POST ์š”์ฒญ ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์ด ์™”์„ ๋•Œ, ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ

์†Œ๋ฉธ ๋‹จ๊ณ„

์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ ๋•Œ, ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋กœ๋ถ€ํ„ฐ ์„œ๋ธ”๋ฆฟ์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ destroy() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋‹จ๊ณ„

์š”์ฒญ ๊ฒฝ๋กœ ๋งคํ•‘

์š”์ฒญ ๊ฒฝ๋กœ ๋งคํ•‘ ๋ฐฉ๋ฒ•

โ€ข
web.xml์„ ์‚ฌ์šฉํ•œ ๋งคํ•‘
โ€ข
์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ ๋งคํ•‘

web.xml์„ ์‚ฌ์šฉํ•œ ๋งคํ•‘

์„œ๋ธ”๋ฆฟ์„ ์š”์ฒญ ๊ฒฝ๋กœ์— ๋งคํ•‘ํ•˜๋Š” ๊ฐ€์žฅ ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•
โ€ข
web.xml
<web-app> <servlet> <servlet-name>BoardListServlet</servlet-name> <servlet-class>com.example.BoardListServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BoardListServlet</servlet-name> <url-pattern>/board/list</url-pattern> </servlet-mapping> </web-app>
XML
๋ณต์‚ฌ
BoardListServlet์ด๋ผ๋Š” ์„œ๋ธ”๋ฆฟ์„ /board/list ๊ฒฝ๋กœ๋กœ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.
/board/list ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์ด ์˜ค๋ฉด BoardListServlet ์ด ์š”์ฒญ์„ ์ „๋‹ฌ๋ฐ›์Šต๋‹ˆ๋‹ค.

์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ ๋งคํ•‘

Java EE 6 ์ด์ƒ ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ธ”๋ฆฟ์„ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•
โ€ข
BoardListServlet.java
import javax.servlet.annotation.WebServlet; // @WebServlet("/board/list") @WebServlet(name = "BoardListServlet", urlPatterns = {"/board/list"}) public class BoardListServlet extends HttpServlet { // doGet, doPost, ... }
Java
๋ณต์‚ฌ

์š”์ฒญ ์ฒ˜๋ฆฌ

HttpServletRequest

: ํด๋ผ์ด์–ธํŠธ์˜ HTTP ์š”์ฒญ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด
HttpServletRequest ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ์„ ํ†ตํ•ด ์š”์ฒญ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์š” ๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ
์„ค๋ช…
getMethod()
HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ ๋ฐ˜ํ™˜ (GET, POST, PUT, ๋“ฑ)
getProtocol()
ํ”„๋กœํ† ์ฝœ ๋ฐ ๋ฒ„์ „ ์ •๋ณด ๋ฐ˜ํ™˜ (์˜ˆ: HTTP/1.1)
getRequestURL()
์š”์ฒญ URL ๋ฐ˜ํ™˜
getRequestURI()
์š”์ฒญ URI ๋ฐ˜ํ™˜ (์ปจํ…์ŠคํŠธ ๊ฒฝ๋กœ ํฌํ•จ)
getContextPath()
์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ปจํ…์ŠคํŠธ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜ (๋ฃจํŠธ ๊ฒฝ๋กœ)
getQueryString()
์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ (URL์˜ ? ์ดํ›„ ๋ถ€๋ถ„)
getHeader(String name)
์ง€์ •๋œ HTTP ํ—ค๋”์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜
getParameter(String name)
์ง€์ •๋œ ์ด๋ฆ„์˜ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ฐ˜ํ™˜
getParameterMap()
๋ชจ๋“  ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋งต ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜
getParameterNames()
๋ชจ๋“  ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์„ ๋‚˜์—ด
getCookies()
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ๋ชจ๋“  ์ฟ ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜ด
getSession()
ํ˜„์žฌ ์š”์ฒญ์— ๋Œ€ํ•œ ์„ธ์…˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
getRemoteAddr()
ํด๋ผ์ด์–ธํŠธ์˜ IP ์ฃผ์†Œ ๋ฐ˜ํ™˜
getRemoteHost()
ํด๋ผ์ด์–ธํŠธ์˜ ํ˜ธ์ŠคํŠธ๋ช… ๋ฐ˜ํ™˜
getLocalAddr()
์„œ๋ฒ„์˜ IP ์ฃผ์†Œ ๋ฐ˜ํ™˜
getLocalPort()
์„œ๋ฒ„์˜ ํฌํŠธ ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜
getScheme()
ํ”„๋กœํ† ์ฝœ ์Šคํ‚ด ๋ฐ˜ํ™˜ (์˜ˆ: http, https)
getServerName()
์„œ๋ฒ„ ํ˜ธ์ŠคํŠธ๋ช… ๋ฐ˜ํ™˜
getServerPort()
์„œ๋ฒ„ ํฌํŠธ ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜
getCharacterEncoding()
์š”์ฒญ์˜ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ ๋ฐ˜ํ™˜
getInputStream()
์š”์ฒญ์˜ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๊ฐ€์ ธ์˜ด
setAttribute(String name, Object value)
์š”์ฒญ ์†์„ฑ ์„ค์ •
getAttribute(String name)
์š”์ฒญ ์†์„ฑ์„ ๊ฐ€์ ธ์˜ด
โ€ข
MyServlet.java
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/MyServlet") public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.getWriter().println("Hello, World!"); // ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ๋ฉ”์„œ๋“œ๋ฅผ ํ™•์ธ String method = request.getMethod(); response.getWriter().println("HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ: " + method); // ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ String paramValue = request.getParameter("paramName"); response.getWriter().println("ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’: " + paramValue); // ์š”์ฒญ URI์™€ URL ๊ฐ€์ ธ์˜ค๊ธฐ String requestURI = request.getRequestURI(); response.getWriter().println("์š”์ฒญ URI: " + requestURI); StringBuffer requestURL = request.getRequestURL(); response.getWriter().println("์š”์ฒญ URL: " + requestURL.toString()); // ํŠน์ • HTTP ํ—ค๋” ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ String userAgent = request.getHeader("User-Agent"); response.getWriter().println("User-Agent ํ—ค๋”: " + userAgent); // ํด๋ผ์ด์–ธํŠธ IP ์ฃผ์†Œ ํ™•์ธ String remoteAddr = request.getRemoteAddr(); response.getWriter().println("ํด๋ผ์ด์–ธํŠธ IP ์ฃผ์†Œ: " + remoteAddr); } }
Java
๋ณต์‚ฌ
โ€ข
๊ฒฐ๊ณผ
Hello, World! HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ: GET ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’: [] ์š”์ฒญ URI: [/ContextPath/MyServlet] ์š”์ฒญ URL: [http://localhost:8080//ContextPath/MyServlet] User-Agent ํ—ค๋”: [์‚ฌ์šฉ์ž ์—์ด์ „ํŠธ ์ •๋ณด] ํด๋ผ์ด์–ธํŠธ IP ์ฃผ์†Œ: [ํด๋ผ์ด์–ธํŠธ์˜ IP ์ฃผ์†Œ]
Java
๋ณต์‚ฌ

์‘๋‹ต ์ฒ˜๋ฆฌ

PrintWriter

: ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ์ค€ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ
์ฃผ๋กœ ๋ฌธ์ž์—ด, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ, ๋˜๋Š” HTML๊ณผ ๊ฐ™์€ ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

์ฃผ์š” ๊ธฐ๋Šฅ

โ€ข
ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
โ€ข
์ž๋™ ์ค„ ๋ฐ”๊ฟˆ ์ฒ˜๋ฆฌ
โ€ข
๋ฌธ์ž ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ
PrintWriter๋Š” ์ฃผ๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ HTTP ์‘๋‹ต์„ ์ƒ์„ฑํ•  ๋•Œ ๋˜๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋กํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
โ€ข
MyServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/MyServlet") public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ์‘๋‹ต์˜ ์ฝ˜ํ…์ธ  ํƒ€์ž…์„ HTML๋กœ ์„ค์ • response.setContentType("text/html"); // PrintWriter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HTML ์‘๋‹ต์„ ์ƒ์„ฑ PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>My HTML Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello, World from My HTML Servlet!</h1>"); out.println("</body>"); out.println("</html>"); } }
Java
๋ณต์‚ฌ

ํŽ˜์ด์ง€ ์ด๋™

RequestDispatcher

: ์„œ๋ธ”๋ฆฟ ๊ฐ„ ๋˜๋Š” ์„œ๋ธ”๋ฆฟ๊ณผ JSP ํŽ˜์ด์ง€ ๊ฐ„์— ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด
โ€ข
MyServlet.java
import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/MyServlet") public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ๋ฃจํŠธ ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์„ ์ „๋‹ฌํ•  URL ์„ค์ • String targetURL = "/NextServlet"; // ํฌ์›Œ๋“œ ๋ฐฉ์‹์œผ๋กœ ์ด๋™ํ•  ๊ฒฝ๋กœ // RequestDispatcher๋ฅผ ์–ป์–ด์˜ด RequestDispatcher dispatcher = request.getRequestDispatcher(targetURL); // RequestDispatcher๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ ๋ฐ ์‘๋‹ต์„ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ ๋˜๋Š” JSP ํŽ˜์ด์ง€๋กœ ์ „๋‹ฌ dispatcher.forward(request, response); } }
Java
๋ณต์‚ฌ

HttpServletResponse

: ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ์˜ HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ƒ์„ฑํ•˜๊ณ  ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด

์ฃผ์š” ๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ
์„ค๋ช…
setContentType(String type)
์‘๋‹ต ์ฝ˜ํ…์ธ  ์œ ํ˜•(MIME ํƒ€์ž…) ์„ค์ •
addCookie(Cookie cookie)
ํด๋ผ์ด์–ธํŠธ์— ์ฟ ํ‚ค ์ถ”๊ฐ€
setStatus(int sc)
์‘๋‹ต์˜ HTTP ์ƒํƒœ ์ฝ”๋“œ ์„ค์ •
sendRedirect(String location)
ํด๋ผ์ด์–ธํŠธ๋ฅผ ๋‹ค๋ฅธ URL๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
setHeader(String name, String value)
์‘๋‹ต ํ—ค๋” ์„ค์ •
getWriter()
PrintWriter ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์‘๋‹ต ๋‚ด์šฉ ์ž‘์„ฑ
getOutputStream()
์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ด์ง„ ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ€๋Šฅ
setCharacterEncoding(String charset)
์‘๋‹ต ์ฝ˜ํ…์ธ ์˜ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ ์„ค์ •
getCharacterEncoding()
ํ˜„์žฌ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ ๋ฐ˜ํ™˜
addHeader(String name, String value)
์‘๋‹ต ํ—ค๋” ์ถ”๊ฐ€
setIntHeader(String name, int value)
์ •์ˆ˜ ๊ฐ’ ํ—ค๋” ์„ค์ •
addIntHeader(String name, int value)
์ •์ˆ˜ ๊ฐ’ ํ—ค๋” ์ถ”๊ฐ€
setDateHeader(String name, long date)
๋‚ ์งœ ํ—ค๋” ์„ค์ •
addDateHeader(String name, long date)
๋‚ ์งœ ํ—ค๋” ์ถ”๊ฐ€
setContentType(String type)
์‘๋‹ต ์ฝ˜ํ…์ธ ์˜ MIME ์œ ํ˜•์„ ์„ค์ •
setBufferSize(int size)
์ถœ๋ ฅ ๋ฒ„ํผ์˜ ํฌ๊ธฐ ์„ค์ •
getBufferSize()
ํ˜„์žฌ ๋ฒ„ํผ ํฌ๊ธฐ ๋ฐ˜ํ™˜
flushBuffer()
๋ฒ„ํผ์˜ ๋‚ด์šฉ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ํ”Œ๋Ÿฌ์‹œ
reset()
์‘๋‹ต์„ ์žฌ์„ค์ •ํ•˜๊ณ  ํ—ค๋”, ๋ฒ„ํผ ๋“ฑ์„ ์ดˆ๊ธฐํ™”
resetBuffer()
๋ฒ„ํผ๋งŒ ์ดˆ๊ธฐํ™”
โ€ข
BoardUpdateServlet.java
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/board/update") public class BoardUpdateServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ์š”์ฒญ ์ฒ˜๋ฆฌ ๋กœ์ง (์ƒ๋žต) // ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  /board/list ๊ฒฝ๋กœ๋กœ ์ด๋™ response.sendRedirect(request.getContextPath() + "/board/list"); } }
Java
๋ณต์‚ฌ