๐ป
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (1) ๋ณธ๋ฌธ
Servlet
์๋ธ๋ฆฟ์ด๋? ์๋ฐ ์น ์ดํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ฑ์์ ์ค ๋์ ์ธ ์ฒ๋ฆฌ๋ฅผ ํ๋ ํ๋ก๊ทธ๋จ์ ์ญํ
- ์๋ธ๋ฆฟ์ WAS์ ๋์ํ๋ JAVA ํด๋์ค์ด๋ค.
- ์๋ธ๋ฆฟ์ HttpServlet ํด๋์ค๋ฅผ ์์๋ฐ์์ผ ํ๋ค.
- ์น ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋ ํ๋ฉด(HTML)์ JSP๋ก ํํํ๊ณ , ๋ณต์กํ ํ๋ก๊ทธ๋๋ฐ์ ์๋ธ๋ฆฟ์ผ๋ก ๊ตฌํํ๋ค.
https://javaee.github.io/javaee-spec/javadocs/
Q. ์ธ์ ์๋ฒ๋ฉ๋ชจ๋ฆฌ์์ ํ์ผ์ด ๋ด๋ ค์ฌ๊น?!
1. ์๋ฒ๋ฅผ ๊บผ๋ฒ๋ ธ์ ๋
2. ์์ค๋ฅผ ๋ณ๊ฒฝํด์ ์ ๋ฐ์ดํธ ํ ๋
์๋ฒ๋ ํ์ผ๋จ์์ด๋ค. ํ๋์ ์ปจํ ์ด๋ ์์ ํ๋์ ์ปจํ ์คํธ(Context)๋ฅผ ๊ฐ์ง๊ณ ์๋ค. servlet, java, html, css, javascript ... ๋ฑ๋ฑ ํ์ผ์ด ์ฌ๋ผ๊ฐ์๋ค.
Servlet ์๋ช ์ฃผ๊ธฐ
- ์์ฑ init() -> ์๋น์ค service() -> ์๋ฉธ destroy()
- WAS๋ ์๋ธ๋ฆฟ ์์ฒญ์ ๋ฐ์ผ๋ฉด ํด๋น ์๋ธ๋ฆฟ์ด ๋ฉ๋ชจ๋ฆฌ์ ์๋์ง ํ์ธํฉ๋๋ค.
https://javaee.github.io/javaee-spec/javadocs/
Request, Response ๊ฐ์ฒด ์ดํดํ๊ธฐ
-
์์ฒญ๊ณผ ์๋ต
-
WAS๋ ์น ๋ธ๋ผ์ฐ์ ๋ก๋ถํฐ Servlet์์ฒญ์ ๋ฐ์ผ๋ฉด,
-
์์ฒญํ ๋ ๊ฐ์ง๊ณ ์๋ ์ ๋ณด๋ฅผ HttpServletRequest๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์ ์ฅํ๋ค.
-
์น ๋ธ๋ผ์ฐ์ ์๊ฒ ์๋ต์ ๋ณด๋ผ ๋ ์ฌ์ฉํ๊ธฐ ์ํ์ฌ HttpServletResponse๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
-
์์ฑ๋ HttpServletRequest, HttpServletResponse ๊ฐ์ฒด๋ฅผ ์๋ธ๋ฆฟ์๊ฒ ์ ๋ฌํ๋ค.
- ์น ๋ธ๋ผ์ฐ์ ๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ์ํด request, response ๋ฅผ ์ฌ์ฉ
-
-
HttpServletRequest
-
httpํ๋กํ ์ฝ์ request์ ๋ณด๋ฅผ ์๋ธ๋ฆฟ์๊ฒ ์ ๋ฌํ๊ธฐ ์ํ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉํ๋ค.
-
ํค๋์ ๋ณด, ํ๋ผ๋ฏธํฐ, ์ฟ ํค, URI, URL ๋ฑ์ ์ ๋ณด๋ฅผ ์ฝ์ด ๋ค์ด๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
-
Body์ Stream์ ์ฝ์ด ๋ค์ด๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
-
-
HttpServletResponse
-
WAS๋ ์ด๋ค ํด๋ผ์ด์ธํธ๊ฐ ์์ฒญ์ ๋ณด๋๋์ง ์๊ณ ์๊ณ , ํด๋น ํด๋ผ์ด์ธํธ์๊ฒ ์๋ต์ ๋ณด๋ด๊ธฐ ์ํ HttpServleResponse๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์๋ธ๋ฆฟ์๊ฒ ์ ๋ฌํ๋ค.
-
์๋ธ๋ฆฟ์ ํด๋น ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ content type, ์๋ต์ฝ๋, ์๋ต ๋ฉ์์ง๋ฑ์ ์ ์กํ๋ค.
-
01_URL.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- GET ๋ฐฉ์ -->
<a href="http://localhost:8181/webTesting/com/java/servlet/Example01">
Example01
</a>
<br /><br />
<a href="http://localhost:8181/webTesting/com/java/servlet/Example02?su=10">
Example02
</a>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>webTesting</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>Example01</servlet-name>
<servlet-class>com.java.servlet.Example01</servlet-class>
</servlet>
<servlet>
<servlet-name>Example02</servlet-name>
<servlet-class>com.java.servlet.Example02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example01</servlet-name>
<url-pattern>/com/java/servlet/Example01</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Example02</servlet-name>
<url-pattern>/com/java/servlet/Example02</url-pattern>
</servlet-mapping>
</web-app>
mapping์ฐ๊ฒฐ html๊ณผ servlet์ ์ฐ๊ฒฐ
Example01.java
package com.java.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example01 extends HttpServlet{
private static final long serialVersionUID = 1L;
// ์๋ฒ ๋ฉ๋ชจ๋ฆฌ : Content ๋จ์๋ก ์ฒ๋ฆฌํ๋ค. ๊ฐ๊ฐ์ Content ์์ Context(webTestingํ๋ก์ ํธ)๊ฐ ์๋ค.
public Example01() {
super();
}
// ์๋ช
์ฃผ๊ธฐ - ์ด๊ธฐํ init() -> ์๋น์ค service() -> ์๋ฉธ destroy()
public void init() {
System.out.println("์๋ธ๋ฆฟ ํ์ผ ์ด๊ธฐํ(์ฒ์ ์์ฒญ ์ ๋จ ํ๋ฒ)");
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// ์๋น์ค - ์์ฒญ๋ฐฉ์์ ์ํด์ get, post ํจ์ ํธ์ถ
doGet(request, response);
}
public void doGet(HttpServletRequest req, HttpServletResponse response) {
System.out.println("...์๋ธ๋ฆฟ ๋ด์ฉ doGet ํจ์์
๋๋ค.");
}
public void destroy() {
System.out.println("์๋ธ๋ฆฟ ํ์ผ ์๋ฉธ");
}
}
์น ํ์ด์ง์ ์์ฒญ์ ๋ด๋ถ ์ฐ๋ ๋, service() ๋ด๋ถ์ ์ผ๋ก ์ฐ๋ ๋ ์ฒ๋ฆฌ -> ํฐ์บฃ์ด ์์์ ์ฒ๋ฆฌ
init(), service(), destroy() ๋ ์์ฑํ์ง ์์ผ๋ฉด ๋ํดํธ๋ก ๋ด๋ถ์ ์ผ๋ก ์คํ๋๋ค.
init() ๋ฉ์๋์ ๊ฒฝ์ฐ ์ด๊ธฐํํ ๋ ์ค์ ํ๊ธฐ ์ํด ์์ฑํ๋ค.
Example02.java
package com.java.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example02 extends HttpServlet{
private static final long serialVersionUID = 1L;
// init()-service()-doGet()-destroy()
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String su = request.getParameter("su");
System.out.println("su:" + su);
// ๋ฌธ์์ด์ ์ ์๋ก ๋ณํ
int value = Integer.parseInt(su);
String a = String.valueOf(value);
// ์ ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ
Integer i = value;
String b = i.toString();
System.out.println(a +"\t" + b);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>webTesting</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>Example01</servlet-name>
<servlet-class>com.java.servlet.Example01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example01</servlet-name>
<url-pattern>/com/java/servlet/Example01</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Example02</servlet-name>
<servlet-class>com.java.servlet.Example02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example02</servlet-name>
<url-pattern>/com/java/servlet/Example02</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Example03</servlet-name>
<servlet-class>com.java.servlet.Example03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example03</servlet-name>
<url-pattern>/com/java/servlet/Example03</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Example04</servlet-name>
<servlet-class>com.java.servlet.Example04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example04</servlet-name>
<url-pattern>/com/java/servlet/Example04</url-pattern>
</servlet-mapping>
</web-app>
03_example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8181/webTesting/com/java/servlet/Example03" method="post">
<label>๋ฉ์ธ์ง</label>
<input type="text" name="msg" />
<br /><br />
<input type="submit" value="์ ์ก"/>
<input type="reset" value="์ทจ์" />
</form>
</body>
</html>
Example03.java
package com.java.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example03 extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public Example03() {
super();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = req.getParameter("msg");
System.out.println(msg);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String msg = req.getParameter("msg");
System.out.println(msg);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("service() ์๋ต๊ฐ๋ฅ, ์ค๋ ๋ ์ฒ๋ฆฌ๋์ ๋ฐ๋ณต ์คํ");
//doGet(req, resp);
doPost(req, resp);
}
@Override
public void destroy() {
System.out.println("destroy() ์๋ต๊ฐ๋ฅ, ์๋ฒ์ข
๋ฃ ๋ฐ ์
๋ฐ์ดํธ ์");
}
@Override
public void init() throws ServletException {
System.out.println("init() ์๋ต๊ฐ๋ฅ, ์๋ฒ๋ฉ๋ชจ๋ฆฌ์ ์ฒ์์๋ง ์คํ๋๋ค.");
}
}
04_example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8181/webTesting/com/java/servlet/Example04" method="get">
<label>์ด๋ฆ</label>
<input type="text" name="name"/>
<br /><br />
<label>์ ํ๋ฒํธ</label>
<input type="text" name="phone"/>
<br /><br />
<label>์ฃผ์</label>
<input type="text" name="addr" size="100"/>
<br /><br />
<input type="submit" value="์ ์ก"/>
<input type="reset" value="์ทจ์" />
</form>
</body>
</html>
Example04.java
package com.java.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example04 extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String phone = req.getParameter("phone");
String addr = req.getParameter("addr");
System.out.println(name+ "\t" + phone + "\t" + addr);
}
}
05_example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function checkForm(obj){
if(obj.buho.value != "+" & obj.buho.value !="-" & obj.buho.value !="*" & obj.buho.value !="/"){
alert("๋ถํธ๋ฅผ ์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค.");
obj.buho.focus();
return false;
}
if(obj.buho.value == "/"){
if(obj.value.value==0){
alert("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.");
obj.value.focus();
return false;
}
}
}
</script>
</head>
<body>
<form action="http://localhost:8181/webTesting/com/java/servlet/Example05"
method="post" onsubmit="return checkForm(this)">
<label>์</label>
<input type="text" name="su"/>
<br /><br />
<label>๋ถํธ</label>
<input type="text" name="buho"/>
<br /><br />
<label>์</label>
<input type="text" name="value"/>
<br /><br />
<input type="submit" value="์ ์ก"/>
<input type="reset" value="์ทจ์" />
</form>
</body>
</html>
Example05.java
package com.java.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example05 extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int su = Integer.parseInt(req.getParameter("su"));
String buho = req.getParameter("buho");
//char ch = buho.charAt(0); //if(ch =='+'){}
int value = Integer.parseInt(req.getParameter("value"));
System.out.println(su + "\t" + buho + "\t" + value);
String result = "";
if(buho.equals("+")) {
/*
* Integer r = su+value;
* result = r.toString();
*/
result = String.valueOf(su+value);
}else if(buho.equals("-")){
result = String.valueOf(su-value);
}else if(buho.equals("*")){
result = String.valueOf(su*value);
}else if(buho.equals("/")){
result = String.valueOf(su/value);
}
System.out.println(result);
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("<html>");
out.print("<head><title>์ฌ์น์ฐ์ฐ</title></head>");
out.print("<body>");
out.print("<h3> ๊ฒฐ๊ณผ:" + result + "</h3>");
out.print("</body>");
out.print("</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
'KITRI > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JSP ํ๋ก๊ทธ๋๋ฐ - JSTL(JSP Standard Tag Library) ๋ค์ด๋ก๋ / c:set, c:remove (0) | 2020.06.10 |
---|---|
JSP ํ๋ก๊ทธ๋๋ฐ - EL, JSTL (0) | 2020.06.10 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Cookie(์ฟ ํค)/ Session(์ธ์ ) (0) | 2020.06.09 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet ๊ฐ์ฒด ๋ฒ์ (0) | 2020.06.09 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (2) (0) | 2020.06.08 |