- ๐ฉ GET ์์ฒญ, POST ์์ฒญ ์ ๋ชจ๋ ๋์ผํ๊ฒ ์ฌ์ฉํ๋ค.
- ์ฃผ์ด์ง name์ ํด๋นํ๋ ๋จ์ผ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ฐํ
// ๐ข src/main/java/com/test/lesson01/GetMethodEx04.java
package com.test.lesson01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/lesson01/ex04")
public class GetMethodEx04 extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
String data = request.getParameter("data"); // โญ๏ธ ์ฟผ๋ฆฌ ์คํธ๋ง ์ฝ์
PrintWriter out = response.getWriter();
out.println("Servlet ์คํ");
out.println(data); // โญ๏ธ ํ๋ฉด์ ์ถ๋ ฅ
}
}
<;!-- ๐ src/main/webapp/lesson01/ex04.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ex04</title>
</head>
<body>
<h1>HTML ํ์ผ</h1>
<form method="get" action="/lesson01/ex04">
<label>
Servlet์ผ๋ก ๋ณด๋ผ ๋ฐ์ดํฐ:
<input type="text" name="data">
</label>
<button type="submit">๋ณด๋ด๊ธฐ</button>
</form>
</body>
</html>
- name์ ํด๋นํ๋ ๋ชจ๋ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋ฐํ
<%-- ๐ค src/main/webapp/lesson02/ex03_1.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ex03_1</title>
</head>
<body>
<%
String[] animalArr = request.getParameterValues("animal");
for(String animal : animalArr) {
out.print(animal + " ");
}
%>
</body>
</html>
<%-- ๐ค src/main/webapp/lesson02/ex03.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ex03</title>
</head>
<body>
<form method="post" action="/lesson02/ex03_1.jsp">
<label for="dog">๊ฐ์์ง</label>
<input type="checkbox" id="dog" name="animal" value="dog">
<label for="cat">๊ณ ์</label>
<input type="checkbox" id="cat" name="animal" value="cat">
<label for="rabbit">ํ ๋ผ</label>
<input type="checkbox" id="rabbit" name="animal" value="rabbit">
<button type="submit">์ ์ถํ๊ธฐ</button>
</form>
</body>
</html>
๐ https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html