[Servlet] Request Parameter ์ฝ๊ธฐ

์†Œ์ด๋Žยท2023๋…„ 11์›” 28์ผ
1

Servlet

๋ชฉ๋ก ๋ณด๊ธฐ
9/13
post-thumbnail
  • ๐Ÿšฉ GET ์š”์ฒญ, POST ์š”์ฒญ ์‹œ ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ“ getParameter(String name)

  • ์ฃผ์–ด์ง„ 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>

๐Ÿ“ getParameterValues(String name)

  • 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>

References

๐Ÿ”— https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html

0๊ฐœ์˜ ๋Œ“๊ธ€