๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/JSP

(56) [JSP] 2์ผ JSPRequest parameter ๋ฐ ๋‚ด๋ถ€๊ฐ์ฒด

by ๋”ฐ๊ถˆ 2024. 5. 8.

 

 

์ธ๊ณ ๋”ฉ๊ณผ ๋””์ฝ”๋”ฉ
๋ฌธ์ž๋ฅผ ์ปดํ“จํ„ฐ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ํ†ต์‹ ์— ์‚ฌ์šฉํ•  ๋ชฉ์ ์œผ๋กœ ๋ถ€ํ™”ํ•˜๋Š” ๊ฒƒ์„ ์ธ์ฝ”๋”ฉ์ด๋ผ ํ•˜๋ฉฐ,
๋ฐ˜๋Œ€๋กœ ๋ฌธ์ž๋ฅผ ์›๋ž˜๋Œ€๋กœ ๋˜๋Œ๋ฆฌ๋Š” ๊ฒƒ์„ ๋””์ฝ”๋”ฉ์ด๋ผ ํ•œ๋‹ค. 

 

 

GET ๋ฐฉ์‹์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก

1. a. ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•œ GET ๋ฐฉ์‹ 

	<h3>ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก - GET ๋ฐฉ์‹</h3>

	<pre>
		- GET ๋ฐฉ์‹ 
			: ๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก ๋ฐฉ์‹
			: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์ ‘์†ํ• ๋•Œ ๊ธฐ๋ณธ์ ์ธ ์ ‘์† ๋ฐฉ์‹์€ GET ์ด๋‹ค.
			: ์ฃผ์†Œ์ค„์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‹ค์–ด์„œ ๋ณด๋‚ธ๋‹ค.
			<%-- ํ˜•์‹ : ์ฃผ์†Œ?์ด๋ฆ„1=๊ฐ’1&์ด๋ฆ„2=๊ฐ’2 --%>
			: Query String - GET ๋ฐฉ์‹์œผ๋กœ ์ „์†ก ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ
	</pre>
	<hr>

	<%-- a ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ GET ๋ฐฉ์‹์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก --%>
	<%-- ํ•œ๊ธ€๋“ฑ์„ ์ธํ† ๋”ฉํ•˜์ง€ ์•Š๊ณ  GET ๋ฐฉ์‹์œผ๋กœ ์ „์†กํ•˜๋ฉด ์ „์†ก ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. --%>

	<p>
		<a href="ex01_ok.jsp?name=์„œ์ง„ํƒœ&age=25">ํ™•์ธ</a>
	</p>

 

 

2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉ(location.href)

	<%--์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ GET ๋ฐฉ์‹์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก --%>
	<p>
		<button type="button" onclick="sendOk();">ํ™•์ธ</button>
	</p>
	
	<script type="text/javascript">
	function sendOk() {
		let name = '์ž%๋ฐ”';  // '์ž'๋งŒ ๋‚˜์˜ด
		name = encodeURIComponent(name); // ์ฃผ์†Œ ํ˜•์‹์œผ๋กœ ์ธ์ฝ”๋”ฉ 
		let age = 20;
		
		let url = 'ex01_ok.jsp?name='+name+'&age='+age;
		location.href=url;
	}
	</script>

 

 

 

3. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉ(form ํƒœ๊ทธ)

	<%--์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ GET ๋ฐฉ์‹์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก --%>
	<p>
		<button type="button" onclick="sendOk();">ํ™•์ธ</button>
	</p>
	<hr>
	
	<%--form ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•œ GET ๋ฐฉ์‹์œผ๋กœ ์ „์†ก --%>
	<%--form ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ GET ๋ฐฉ์‹์œผ๋กœ ์ „์†กํ•˜์ง€ ์•Š๋Š”๋‹ค. --%>
	<%--form ํƒœ๊ทธ์—์„œ method ์†์„ฑ์„ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด GET ๋ฐฉ์‹์ด๋‹ค. --%>
	
	<form action="ex01_ok.jsp">
			<p>์ด๋ฆ„ : <input type="text" name="name"></p>
			<p>๋‚˜์ด : <input type="text" name="age"></p>
			<p>
				<button type="submit">๋ณด๋‚ด๊ธฐ</button>
			</p>
			
	</form>

 

ํ˜•์‹ <form name="ํผ์ด๋ฆ„" action="์„œ๋ฒ„์ฃผ์†Œ" method="์ „์†ก๋ฐฉ์‹">~</form>

 

 

 

 

 

 

encodeUROComponent ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ธ์ฝ”๋”ฉ
encodeURI ์ธํ„ฐ๋„ท ์ฃผ์†Œ์— ์‚ฌ์šฉ๋˜๋Š” ์ผ๋ถ€ ํŠน์ˆ˜๋ฌธ์ž ๋ฐ ์˜์ˆ˜์ž๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ธ์ฝ”๋”ฉ
decodeURIComponent ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋””์ฝ”๋”ฉ
decodeURI ์ธํ„ฐ๋„ท ์ฃผ์†Œ์— ์‚ฌ์šฉ๋˜๋Š” ์ผ๋ถ€ ํŠน์ˆ˜๋ฌธ์ž ๋ฐ ์˜์ˆ˜์ž๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋””์ฝ”๋”ฉ

 

 

 

POST ๋ฐฉ์‹์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก

์ผ๋ฐ˜์ ์œผ๋กœ POST ๋ฐฉ์‹์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” form ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ „์†กํ•œ๋‹ค. form ํƒœ๊ทธ์˜ method ์†์„ฑ์„ method="post"๋กœ ์ง€์ •ํ•˜๋ฉด POST ๋ฐฉ์‹์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „์†กํ•œ๋‹ค. 

 

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

	<h3>ํŒŒ๋ผ๋ฏธํ„ฐ ์ „์†ก ๋ฐฉ์‹: POST</h3>
	<pre>
		- POST ๋ฐฉ์‹์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „์†กํ•˜๋ฉด body์— ์ •๋ณด๋ฅผ ์‹ค์–ด์„œ ๋ณด๋ƒ„
		- POST ๋ฐฉ์‹์€ ์šฉ๋Ÿ‰์— ์ œํ•œ์ด ์—†์Œ
		- POST ๋ฐฉ์‹์€ form ํƒœ๊ทธ์˜ method ="post" ์†์„ฑ์œผ๋กœ ์„ค์ •
		- form ํƒœ๊ทธ์—์„œ method ="post" ๋ฐฉ์‹์˜ enctype์€ ๊ธฐ๋ณธ์ ์œผ๋กœ 
		  enctype="allication/x-www-form.urlencoded"์ด๋‹ค.
		- application/x-www-form-urlencoded๋Š” 
		  ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฃผ์†Œ ํ˜•์‹์œผ๋กœ ์ธ์ฝ”๋”ฉํ•œ๋‹ค.
		- enctype์€ method="post"์—์„œ๋งŒ ์œ ํšจํ•˜๋‹ค.
	</pre>
	<hr>

	<form action="ex02_ok.jsp" method="post">
	<p>
		์ด๋ฆ„ : <input type="text" name="name">
	</p>
	<p>
		๋‚˜์ด : <input type="text" name="age">
	</p>
	<p>
		<button type="submit">์ „์†กํ•˜๊ธฐ</button>
	</p>
	</form>


</body>
</html>

 

 

form / submit 

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

	<h3> form ๋ฐ์ดํ„ฐ ์„œ๋ฒ„๋กœ ์ „์†ก:submit ๋ฒ„ํŠผ</h3>
	<%--
		- form ํƒœ๊ทธ ์•ˆ์— submit ๋ฒ„ํŠผ์ด ์žˆ์–ด์•ผ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.
		- submit ๋ฒ„ํŠผ์˜ ์ข…๋ฅ˜ 
		  1) <button> ํ™•์ธ </button>
		  2) <button type="submit"> ํ™•์ธ </button>
		  3) <input type="image" src="์ด๋ฏธ์ง€">
		  4) <input type="submit" value="ํ™•์ธ">
		  
		- input ํƒœ๊ทธ์˜ required, pattern ์†์„ฑ 
		  input ํƒœ๊ทธ๊ฐ€ form ํƒœ๊ทธ์•ˆ์— ์žˆ์–ด์•ผ ํ•˜๊ณ  submit ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ ๋ฐ˜์‘
		  
	    - <button type="reset">์ดˆ๊ธฐํ™”</button>
	      reset ๋ฒ„ํŠผ์€ form ํƒœ๊ทธ ์•ˆ์— ์žˆ์–ด์•ผ ๋ฐ˜์‘ํ•œ๋‹ค.  
	 --%>
	<hr>

	<form action="ex03_ok.jsp" method="post">
	<p>
		์ด๋ฆ„ : <input type="text" name="name" required>
	</p>
	<p>
		๋‚˜์ด : <input type="text" name="age" pattern="\d+">
	</p>
	<p>
		<button type="submit">์ „์†กํ•˜๊ธฐ</button>
		<button>์ „์†ก</button>
		<input type="submit" value="์ „์†ก">
		<input type="image" src="btn_confirm.png">
		<button type="button">์ผ๋ฐ˜๋ฒ„ํŠผ-์ „์†ก๊ธฐ๋Šฅ์—†์Œ</button>
		<button type="reset">์ดˆ๊ธฐํ™”</button>
		
	</p>
	</form>


</body>
</html>

 

 

 

form / submit ์œ ํšจ์„ฑ๊ฒ€์‚ฌ

 

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

	<h3> form ๋ฐ์ดํ„ฐ ์„œ๋ฒ„๋กœ ์ „์†ก:submit ๋ฒ„ํŠผ - ์œ ํšจ์„ฑ๊ฒ€์‚ฌ</h3>
	
	<%--
		- form ํƒœ๊ทธ ์•ˆ์˜ submit ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด submit ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ
		- submit ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋Š” form ํƒœ๊ทธ์˜ submit
		  ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ ํ•œ๋‹ค.
		- submit ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์„œ๋ฒ„๋กœ ์ „์†ก๋˜์ง€ ์•Š๋Š”๋‹ค.  
	 --%>

	<form name="myForm" action="ex04_ok.jsp" method="post">
	<p>
		์ด๋ฆ„ : <input type="text" name="name">
	</p>
	<p>
		๋‚˜์ด : <input type="text" name="age">
	</p>
	<p>
		<button type="submit">์ „์†กํ•˜๊ธฐ</button>
	</p>
	</form>
	
	<script type="text/javascript">
	const check = () => {
		const f = document.myForm;
		
		if(! /^[๊ฐ€-ํžฃ]{2,5}$/.test(f.name.value)){
			f.name.focus();
			return false;
		}
		
		if(! /^\d{1,3}$/.test(f.age.vlaue)){
			f.age.focus();
			return false;
		}
		
		// f.submit(); // ์ด๊ณณ์—์„œ submit()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์„œ๋ฒ„๋กœ ๋‘๋ฒˆ ์ „์†ก๋œ๋‹ค. 
		return true;
	};
	
	
	// 
	window.addEventListener('load',()=>{
		document.myForm.addEventListener('sumbit',e=> {
			if(! check()){
				e.preventDefault();
			}
		});
	});
	
	</script>


</body>
</html>

 

 

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

	<h3> form ๋ฐ์ดํ„ฐ ์„œ๋ฒ„๋กœ ์ „์†ก:submit ๋ฒ„ํŠผ - ์œ ํšจ์„ฑ๊ฒ€์‚ฌ</h3>
	
	<%--
		- form ํƒœ๊ทธ ์•ˆ์˜ submit ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด submit ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ
		- submit ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋Š” form ํƒœ๊ทธ์˜ submit
		  ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ ํ•œ๋‹ค.
		- submit ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์„œ๋ฒ„๋กœ ์ „์†ก๋˜์ง€ ์•Š๋Š”๋‹ค.  
	 --%>
	
	<form name="myForm" action="ex04_ok.jsp" method="post" onsubmit="return check();">
	<!-- form ํƒœ๊ทธ์•ˆ์— ์žˆ์–ด์•ผ submit ์ด๋ฒคํŠธ ๋ฐœ์ƒ -->
	<!-- submit ๋ฒ„ํŠผ์€ onclick ์ด๋ฒคํŠธ x -->
	
	<p>
		์ด๋ฆ„ : <input type="text" name="name">
	</p>
	<p>
		๋‚˜์ด : <input type="text" name="age">
	</p>
	<p>
		<button type="submit">์ „์†กํ•˜๊ธฐ</button>
	</p>
	</form>
	
	
	<script type="text/javascript">
	const check = () => {
		const f = document.myForm;
		
		if(! /^[๊ฐ€-ํžฃ]{2,5}$/.test(f.name.value)){
			alert('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”');
			f.name.focus();
			return false;
		}
		
		if(! /^\d{1,3}$/.test(f.age.vlaue)){
			f.age.focus();
			return false;
		}
		
		// f.submit(); // ์ด๊ณณ์—์„œ submit()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์„œ๋ฒ„๋กœ ๋‘๋ฒˆ ์ „์†ก๋œ๋‹ค. 
		return true;
	};
	

	</script>


</body>
</html>

 

 

form/์ผ๋ฐ˜ ๋ฒ„ํŠผ - ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

	<h3> form ๋ฐ์ดํ„ฐ ์„œ๋ฒ„๋กœ ์ „์†ก : ์ผ๋ฐ˜ ๋ฒ„ํŠผ - ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ</h3>
	
	<%--
		- form ํƒœ๊ทธ์˜ ๋‚ด์šฉ์„ ์ผ๋ฐ˜๋ฒ„ํŠผ, a ํƒœ๊ทธ๋“ฑ์„ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 
		  form ํƒœ๊ทธ์˜ submit() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 
		  
	 --%>
	
	<hr>

	<form name="myForm" action="ex05_ok.jsp" method="post">
	
	<p>
		์ด๋ฆ„ : <input type="text" name="name">
	</p>
	<p>
		๋‚˜์ด : <input type="text" name="age">
	</p>
	<p>
		<button type="button" onclick="sendOk();">์ „์†กํ•˜๊ธฐ</button> <!-- span,a ํƒœ๊ทธ ๋ชจ๋‘ ๊ฐ€๋Šฅ -->
		<span onclick="sendOk()">์ „์†ก</span>
	</p>
	</form>
	
	
	<script type="text/javascript">
	const sendOk = () => {
		const f = document.myForm;
		
		if(! /^[๊ฐ€-ํžฃ]{2,5}$/.test(f.name.value)){
			alert('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”');
			f.name.focus();
			return;
		}
		
		if(! /^\d{1,3}$/.test(f.age.vlaue)){
			f.age.focus();
			return;
		}
		
		f.submit(); // ์ด๊ณณ์—์„œ submit()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์„œ๋ฒ„๋กœ ๋‘๋ฒˆ ์ „์†ก๋œ๋‹ค. 
		
	};

	</script>

</body>
</html>

 

'๐Ÿ’ป > JSP' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

(57) [JSP] 3์ผ JSP ๋‚ด๋ถ€ ๊ฐ์ฒด, ์•ก์…˜ ํƒœ๊ทธ  (0) 2024.05.09