์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- ์ค๋ผํด
- css
- ์นํ์ด์ง๋ง๋ค๊ธฐ
- ๋ ์
- Python
- ๋ผํ๋ผ์ค์๋ง๋
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ์ค๋ธ์
- ์ฝ๋ฉ
- ์๋ฐ์คํฌ๋ฆฝํธ
- html
- ํ์ด์ฌ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ๋ง์ผ๋ด๊ฐ์ธ์์๋ค์์ฐ๋ค๋ฉด
- JavaScript
- ์๋ฐ
- ๊ฐ๋ฐ
- ๋ฆฌ์กํธ
- ์ปดํจํฐ๊ณผํ
- ์ํ
- K๋ฐฐํฐ๋ฆฌ
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- ์ฑ
- ์นํผ๋ธ๋ฆฌ์ฑ
- ํ๋ก๊ทธ๋๋ฐ
- Java
- ๋ฐ์ํ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ComputerScience
- database
- Today
- Total
JiYoung Dev ๐ฅ
[JSP/Servlet] Servlet ๊ฐ์, ๋ฐ์ดํฐ ์ ์ก (2023.05.10) ๋ณธ๋ฌธ
[JSP/Servlet] Servlet ๊ฐ์, ๋ฐ์ดํฐ ์ ์ก (2023.05.10)
Shinjio 2023. 5. 12. 18:23๐ request์ response
๐ ๋ฐ์ดํฐ ์ ์ก
request : ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๊ฐ์ ์ด๋ป๊ฒ ๋ณด๋ผ๊ฒ์ธ๊ฐ๊ฐ ์ค์
action ์ด๋๋ก ์์ฒญํ ๊ฑด์ง
method ์์ฒญ ๋ฐฉ์ (foam ํ๊ทธ๋ get / post ๋ง ์ฌ์ฉ ๊ฐ๋ฅ, default ๋ get์ผ๋ก ์๋ต ๊ฐ๋ฅ)
name input ํ๊ทธ๋ค์ ๋ช ์นญ, ๊ฐ๊ฐ์ ๊ฐ์ ๊ตฌ๋ณํ ์ ์๋๋ก ์ง์ ํด์ฃผ๋ ์ด๋ฆ (์ด๋ฆ์ด ์์ผ๋ฉด ํ๊ทธ๋ฅผ ๊ตฌ๋ณํ ์ ์์)
submit ์ ์ถ > ๋ฒํผ ๋๋ฅด๋ ์๊ฐ action์ ๊ฒฝ๋ก๋ก ์์ฒญ
๐ request.getParameter()
package com.smhrd.servlet;
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("/Ex01")
public class Ex01 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ์์ฒญ ๋ฐ์ดํฐ(name = data) ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
// getParameter(String : foam์ name๊ฐ)
// ์์ฒญ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ >> ๋ฐํํ์
: String
String data = request.getParameter("data");
System.out.println(data);
// ์๋ต๋ฐ์ดํฐ ์ธ์ฝ๋ฉ ์ค์ ์ถ๊ฐ
response.setCharacterEncoding("UTF-8"); //์๋ต๊ฐ๋ฅ
// ์น๋ธ๋ผ์ฐ์ ๊ฐ ํ๊ธ ์ธ์ฝ๋ฉ์ ํ ์ ์๋๋ก ์๋ ค์ฃผ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ผ ํจ!
// >> ๋ธ๋ผ์ฐ์ ์๊ฒ ์๋ต๋ฐ์ดํฐ์ ํ์๊ณผ ์ธ์ฝ๋ฉ ๋ฐฉ์์ ์๋ ค์ฃผ๋ ์ฝ๋
response.setContentType("text/html; charset=UTF-8");
// ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ฐ ๊ทธ๋๋ก html ๋ฌธ์๋ก ๋ง๋ค์ด์ ์๋ตํ๊ธฐ
PrintWriter out = response.getWriter(); //ํ
์คํธ ์ถ๋ ฅ ์คํธ๋ฆผ ๋ง๋ค๊ธฐ
out.print(data);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- action : ์ด๋๋ก ์์ฒญํ ๊ฑด์ง(๊ฒฝ๋ก)
method : http ์์ฒญ ๋ฉ์๋ (get/post) -->
<!-- action : ๋ธ๋ผ์ฐ์ ์ฃผ์์ฐฝ์ ๋จ๋ ์ฃผ์ ๊ฒฝ๋ก ๊ธฐ์ค ์๋๊ฒฝ๋ก -->
<form action="Ex01" method="get">
<input type="text" name="data"> <!-- name : ์๋ฒ์์ ๊ฐ์ ์๋ณํ ์ ์๋๋ก -->
<input type="submit" value="์ ์ก">
</form>
</body>
</html>
๐ ์๋ต ๋ฐ์ดํฐ ์ธ์ฝ๋ฉํ๊ธฐ
๐ ์ค์ต
HTML ํ์ผ์ webapp ํด๋ ๋ด์ ์ ์ฅ!
์ค์ต1.
package com.smhrd.servlet;
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("/Ex02Number")
public class Ex02Number extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String number = request.getParameter("number");
System.out.println(number);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("์
๋ ฅํ ์ซ์๋ <b>" + number + "</b>์
๋๋ค");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Ex02Number" method="get">
<input type="number" name="number">
<input type="submit" value="SEND">
</form>
</body>
</html>
์ค์ต2.
package com.smhrd.servlet;
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("/Ex03")
public class Ex03 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ์์ฒญ ๋ฐ์ดํฐ 2๊ฐ(num1, num2) ๋ฐ๊ธฐ
String num1 = request.getParameter("num1");
// String์ ์ ์ํ์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ
// 1) Integer (intํ์ ํด๋์ค ํ์
์ผ๋ก ๋ค์ํ ๊ธฐ๋ฅ ๋ณด์ )
// >> parsInt(String) : ๋ฌธ์์ด์ ์ ์ํ์ผ๋ก ๋ณํ
// >> ์ซ์๋ก ๋ณํ ๋ถ๊ฐ๋ฅํ ๋ฌธ์์ด์ ๋ฃ๋ ๊ฒฝ์ฐ(ex.ใฑ, ใด) ์ค๋ฅ ๋ฐ์
// >> ์ฌ์ฉ์ ๋ฌธ์์ด ์์ ์ซ์๋ง ํฌํจ๋๋๋ก ์ฃผ์!
int int_num1 = Integer.parseInt(num1);
int int_num2 = Integer.parseInt(request.getParameter("num2"));
// ์๋ต
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(int_num1 + " + " + int_num2 + " = " + (int_num1 + int_num2));
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Ex03">
<input type="number" name="num1">
+
<input type="number" name="num2">
<input type="submit" value="๋ํ๊ธฐ!">
</form>
</body>
</html>
์ค์ต3.
package com.smhrd.servlet;
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("/Ex04")
public class Ex04 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int int_num1 = Integer.parseInt(request.getParameter("num1"));
String ope = request.getParameter("ope");
int int_num2 = Integer.parseInt(request.getParameter("num2"));
System.out.println(ope);
// HTML์์ val๊ฐ์ ์์ฑํ๋ฉด value๊ฐ์ ๋ฐ์์ด
response.setContentType("text/html; charset=UTF-8");
int result = 0;
String operator;
if(ope.equals("plus")) {
result = int_num1 + int_num2;
operator = "+";
}else if(ope.equals("minus")) {
result = int_num1 - int_num2;
operator = "-";
}else if(ope.equals("mul")) {
result = int_num1 * int_num2;
operator = "*";
}else {
result = int_num1 / int_num2;
operator = "/";
}
String str = int_num1 + " " + operator + " " + int_num2 + " = " + result ;
PrintWriter out = response.getWriter();
out.print(str);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="Ex04">
<input type="number" name="num1">
<select name="ope">
<option value="plus">+</option>
<option value="minus">-</option>
<option value="mul">*</option>
<option value="div">/</option>
</select>
<input type="number" name="num2">
<input type="submit" value="SEND">
</form>
</body>
</html>
์ค์ต4.
package com.smhrd.servlet;
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("/Ex05Table")
public class Ex05Table extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ์์ฒญ
// ์๋ต
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<table border=1>");
out.print("<tr>");
for(int i = 1; i <= 6 ; i++) {
out.print("<td>" + i + "</td>");
}
// out.print("<td>1</td>");
// out.print("<td>2</td>");
// out.print("<td>3</td>");
// out.print("<td>4</td>");
// out.print("<td>5</td>");
// out.print("<td>6</td>");
out.print("</tr>");
out.print("</table>");
}
}
์ค๋ฅ ์. 404
WEB-INF ๋ ํด๋ผ์ด์ธํธ(์น๋ธ๋ผ์ฐ์ )๊ฐ ์ ๋ ์ ๊ทผํ ์ ์์ > ๊ฒฝ๋ก๋ ๋ง์ง๋ง ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ฏ๋ก ๊ฒฝ๊ณ ๋ฐ์
๋ฐ๋ผ์ htmlํ์ผ์ด WEB-INF ํด๋ ๋ด์ ๋ค์ด๊ฐ์ง ์๋๋ก ์ฃผ์!
์ค๋ฅ ์. 500
์ค๋ฅ๋ ๋ด๊ฐ ์ด ์ฝ๋์์ ํ์ธ์ ํ ์ ์๋ค!
์ ๋ ฅํ ๊ฐ์ด ์์ด ์คํ >> ์ ๋ ฅํ ๊ฐ์ด ์๊ธฐ ๋๋ฌธ์ null์ ๊ฐ์ง >> ์์ธ์ํฉ ๋ฐ์
๋ฐํ์์ค๋ฅ : ์คํํ์ ๋ ๋ฐ์ํ๋ ์ค๋ฅ
try ~ catch๋ก ์์ธ์ฒ๋ฆฌ
'full stack > Back-End' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JSP/Servlet] Scope (2023.05.18~19) (0) | 2023.05.19 |
---|---|
[JSP/Servlet] ์ ์ธ๋ฌธ, ์ง์์, ๋ด์ฅ๊ฐ์ฒด (2023.05.16) (0) | 2023.05.16 |
[JSP/Servlet] JSP ํ์์ฑ๊ณผ ํน์ง, ๊ตฌ์ฑ์์ (2023.05.15) (0) | 2023.05.15 |
[JSP/Servlet] ๋ฐ์ดํฐ ์ ์ก GET/POST(2023.05.12) (0) | 2023.05.12 |
[JSP/Servlet] Server์ Servlet ๊ฐ์ (2023.05.09) (0) | 2023.05.09 |