Servlet์ ์ ์ด๋ก์ ์์์ ๋น์ฆ๋์ค ๋ก์ง ์คํ ๋ถ๋ถ์ ์ ์ธํ ๋ชจ๋ ๋ถ๋ถ์ ๋ค ์๋ํ ํด์ค๋ค.
๋์ ์น์ดํ๋ฆฌ์ผ์ด์
์ปดํฌ๋ํธ
ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋์ ์ผ๋ก ์๋ํ๊ณ , ์๋ต์ html์ ์ด์ฉ.
java thread์ด์ฉํ์ฌ ๋์(๊ฐ์ฅ ํฐ ๊ฐ์ )
thread๋ฅผ ์ด์ฉํด ์๋ฒ์ ๋ถํ๊ฐ ์ ๋ค.
MVCํจํด์์ Controller๋ก ์ด์ฉ๋จ.
Servlet ๊ธฐ๋ณธ ๊ตฌํ
/**
* Servlet implementation class HelloWorld
*/
@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Servlet Class๋ HttpServlet
ํด๋์ค๋ฅผ ์์๋ฐ๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ Servlet
์ธํฐํ์ด์ค์ GenericServlet
, HttpServlet
์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
Servlet์ ์คํ ์์๋ ๊ฐ๋ฐ์๊ฐ ๊ด๋ฆฌํ๋ ๊ฒ ์๋ Servlet Container๊ฐ ๊ด๋ฆฌ๋ฅผ ํ๋ค. Servlet์ ์ํด ์ฌ์ฉ์๊ฐ ์ ์ํ Servlet ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ ํธ์ถ๋๊ณ ์ฌ๋ผ์ง๋ค.
์ด๋ ๊ฒ ๊ฐ๋ฐ์๊ฐ ์๋ ํ๋ก๊ทธ๋จ์ ์ํด ๊ฐ์ฒด๋ค์ด ๊ด๋ฆฌ๋๋ ๊ฒ์ IoC(Inversion of Control)๋ผ๊ณ ํ๋ค.
@WebServlet
: ์๋ธ๋ฆฟ ์ด๋
ธํ
์ด์
HTTP ์์ฒญ์ ํตํด ๋งคํ๋ url์ด ํธ์ถ๋๋ฉด ์๋ธ๋ฆฟ ์ปจํ
์ด๋๋ ๋ค์ ๋ฉ์๋(protected void service(HttpServletRequest request, HttpServletResponse response)
)๋ฅผ ์คํํ๋ค.
์์ฒญ / ์๋ต ์ฒ๋ฆฌ
์์ฒญ ์ฒ๋ฆฌ ๊ฐ์ฒด ๋ฐ ์๋ต ์ฒ๋ฆฌ ๊ฐ์ฒด๋ฅผ Tomcat์์ ๋ฐ๋๋ค.
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
HttpServletRequest
ํด๋ผ์ด์ธํธ์ ์์ฒญ ์ฒ๋ฆฌ ๊ฐ์ฒด
HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ์ง์ ํ์ฑํด์ ์ฌ์ฉํด๋ ๋์ง๋ง, ๋งค์ฐ ๋ถํธํ ๊ฒ์ด๋ค. ์๋ธ๋ฆฟ์ ๊ฐ๋ฐ์๊ฐ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๊ฐ๋ฐ์ ๋์ ์ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํ์ฑํ์ฌ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ HttpServletRequest
๊ฐ์ฒด์ ๋ด์์ ์ ๊ณตํด์ค๋ค.
HTTP ์์ฒญ ๋ฉ์ธ์ง
POST /save HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded
username=dahye&age=20
๋ฐ๋
form ํ๋ผ๋ฏธํฐ ํ์ ์กฐํ
message body ๋ฐ์ดํฐ ์ง์ ์กฐํ
์์ ์ ์ฅ์ ๊ธฐ๋ฅ
ํด๋น HTTP ์์ฒญ์ด ์์๋ถํฐ ๋๋ ๋๊น์ง ์ ์ง๋๋ ์์ ์ ์ฅ์ ๊ธฐ๋ฅ
์ ์ฅ : request.setAttribute(name, value)
์กฐํ: request.getAttribute(name)
์ธ์
๊ด๋ฆฌ ๊ธฐ๋ฅ
request.getSession(create: true)
HttpServletRequest
๋ก ์กฐํํ ์ ์๋ ์ ๋ณด๋ค์ ์กฐํํด๋ณด๋ ์์ ์ด๋ค.
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
@WebServlet(name = "requestHeaderServlet", urlPatterns = "/request-header")
public class RequestHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
printStartLine(request);
printHeaders(request);
printHeaderUtils(request);
printEtc(request);
}
private void printStartLine(HttpServletRequest request) {
System.out.println("--- REQUEST-LINE - start ---");
System.out.println("request.getMethod() = " + request.getMethod()); //GET
System.out.println("request.getProtocal() = " + request.getProtocol()); //
// HTTP/1.1
System.out.println("request.getScheme() = " + request.getScheme()); //http
// http://localhost:8080/request-header
System.out.println("request.getRequestURL() = " + request.getRequestURL());
// /request-test
System.out.println("request.getRequestURI() = " + request.getRequestURI());
//username=hi
System.out.println("request.getQueryString() = " +
request.getQueryString());
System.out.println("request.isSecure() = " + request.isSecure()); //https
// ์ฌ์ฉ ์ ๋ฌด
System.out.println("--- REQUEST-LINE - end ---");
System.out.println();
}
private void printHeaders(HttpServletRequest request) {
System.out.println("--- Headers start ---");
request.getHeaderNames().asIterator().forEachRemaining(headerName -> System.out.println(headerName + " : " + request.getHeader(headerName)));
System.out.println("--- Headers end ---");
System.out.println();
}
// Header ํธ๋ฆฌํ ์กฐํ
private void printHeaderUtils(HttpServletRequest request) {
System.out.println("--- Header ํธ์ ์กฐํ start ---");
System.out.println("[Host ํธ์ ์กฐํ]");
System.out.println("request.getServerName() = " +
request.getServerName()); //Host ํค๋
System.out.println("request.getServerPort() = " + request.getServerPort()); //Host ํค๋
System.out.println();
System.out.println("[Accept-Language ํธ์ ์กฐํ]");
request.getLocales().asIterator()
.forEachRemaining(locale -> System.out.println("locale = " + locale));
System.out.println("request.getLocale() = " + request.getLocale());
System.out.println();
System.out.println("[cookie ํธ์ ์กฐํ]");
if (request.getCookies() != null) {
for (Cookie cookie : request.getCookies()) {
System.out.println(cookie.getName() + ": " + cookie.getValue());
}
}
System.out.println();
System.out.println("[Content ํธ์ ์กฐํ]");
System.out.println("request.getContentType() = " +
request.getContentType());
System.out.println("request.getContentLength() = " + request.getContentLength());
System.out.println("request.getCharacterEncoding() = " +
request.getCharacterEncoding());
System.out.println("--- Header ํธ์ ์กฐํ end ---");
System.out.println();
}
private void printEtc(HttpServletRequest request) {
System.out.println("--- ๊ธฐํ ์กฐํ start ---");
System.out.println("[Remote ์ ๋ณด]");
System.out.println("request.getRemoteHost() = " + request.getRemoteHost());
System.out.println("request.getRemoteAddr() = " + request.getRemoteAddr());
System.out.println("request.getRemotePort() = " + request.getRemotePort());
System.out.println();
System.out.println("[Local ์ ๋ณด]");
System.out.println("request.getLocalName() = " + request.getLocalName());
System.out.println("request.getLocalAddr() = " + request.getLocalAddr());
System.out.println("request.getLocalPort() = " + request.getLocalPort());
System.out.println("--- ๊ธฐํ ์กฐํ end ---");
System.out.println();
}
}
--- REQUEST-LINE - start ---
request.getMethod() = GET
request.getProtocal() = HTTP/1.1
request.getScheme() = http
request.getRequestURL() = http://localhost:8080/request-header
request.getRequestURI() = /request-header
request.getQueryString() = null
request.isSecure() = false
--- REQUEST-LINE - end ---
--- Headers start ---
host : localhost:8080
connection : keep-alive
cache-control : max-age=0
sec-ch-ua : " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile : ?0
upgrade-insecure-requests : 1
user-agent : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
sec-fetch-site : same-origin
sec-fetch-mode : navigate
sec-fetch-user : ?1
sec-fetch-dest : document
referer : http://localhost:8080/basic.html
accept-encoding : gzip, deflate, br
accept-language : ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
--- Headers end ---
--- Header ํธ์ ์กฐํ start ---
[Host ํธ์ ์กฐํ]
request.getServerName() = localhost
request.getServerPort() = 8080
[Accept-Language ํธ์ ์กฐํ]
locale = ko_KR
locale = ko
locale = en_US
locale = en
request.getLocale() = ko_KR
[cookie ํธ์ ์กฐํ]
[Content ํธ์ ์กฐํ]
request.getContentType() = null
request.getContentLength() = -1
request.getCharacterEncoding() = UTF-8
--- Header ํธ์ ์กฐํ end ---
--- ๊ธฐํ ์กฐํ start ---
[Remote ์ ๋ณด]
request.getRemoteHost() = 0:0:0:0:0:0:0:1
request.getRemoteAddr() = 0:0:0:0:0:0:0:1
request.getRemotePort() = 64206
[Local ์ ๋ณด]
request.getLocalName() = localhost
request.getLocalAddr() = 0:0:0:0:0:0:0:1
request.getLocalPort() = 8080
--- ๊ธฐํ ์กฐํ end ---
์์ฒญ ๋ฐ์ดํฐ
GET : ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ
url?username=hello&age=30
๋ฉ์ธ์ง ๋ฐ๋ ์์ด url์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ์ ๋ฐ์ดํฐ๋ฅผ ํฌํจํด์ ์ ๋ฌ
๊ฒ์, ํํฐ, ํ์ด์ง๋ฑ์์ ๋ง์ด ์ฌ์ฉ
/**
* 1. ํ๋ผ๋ฏธํฐ ์ ์ก ๊ธฐ๋ฅ
* http://localhost:8080/request-param?username=hello&age=20&username=hello2
*/
@WebServlet(name = "requestParamServlet", urlPatterns = "/request-param")
public class RequestParamServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("==> ์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ start");
request.getParameterNames().asIterator().forEachRemaining(paramName -> System.out.println(paramName + " = " + request.getParameter(paramName)));
System.out.println("==> ์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ end");
System.out.println();
System.out.println("==> ๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ start");
String username = request.getParameter("username");
System.out.println("username = " + username);
String age = request.getParameter("age");
System.out.println("age = " + age);
System.out.println("==> ๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ end");
System.out.println();
System.out.println("==> ์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ start");
String[] usernames = request.getParameterValues("username");
for (String name : usernames) {
System.out.println("username = " + name);
}
System.out.println("==> ์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ end");
}
}
ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ด ํ๊ฐ์ธ๋ฐ, ๊ฐ์ด ์ฌ๋ฌ๊ฐ๊ฐ ๋์ด์ค๋ ๊ฒฝ์ฐ์๋ request.getParameterValues
๋ฅผ ์ฌ์ฉํด์ ์ค๋ณต๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์ผ๋ฉฐ, request.getParameter
๋ ์ฌ๋ฌ๊ฐ์ ๊ฐ์ค ์ฒซ๋ฒ์งธ ๊ฐ์ ๋ฐํํ๋ค.
==> ์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ start
username = hello
age = 20
==> ์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ end
==> ๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ start
username = hello
age = 20
==> ๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ end
==> ์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ start
username = hello
username = hello2
==> ์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ end
์ฌ๊ธฐ์๋ ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋, HTTP ๋ฉ์ธ์ง ๋ฐ๋๋ฅผ ์ฌ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์, content-type์ด ์๋ค.
POST : HTML-form ๋ฐฉ์
content-type: application/x-www-form-urlencoded
๋ฉ์ธ์ง ๋ฐ๋์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ํ์์ผ๋ก ์ ๋ฌ(username=hello&age=30)
ํ์๊ฐ์
, ์ํ ์ฃผ๋ฌธ, HTML form ์ฌ์ฉ
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/request-param" method="post">
username: <input type="text" name="username" /> age: <input type="text" name="age" /> <button type="submit">์ ์ก</button>
</form>
</body>
</html>
form ์ ์ก์ผ๋ก ํ๊ฒ ๋๋ ๊ฒฝ์ฐ์ input์ ๋์ด์ค๋ ๋ฐ์ดํฐ๊ฐ ์ ๋ฌ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
username=test&age=30]
==> ์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ start
username = test
age = 30
==> ์ ์ฒด ํ๋ผ๋ฏธํฐ ์กฐํ end
==> ๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ start
username = test
age = 30
==> ๋จ์ผ ํ๋ผ๋ฏธํฐ ์กฐํ end
==> ์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ start
username = test
==> ์ด๋ฆ์ด ๊ฐ์ ๋ณต์ ํ๋ผ๋ฏธํฐ ์กฐํ end
request.getParameter()
๋ GET๊ณผ POST ํ์ ๋๊ฐ ๋ค ์ง์ํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. POST HTML form ํ์์ HTTP ๋ฉ์ธ์ง ๋ฐ๋์ ํด๋น ๋ฐ์ดํฐ๋ฅผ ํฌํจํด์ ๋ณด๋ด๊ธฐ๋๋ฌธ์ ๋ฐ๋์ ํฌํจ๋ ๋ฐ์ดํฐ๊ฐ ์ด๋ค ํ์์ธ์ง content-type
์ ๋ฐ๋์ ์ง์ ํด์ ๋ณด๋ด์ผํ๋ค. Form ํ์์ application/x-www-form-urlencoded
ํ์์ด๋ค.
HTTP message body
HTTP API์์ ์ฃผ๋ก ์ฌ์ฉ
JSON, XML, TEXT(์ฃผ๋ก JSON ์ฌ์ฉ)
// ์ด๋ ํ
์คํธ๋ postman์ผ๋ก ์งํ
@WebServlet(name = "RequestBodyStringServlet", urlPatterns = "/request-body-string")
public class RequestBodyStringServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletInputStream inputStream = request.getInputStream();
String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
System.out.println("messageBody = " + messageBody);
response.getWriter().write("OK");
}
}
HttpServletResponse
ํด๋ผ์ด์ธํธ ์๋ต ์ฒ๋ฆฌ ๊ฐ์ฒด
ํธ์ ๊ธฐ๋ฅ ์ ๊ณต
Content-Type
, Cookie, Redirect
@WebServlet(name = "ResponseHeaderServlet", urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// status-line
response.setStatus(HttpServletResponse.SC_OK);
// response-header
response.setHeader("Content-Type", "text/plain");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setHeader("my-header", "hello");
PrintWriter writer = response.getWriter();
writer.println("OK");
}
}
์์์ ์ค์ ํ ๊ฐ์ด Response Header์ ์ค์ ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ฟ ํค, redirect ์ค์
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "ResponseHeaderServlet", urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// status-line
response.setStatus(HttpServletResponse.SC_OK);
// response-header
// response.setHeader("Content-Type", "text/plain");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setHeader("my-header", "hello");
// header ํธ์ ๋ฉ์๋
content(response);
// cookie ํธ์ ๋ฉ์๋
cookie(response);
// redirect
redirect(response);
PrintWriter writer = response.getWriter();
writer.println("OK");
}
private void content(HttpServletResponse response) {
// response.setHeader("Content-Type", "text/plain;charset=utf-8");
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
response.setContentLength(2); //(์๋ต์ ์๋ ์์ฑ)
}
private void cookie(HttpServletResponse response) {
response.setHeader("Set-Cookie", "myCookie=good; Max-Age=600");
Cookie cookie = new Cookie("myCookie", "good");
cookie.setMaxAge(600); //600์ด
response.addCookie(cookie);
}
private void redirect(HttpServletResponse response) throws IOException {
//Status Code 302
//Location: /basic/hello-form.html
//response.setStatus(HttpServletResponse.SC_FOUND); //302
//response.setHeader("Location", "/basic/hello-form.html");
response.sendRedirect("/basic/hello-form.html");
}
}
์๋ต ๋ฐ์ดํฐ
๋จ์ ํ
์คํธ ์๋ต
PrintWriter writer = response.getWriter();
writer.println("OK");
HTML ์๋ต : text/html
์ง์ ํ์
@WebServlet(name = "responseHtmlServlet", urlPatterns = "/response-html")
public class ResponseHtmlServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
writer.write("<html>");
writer.write("<body>");
writer.write(" <h1>hello</h1>");
writer.write("</body>");
writer.write("</html>");
}
}
<html>
<body>
<h1>hello</h1>
</body>
</html>
HTTP API - MessageBody JSON ์๋ต
@WebServlet(name = "responseJsonServlet", urlPatterns = "/response-json")
public class ResponseJsonServlet extends HttpServlet {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
HelloData helloData = new HelloData();
helloData.setAge(30);
helloData.setUsername("test");
String result = objectMapper.writeValueAsString(helloData);
response.getWriter().write(result);
}
}
{"username":"test","age":30}
application/json
์ ์คํ์ utf-8 ํ์์ ์ฌ์ฉํ๋๋ก ์ ์๋์ด ์์ผ๋ฉฐ, charset=utf-8
๊ณผ ๊ฐ์ ์ถ๊ฐ ๋ฐ๋ผ๋ฏธํฐ๋ ์ง์ํ์ง ์๋๋ค. ์ฆ, application/json;charset=utf-8
์ ์๋ฏธ ์๋ ํ๋ผ๋ฏธํฐ๊ฐ ์ถ๊ฐ๋ ๊ฒ์ด๊ณ , response.getWriter()
๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ผ๋ก ์ถ๊ฐ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ๋ค. ์ด ๋ฌธ์ ๋ response.getOutputStream()
์ผ๋ก ํด๊ฒฐํ ์ ์๋ค.
service()
GET, POST ๋ฐฉ์์ ๊ตฌ๋ถํ์ง ์๋๋ค. HTTP ์์ฒญ์์ GET๋ฐฉ์์ผ๋ก ์ค๋ฉด, doGet()
๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ POST๋ฐฉ์์ผ๋ก ์์ฒญ์ด ์ค๋ฉด, doPost()
๋ฅผ ํธ์ถํ๋ค.
@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);
}
doGet()
GET ๋ฐฉ์ : URL ๊ฐ์ผ๋ก ์ ๋ณด๊ฐ ์ ์ก๋์ด ๋ณด์์ ์ฝํจ
Get : http://IP์ฃผ์:port๋ฒํธ/์ปจํ
์คํธ/path/MemberJoin?id=โabcdefgโ&name=โํ๊ธธ๋โ
๋ค์๊ณผ ๊ฐ์ด url ๊ฐ์ผ๋ก ์ ๋ณด๊ฐ ์ ์ก๋๋ ๋ฐฉ๋ฒ์ด๋ค.
html form ํ๊ทธ์ method ์์ฑ์ด get์ธ ๊ฒฝ์ฐ ํธ์ถ๋๋ค.
<form action="GetMethod" method="get"></form>
์น๋ธ๋ผ์ฐ์ ์ ์ฃผ์์ฐฝ์ ์ด์ฉํด servlet์ ์์ฒญํ ๊ฒฝ์ฐ์๋ ํธ์ถ๋๋ค.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doGet");
// ๋ฌด์์ผ๋ก ์๋ตํ ์ง ๊ตฌํํด ์ค์ผํ๋ค.
// ํ๊ธ์ง์ charset=euc-kr
response.setContentType("text/html; charset=euc-kr");
//์น ๋ธ๋ผ์ฐ์ ์ ์ถ๋ ฅํ๊ธฐ ์ํ ์คํธ๋ฆผ
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head>");
writer.println("</head>");
writer.println("<body>");
writer.println("<h1>Hello World</h1>");
writer.println("</body>");
writer.println("</html>");
writer.close();
}
doPost()
POST ๋ฐฉ์ : header๋ฅผ ์ด์ฉํด ์ ๋ณด๊ฐ ์ ์ก๋์ด ๋ณด์์ ๊ฐํ๋ค.
Post : http://IP์ฃผ์:port๋ฒํธ/์ปจํ
์คํธ/path/MemberJoin
html form ํ๊ทธ์ method ์์ฑ์ด post์ธ ๊ฒฝ์ฐ ํธ์ถ๋๋ค.
<!-- post.html -->
<form action="PostMethod" method="post">
<input type="sublmit" value="post">
</form>
//PostMethod.java (servlet)
//...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doPost");
response.setContentType("text/html; charset=euc-kr");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head>");
writer.println("</head>");
writer.println("<body>");
writer.println("<h1>POST ๋ฐฉ์์
๋๋ค..</h1>");
writer.println("</body>");
writer.println("</html>");
}
Context Path
WAS(Web Application Server)์์ ์น์ดํ๋ฆฌ์ผ์ด์
์ ๊ตฌ๋ถํ๊ธฐ ์ํ path ์
๋๋ค. ์ดํด๋ฆฝ์ค์์ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ฉด, ์๋์ผ๋ก server.xml์ ์ถ๊ฐ ๋ฉ๋๋ค.
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context docBase="ch05" path="/ch05" reloadable="true" source="org.eclipse.jst.jee.server:ch05"/></Host>
๋ก๊ทธ ์ถ๋ ฅ
application.properties
logging.level.org.apache.coyote.http11=debug
๊ฐ๋ฐ ์๋ฒ์์ ์ ์ค์ ์ ์ถ๊ฐํด HTTP ์์ฒญ๊ด๋ จํด์ ๋ก๊ทธ๋ฅผ ์ถ๋ ฅํ ์ ์๋ค.
์์ฒญ URL : http://localhost:8080/hello?username=%EB%8B%A4%ED%98%9C
2021-05-23 22:19:31.486 DEBUG 84310 --- [nio-8080-exec-1] o.a.coyote.http11.Http11InputBuffer : Before fill(): parsingHeader: [true], parsingRequestLine: [true], parsingRequestLinePhase: [0], parsingRequestLineStart: [0], byteBuffer.position(): [0], byteBuffer.limit(): [0], end: [0]
2021-05-23 22:19:31.489 DEBUG 84310 --- [nio-8080-exec-1] o.a.coyote.http11.Http11InputBuffer : Received [GET /hello?username=%EB%8B%A4%ED%98%9C HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile: ?0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7
]
Servlet ์๋ ์์
ํด๋ผ์ด์ธํธ์์ servlet ์์ฒญ์ด ๋ค์ด์ค๋ฉด ์๋ฒ์์๋ servlet ์ปจํ
์ด๋๋ฅผ ๋ง๋ค๊ณ ์์ฒญ์ด ์์ ๋๋ง๋ค ์ค๋ ๋๊ฐ ์์ฑ๋๋ค.
์ค๋ ๋๋ฅผ ์ด์ฉํด request๋ฅผ ์ฒ๋ฆฌํด ์๋ฒ์ ๋ถํ๊ฐ ์ ๊ฒ ๊ฑธ๋ฆฐ๋ค.
์ด๋ Servlet ์ปจํ
์ด๋์์ 1. ์ค๋ ๋ ์์ฑ 2. servlet ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
HTTP ์์ฒญ์ด ๋ฐ์ํ์ ๋๋ฅผ ์๊ฐํด๋ณด์.
WAS๋ Request, Response ๊ฐ์ฒด๋ฅผ ์๋ก ๋ง๋ค์ด์ ์๋ธ๋ฆฟ ๊ฐ์ฒด๋ฅผ ํธ์ถํ๋ค.
์ด๋, ๊ฐ๋ฐ์๋ Request ๊ฐ์ฒด์์ HTTP ์์ฒญ์ ๋ณด๋ฅผ ๊บผ๋ด ์ฌ์ฉํ๊ณ , Response ๊ฐ์ฒด์ HTTP ์๋ต ์ ๋ณด๋ฅผ ํธ๋ฆฌํ๊ฒ ์
๋ ฅํ๋ค.
WAS๋ Response ๊ฐ์ฒด์ ๋ด๊ฒจ์๋ ๋ด์ฉ์ผ๋ก HTTP ์๋ต ์ ๋ณด๋ฅผ ์์ฑํ๋ค.
Servlet Container(WAS)
ํฐ์บฃ๊ณผ ๊ฐ์ด ์๋ธ๋ฆฟ์ ์ง์ํ๋ WAS๋ฅผ ์๋ธ๋ฆฟ ์ปจํ
์ด๋๋ผ๊ณ ํ๋ค.
์๋ธ๋ฆฟ ๊ฐ์ฒด๋ฅผ ์์ฑ, ์ด๊ธฐํ, ํธ์ถ, ์ข
๋ฃํ๋ ์๋ช
์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌ(์ฑ๊ธํค)
๊ณ ๊ฐ์ ์์ฒญ์ด ์ฌ๋๋ง๋ค ๊ณ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ ๋นํจ์จ
์ต์ด ๋ก๋ฉ ์์ ์ ์๋ธ๋ฆฟ ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋๊ณ ์ฌํ์ฉ
๋ชจ๋ ๊ณ ๊ฐ ์์ฒญ์ ๋์ผํ ์๋ธ๋ฆฟ ๊ฐ์ฒด ์ธ์คํด์ค์ ์ ๊ทผ
์ด๋, ๊ณต์ ๋ณ์๋ ์ฃผ์ํด์ ์ฌ์ฉํด์ผํ๋ค.
์๋ธ๋ฆฟ ์ปจํ
์ด๋ ์ข
๋ฃ์ ํจ๊ป ์ข
๋ฃ
JSP๋ ์๋ธ๋ฆฟ์ผ๋ก ๋ณํ๋์ด ์ฌ์ฉ
๋์ ์์ฒญ์ ์ํ ๋ฉํฐ ์ฐ๋ ๋ ์ฒ๋ฆฌ ์ง์
๋์์์ฒญ - ๋ฉํฐ ์ฐ๋ ๋
์ฐ๋ ๋๋?
์ดํ๋ฆฌ์ผ์ด์
์ฝ๋๋ฅผ ํ๋ํ๋ ์์ฐจ์ ์ผ๋ก ์คํ
์๋ฐ ๋ฉ์ธ ๋ฉ์๋๋ฅผ ์ฒ์ ์คํํ๋ฉด main
์ด๋ผ๋ ์ด๋ฆ์ ์ฐ๋ ๋๊ฐ ์คํ
์ฐ๋ ๋๊ฐ ์๋ค๋ฉด ์๋ฐ ์ ํ๋ฆฌ์ผ์ด์
์คํ ๋ถ๊ฐ๋ฅ
์ฐ๋ ๋๋ ํ๋ฐ์ ํ๋์ ์ฝ๋ ๋ผ์ธ๋ง ์ํ
๋์ ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ฉด ์ฐ๋ ๋ ์ถ๊ฐ๋ก ์์ฑ
์์ฒญ์ด ์ฌ๋๋ง๋ค ์ฐ๋ ๋๋ฅผ ์์ฑํ๋ค๋ฉด?
์ฅ์
๋์ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์๋ค.
๋ฆฌ์์ค(CPU, ๋ฉ๋ชจ๋ฆฌ)๊ฐ ํ์ฉํ ๋๊น์ง ์ฒ๋ฆฌ ๊ฐ๋ฅ
ํ๋์ ์ฐ๋ ๋๊ฐ ์ง์ฐ๋์ด๋, ๋๋จธ์ง ์ฐ๋ ๋๋ ์ ์ ๋์
๋จ์
์ฐ๋ ๋๋ ์์ฑ ๋น์ฉ์ด ๋งค์ฐ ๋น์ธ๋ค.
๊ณ ๊ฐ์ ์์ฒญ์ด ๋ค์ด์ฌ ๋๋ง๋ค ์ฐ๋ ๋๋ฅผ ์์ฑํ๋ฉด, ์๋ต ์๋๊ฐ ๋ฆ์ด์ง๋ค.
์ฐ๋ ๋๋ ์ปจํ
์คํธ ์ค์์นญ ๋น์ฉ์ด ๋ฐ์
์ฐ๋ ๋ ์์ฑ์ ์ ํ์ด ์๋ค.
๊ณ ๊ฐ ์์ฒญ์ด ๋๋ฌด ๋ง์ด ์ค๊ฒ๋๋ฉด, CPU, ๋ฉ๋ชจ๋ฆฌ ์๊ณ์ ์ ๋์ด ์๋ฒ๊ฐ ์ฃฝ์ ์ ์๋ค.
์ฐ๋ ๋ ํ
Thread Pool์ ์ฐ๋ ๋๋ฅผ ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋๊ณ ๊ด๋ฆฌํ๋ค. ์ฐ๋ ๋ ํ์ ์์ฑ ๊ฐ๋ฅํ ์ฐ๋ ๋์ ์ต๋์น๋ฅผ ๊ด๋ฆฌํ๋ค.(ํฐ์บฃ์ 200๊ฐ๊ฐ default ๊ฐ)
์ด๋, ์ฐ๋ ๋๊ฐ ํ์ํ๋ฉด ์ด๋ฏธ ์ฐ๋ ๋ํ์ ์์ฑ๋์ด ์๋ ์ฐ๋ ๋๋ฅผ ๊บผ๋ด์ ์ฌ์ฉํ๊ณ ์ฌ์ฉ์ ์๋ฃํ๋ฉด, ์ฐ๋ ๋ ํ์ ํด๋น ์ฐ๋ ๋๋ฅผ ๋ฐ๋ฉํ๋ค. ๋ง์ฝ ์ฐ๋ ๋ ํ์ ์๋ ๋ชจ๋ ์ฐ๋ ๋๊ฐ ์ฌ์ฉ์ค์ด๋ผ๋ฉด, ์์ฒญ์ ๊ฑฐ์ ํ๊ฑฐ๋, ํน์ ์ซ์๋งํผ๋ง ๋๊ธฐํ๋๋ก ์ค์ ํ ์ ์๋ค.
์ด๋ ๊ฒ ์ฐ๋ ๋ ํ์ ์ฌ์ฉํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ฅ์ ์ด ์๋ค.
์ฐ๋ ๋๊ฐ ๋ฏธ๋ฆฌ ์์ฑ๋์ด ์์ด, ์ฐ๋ ๋๋ฅผ ์์ฑํ๊ณ ์ข
๋ฃํ๋ ๋น์ฉ(CPU)์ด ์ ์ฝ๋๊ณ , ์๋ต ์๊ฐ์ด ๋นจ๋ผ์ง๋ค.
์์ฑ ๊ฐ๋ฅํ ์ฐ๋ ๋์ ์ต๋์น๊ฐ ์์ผ๋ฏ๋ก ๋๋ฌด ๋ง์ ์์ฒญ์ด ๋ค์ด์๋ ๊ธฐ์กด ์์ฒญ์ ์์ ํ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค.
์ฌ๊ธฐ์ ์ต๋ ์ฐ๋ ๋ ์(max thread) ์ค์ ์ด ๊ฐ์ฅ ์ฃผ์ ํ๋ ํฌ์ธํธ์ด๋ค.
์ด ๊ฐ์ ๋๋ฌด ๋ฎ๊ฒ ์ค์ ํ๊ฒ๋๋ฉด, ๋์ ์์ฒญ์ด ๋ง์ด ๋ค์ด์์๋ ์๋ฒ ๋ฆฌ์์ค๋ ์ฌ์ ๋กญ์ง๋ง, ํด๋ผ์ด์ธํธ๋ ๊ธ๋ฐฉ ์๋ต ์ง์ฐ์ด๋๋ ํ์์ด ๋ฐ์ํ ์ ์๋ค.
๋ฐ๋๋ก ์ต๋ ์ฐ๋ ๋ ์๋ฅผ ๋๋ฌด ๋๊ฒ ์ค์ ํ๋ฉด, ๋์ ์์ฒญ์ด ๋ง์ด ๋ค์ด์์๋, CPU, ๋ฉ๋ชจ๋ฆฌ ๋ฆฌ์์ค ์๊ณ์ ์ด๊ณผ๋ก ์๋ฒ๊ฐ ๋ค์ด๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์ ์ ์๋ ์ด๋ป๊ฒ ์ฐพ์๊น? ์ ํ๋ฆฌ์ผ์ด์
๋ก์ง์ ๋ณต์ก๋, CPU, ๋ฉ๋ชจ๋ฆฌ, IO ๋ฆฌ์์ค ์ํฉ์ ๋ฐ๋ผ ๋ชจ๋ ๋ค๋ฅด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์ต๋ํ ์ค์ ์๋น์ค์ ์ ์ฌํ๊ฒ ์ฑ๋ฅ ํ
์คํธ๋ฅผ ์๋ํด๋ด์ผํ๋ค. (์ํ์น ab, ์ ์ด๋ฏธํฐ, nGrinder )
์ฆ, ํต์ฌ์ WAS๊ฐ ๋ฉํฐ์ฐ๋ ๋์ ๋ํ ๋ถ๋ถ์ ๋ชจ๋ ์ฒ๋ฆฌํด์ค๋ค๋ ๊ฒ์ด๋ค. ๋ฉํฐ ์ฐ๋ ๋ ๊ด๋ จ ์ฝ๋๋ฅผ ์ ๊ฒฝ์ฐ์ง ์์๋ ๋๋ฉฐ, ๊ฐ๋ฐ์๋ ์ฑ๊ธ ์ฐ๋ ๋ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ฏ์ด ํธ๋ฆฌํ๊ฒ ์์ค ์ฝ๋๋ฅผ ๊ฐ๋ฐํ ์ ์๋ค. ํ์ง๋ง, ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์ด๋ฏ๋ก ์ฑ๊ธํค ๊ฐ์ฒด๋ ์ฃผ์ํด์ ์ฌ์ฉํด์ผํ๋ค.
Servlet life cycle(์๋ช
์ฃผ๊ธฐ)
Servlet์ ์ฌ์ฉ๋๊ฐ ๋์ ์ด์ ๋ ๋น ๋ฅธ ์๋ต ์๋ ๋๋ฌธ์ด๋ค. Servlet์ ์ต์ด ์์ฒญ ์ ๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ ธ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ฉ๋๊ณ , ์ดํ ์์ฒญ์๋ ๊ธฐ์กด์ ๊ฐ์ฒด๋ฅผ ์ฌํ์ฉํ๊ฒ ๋๋ฏ๋ก ๋์ ์๋๊ฐ ๋น ๋ฅด๋ค.
@WebServlet("/LifeCycleEx")
public class LifeCycleEx extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LifeCycleEx() {
super();
// TODO Auto-generated constructor stub
}
// @Override
// public void service(ServletRequest arg0, ServletResponse arg1)
// throws ServletException, IOException {
// // TODO Auto-generated method stub
// System.out.println("service");
// }
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
System.out.println("init");
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doGet");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doPost");
}
}
init
doGet
doGet
doGet
destroy
์๋ก ๊ณ ์นจํ๋ฉด doGet๋ง ๋ฐ๋ณตํด์ ํธ์ถ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ์ด๋ ์์ ์ ํธ์ถ์ด ๋๋์ง ์๊ณ ์์ด์ผํ๋ค.
Servlet ์ ์ฒ๋ฆฌ / ํ์ฒ๋ฆฌ
์ ์ฒ๋ฆฌ @PostConstruct
init์ ์ ํ๋ฒ ํธ์ถ๋๋ ๊ฒ
@PostConstruct
private void initPostConstruct() {
// TODO Auto-generated method stub
System.out.println("initPostConstruct");
}
ํ์ฒ๋ฆฌ @PreDestroy
destroy ํ์ ํธ์ถ๋๋ค.
@PreDestroy
private void destoryPreDestory() {
// TODO Auto-generated method stub
System.out.println("destoryPreDestory");
}
initPostConstruct
init
doGet
doGet
doGet
doGet
destroy
destoryPreDestory
HTML Form tag
formํ๊ทธ๋ ์๋ฒ์ชฝ์ผ๋ก ์ ๋ณด๋ฅผ ์ ๋ฌํ ๋ ์ฌ์ฉํ๋ ํ๊ทธ์ด๋ค.[HTML - form ]ํ๊ทธ์ ๊ด๋ จํด์ ์ ๋ฆฌํด๋ ์๋ฃ์ด๋ค.
<form action="์์ฒญํ๋ ์ปดํฌ๋ํธ ์ด๋ฆ" method=["post"|"get"]>
</form>
์์ฒญํ๋ ์ปดํฌ๋ํธ ์ด๋ฆ์๋ ์ด๋ํ ์์น๋ฅผ ๋ฃ์ด์ค๋ค. (ex) join.jsp, info.html, HWorldServlet Parameter
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="FormMethod" method="get">
์ด๋ฆ : <input type="text" name="name" size="10"><br/>
์์ด๋: <input type="text" name="id" size="10"><br/>
๋น๋ฐ๋ฒํธ : <input type="password" name="pw" size="10"><br/>
์ทจ๋ฏธ : <input type="checkbox" name="hobby" value="read">๋
์
<input type="checkbox" name="hobby" value="cook">์๋ฆฌ
<input type="checkbox" name="hobby" value="run">์กฐ๊น
<input type="checkbox" name="hobby" value="swim">์์
<input type="checkbox" name="hobby" value="sleep">์ทจ์นจ<br/>
<input type="radio" name="major" value="kor">๊ตญ์ด
<input type="radio" name="major" value="eng" checked="checked">์์ด
<input type="radio" name="major" value="mat" >์ํ
<input type="radio" name="major" value="des" >๋์์ธ<br/>
<select name="protocol">
<option value="http">http</option>
<option value="ftp" selected="selected">ftp</option>
<option value="smtp">smtp</option>
<option value="pop">pop</option>
</select><br/>
<input type="submit" value="์ ์ถ"><input type="reset" value="์ด๊ธฐํ">
</form>
</body>
</html>
Servlet Parameter
form ํ๊ทธ์ submit ๋ฒํผ์ ํด๋ฆญํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ์ ์กํ๋ฉด, ํด๋นํ์ผ(servlet)์์๋ HttpServletRequest ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ Parameter๊ฐ์ ์ป์ ์ ์๋ค.
๋ฐํ๊ฐ์ ๋ค String์ด๋ค.
getParameter(name) : name์ ํด๋นํ๋ value๊ฐ์ ๋ฆฌํด
getParameterValues(name) : value๊ฐ๋ค์ด ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ(์ฒดํฌ๋ฐ์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ)์ ์ฌ์ฉ
getParameterNames() : name๋ค์ ๋ฐฐ์ด์ด ๋์ด์จ๋ค.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doPost");
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String[] hobbys = request.getParameterValues("hobby");
String major = request.getParameter("major");
String protocol = request.getParameter("protocol");
response.setContentType("text/html; charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("<html><head></head><body>");
writer.println("์ด๋ฆ : " + name + "<br />");
writer.println("์์ด๋ : " + id + "<br />");
writer.println("๋น๋ฐ๋ฒํธ : " + pw + "<br />" );
writer.println("์ทจ๋ฏธ : " + Arrays.toString(hobbys) + "<br />");
writer.println("์ ๊ณต : " + major + "<br />");
writer.println("ํ๋กํ ์ฝ : " + protocol);
writer.println("</body></html>");
}
}
ServletConfig
ํน์ Servlet์ด ์์ฑ๋ ๋ ์ด๊ธฐ์ ํ์ํ ๋ฐ์ดํฐ๋ค์ ์ด๊ธฐํ ํ๋ผ๋ฏธํฐ์ด๋ค.
web.xml
์ ๊ธฐ์ ํ๊ณ Servletํ์ผ์์๋ ServletConfing
ํด๋์ค๋ฅผ ์ด์ฉํด์ ์ ๊ทผ(์ฌ์ฉ)ํ๋ค.
+web.xml
์ด ์๋ Servletํ์ผ์ ์ง์ ๊ธฐ์ ํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
web.xml์ด์ฉํ๊ธฐ
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsp_8_1_ex1_initparamex</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletInitParam</servlet-name>
<servlet-class>com.javalec.ex.ServletInitParam</servlet-class>
<init-param>
<param-name>id</param-name>
<param-value>abcdef</param-value>
</init-param>
<init-param>
<param-name>pw</param-name>
<param-value>1234</param-value>
</init-param>
<init-param>
<param-name>path</param-name>
<param-value>C:\\javalec\\workspace</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletInitParam</servlet-name>
<url-pattern>/InitParam</url-pattern>
</servlet-mapping>
</web-app>
package com.javalec.ex;
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;
/**
* Servlet implementation class ServletInitParam
*/
//@WebServlet("/ServletInitParam")
public class ServletInitParam extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletInitParam() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doGet");
String id = getInitParameter("id");
String pw = getInitParameter("pw");
String path = getInitParameter("path");
response.setContentType("text/html; charset=EUC-KR");
PrintWriter writer = response.getWriter();
writer.println("<html><head></head><body>");
writer.println("์์ด๋ค : " + id + "<br />");
writer.println("๋น๋ฐ๋ฒํธ : " + pw + "<br />");
writer.println("path : " + path);
writer.println("</body></html>");
writer.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doPost");
}
}
Servlet ํ์ผ์ ์ง์ ๊ธฐ์ ํ๊ธฐ
@WebServlet(urlPatterns={"/ServletInitParam"}, initParams={@WebInitParam(name="id", value="abcdef"), @WebInitParam(name="pw", value="1234")})
๋ฐ์ดํฐ ๊ณต์ : ServletContext
์ฌ๋ฌ Servlet์์ ํน์ ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํด์ผํ ๊ฒฝ์ฐ context parameter๋ฅผ ์ด์ฉํด์ web.xml
์ ๊ธฐ์ ํ๊ณ , Servlet์์ ๊ณต์ ํ๋ฉด์ ์ฌ์ฉํ ์ ์๋ค.
์ด๋ <servlet>
๋ณด๋ค ์์ชฝ์ ์์ด์ผํ๋ค.
<context-param>
<param-name>id</param-name>
<param-value>abcdef</param-value>
</context-param>
<context-param>
<param-name>pw</param-name>
<param-value>1234</param-value>
</context-param>
<context-param>
<param-name>path</param-name>
<param-value>C:\javalec\workspace</param-value>
</context-param>
String id = getServletContext().getInitParameter("id");
String pw = getServletContext().getInitParameter("pw");
String path = getServletContext().getInitParameter("path");
์น์ดํ๋ฆฌ์ผ์ด์
๊ฐ์
์น์ดํ๋ฆฌ์ผ์ด์
์ ์๋ช
์ฃผ๊ธฐ(LifeCycle)๋ฅผ ๊ฐ์ํ๋ ๋ฆฌ์ค๋(Listener)๊ฐ ์์ต๋๋ค. ๋ฐ๋ก ServletContextListener ์
๋๋ค.
๋ฆฌ์ค๋์ ํด๋น ๋ฉ์๋๊ฐ ์น ์ดํ๋ฆฌ์ผ์ด์
์ ์์๊ณผ ์ข
๋ฃ ์ ํธ์ถ ๋ฉ๋๋ค.(contextInitialized(), contextDestroyed())
(ํด๋์ค ์์ฑ + implements ServletContextListener)๋ฅผ ํด์ค์ผํ๋ค.
<listener>
<listener-class>com.javalec.ex.ContextListenerEx</listener-class>
</listener>
package com.javalec.ex;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ContextListenerEx implements ServletContextListener{
public ContextListenerEx() {
// TODO Auto-generated constructor stub
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("contextDestroyed");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("contextInitialized");
}
}
ํ๊ธ ์ฒ๋ฆฌ
Tomcat ์๋ฒ์ ๊ธฐ๋ณธ ๋ฌธ์ ์ฒ๋ฆฌ๋ฐฉ์์ IOS-8859-1 ๋ฐฉ์์ด๋ค. ๋ฐ๋ผ์ ๊ฐ๋ฐ์๊ฐ ๋ฐ๋ก ํ๊ธ ์ธ์ฝ๋ฉ์ ํ์ง์์ผ๋ฉด ํ๊ธ์ด ๊นจ์ ธ๋ณด์ธ๋ค.
Get๋ฐฉ์ - server.xml ์์
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8181" protocol="HTTP/1.1" redirectPort="8443"/>
Post ๋ฐฉ์ - .setCharacterEncoding
request.setCharacterEncoding("utf-8");