Form tag

์ฝœ๋ผ๋ง˜ยท2023๋…„ 6์›” 15์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
5/17
post-thumbnail

๐Ÿ Form tag

  • ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์–ด์„œ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ํƒœ๊ทธ
  • form ๋‚ด๋ถ€์˜ input ํƒœ๊ทธ๋“ค์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’๋“ค์„ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๊ฒŒ ๋œ๋‹ค
  • ์‹ค์–ด ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ๋Š” ์„œ๋ฒ„์ธก์˜ request ๊ฐ์ฒด์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค
  • ๋ฐ์ดํ„ฐ๋Š” Key-Value ๋ฐฉ์‹์œผ๋กœ ์ „์†ก๋œ๋‹ค
  • input ํƒœ๊ทธ์˜ name ์†์„ฑ ๊ฐ’์ด Key, value ์†์„ฑ ๊ฐ’์ด value ์—ญํ• ์„ ํ•œ๋‹ค
  • action ์†์„ฑ : ์š”์ฒญ์„ ๋ณด๋‚ผ url์„ ์ ๋Š” ์†์„ฑ, ํ•ด๋‹น form ํƒœ๊ทธ ๋‚ด๋ถ€์—์„œ submit์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค
  • ํ•˜๋‚˜์˜ name์— ์—ฌ๋Ÿฌ value๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒฝ์šฐ๋„ ์กด์žฌํ•œ๋‹ค(ex: checkbox)
  • ์ „์†ก ๋ฒ„ํŠผ : ๋ˆ„๋ฅด๋ฉด action์— ์„ค์ •ํ•œ ์ฃผ์†Œ๋กœ ์ „์†ก๋œ๋‹ค

๐Ÿ ๋‹ค๋ฅธ JSP ํŒŒ์ผ๋กœ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์š”์ฒญ ๋ณด๋‚ด๋ณด๊ธฐ

  • form ํƒœ๊ทธ๋กœ ์ „์†ก ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ request ๊ฐ์ฒด์—์„œ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค
  • ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ฌด์กฐ๊ฑด ๋ฌธ์ž์—ด ํƒ€์ž…์œผ๋กœ ๊บผ๋‚ด์ง€๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค
  • checkbox ์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ name์— ๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ getParameter() ๋Œ€์‹  getParameterValues()๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค

๐Ÿ’ฌ ์ฝ”๋“œ >>

โœ” index.jsp (ํ˜„์žฌ ํŒŒ์ผ : ํด๋ผ์ด์–ธํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ form์œผ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๊ณณ)

	<form action="http://localhost:8090/chap02/form/order.jsp">
		<select name="drink">
			<option value="Americano">์•„๋ฉ”๋ฆฌ์นด๋…ธ</option>
			<option value="GreenTea">๋…น์ฐจ</option>
			<option value="RedTea">ํ™์ฐจ</option>
			<option value="Water">๋ฌผ</option>
		</select>
		<div>
			<input type="checkbox" id="op1" name="drink-option" value="ice"/>
			<label for="op1">์–ผ์Œ ๋งŽ์ด</label> <br>
			<input type="checkbox" id="op2" name="drink-option" value="syrup"/>
			<label for="op2">์‹œ๋Ÿฝ ์ถ”๊ฐ€</label> <br>
			<input type="checkbox" id="op3" name="drink-option" value="shot"/>
			<label for="op3">์ƒท ์ถ”๊ฐ€</label> <br>
		</div>
		<input type="number" name="qty" value="3" />
		<input type="submit" /> 
	</form>

โœ” order.jsp (๋‹ค๋ฅธ jspํŒŒ์ผ : ํ˜„์žฌ ํŒŒ์ผ์—์„œ form์„ ํ†ตํ•ด ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญ๊ณผ ํ•จ๊ป˜ ๋ฐ›๋Š”๋‹ค)

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    
<% 
	String drink = request.getParameter("drink");
	String qty = request.getParameter("qty");
	String[] drinkOptions = request.getParameterValues("drink-option"); // ๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>์ฃผ๋ฌธ ํ™•์ธ ํŽ˜์ด์ง€</title>
</head>
<body>
	<h1>์ฃผ๋ฌธ ํ™•์ธ</h1>
	
	<p><%=drink %> x <%=qty %></p><br>
	์„ ํƒํ•œ ์˜ต์…˜ : 
	<%
		// ์•„๋ฌด ๊ฒƒ๋„ ์„ ํƒ์„ ์•ˆํ•˜๋Š” ๊ฒฝ์šฐ null ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฏ€๋กœ 
		// null ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๋‹ค 
		if(drinkOptions != null){ // drinkOptions๊ฐ€ null์ผ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ
			for(String drinkOption : drinkOptions){
				out.print(drinkOption + ", ");
			}
		}
	%>
</body>
</html>

๐Ÿ“บ ์‹คํ–‰ํ™”๋ฉด >>


โœ… submit์„ ๋ˆ„๋ฅด๋ฉด >>


โ— submit์„ ๋ˆ„๋ฅด๋ฉด form์˜ action์†์„ฑ์— ์ ์€ url๋’ค์— ์–นํ˜€์„œ ์ „์†ก๋œ๋‹ค


๐Ÿ Servlet์œผ๋กœ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์š”์ฒญ ๋ณด๋‚ด๋ณด๊ธฐ

๐Ÿ’ฌ ์ฝ”๋“œ >>

โœ” jsp >>

<h3># Servlet์œผ๋กœ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์š”์ฒญ ๋ณด๋‚ด๋ณด๊ธฐ</h3>
	<form action="http://localhost:8090/chap02/form/order">
		<select name="drink">
			<option value="Americano">์•„๋ฉ”๋ฆฌ์นด๋…ธ</option>
			<option value="GreenTea">๋…น์ฐจ</option>
			<option value="RedTea">ํ™์ฐจ</option>
			<option value="Water">๋ฌผ</option>
		</select>
		<div>
			<input type="checkbox" id="sop1" name="drink-option" value="ice"/>
			<label for="sop1">์–ผ์Œ ๋งŽ์ด</label> <br>
			<input type="checkbox" id="sop2" name="drink-option" value="syrup"/>
			<label for="sop2">์‹œ๋Ÿฝ ์ถ”๊ฐ€</label> <br>
			<input type="checkbox" id="sop3" name="drink-option" value="shot"/>
			<label for="sop3">์ƒท ์ถ”๊ฐ€</label> <br>
		</div>
		<input type="number" name="qty" value="3" />
		<input type="submit" />
	</form>

โœ” java(์„œ๋ธ”๋ฆฟ) >>

import java.io.IOException;
import java.util.Arrays;

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("/form/order")
public class OrderServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// ์„œ๋ธ”๋ฆฟ์—์„œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ HttpServletRequest ์ธ์Šคํ„ด์Šค์—์„œ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค
		// (โ€ป .jsp ํŒŒ์ผ์—์„œ๋Š” ํ•ด๋‹น ์ธ์Šคํ„ด์Šค ๋ช…์ด request๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ์Œ)
		// jsp๋Š” 1์ฐจ๋กœ ์„œ๋ธ”๋ฆฟํ˜•ํƒœ๋กœ ๋ณ€ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์— request๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค
		
		String name = req.getParameter("drink");
		String qty = req.getParameter("qty");
		String[] ops = req.getParameterValues("drink-option");

		System.out.println("์ฃผ๋ฌธํ•œ ๋ฉ”๋‰ด " + name);
		System.out.println("์ถ”๊ฐ€ ์˜ต์…˜ " + Arrays.toString(ops));		
		System.out.println("์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์€ " + qty + "๊ฐœ ์ž…๋‹ˆ๋‹ค.");
	}
}

๐Ÿ“บ ์‹คํ–‰ํ™”๋ฉด >>


โœ… submit์„ ๋ˆ„๋ฅด๋ฉด >>


โ— submit์„ ๋ˆ„๋ฅด๋ฉด form์˜ action์†์„ฑ์— ์ ์€ url๋’ค์— ์–นํ˜€์„œ ์ „์†ก๋œ๋‹ค

profile
์ฝœ๋ผ๋ง˜ ์ฝ”๋”ฉ์ผ๊ธฐ

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