[JSP] ํ๋ผ๋ฏธํฐ๋ฅผ ๋๊ฒจ ๋ฐ๋ ๋ฐฉ๋ฒ - getParameter()/getParameterValues()/getParameterMap
getParameter() ๊ณผ getParameterValues๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
getParameterMap()
๋ฆฌํด ํ์ ์ด Map์ด๋ค.
ํด๋ผ์ด์ธํธ(์น๋ธ๋ผ์ฐ์ ) ์์ ์๋ฒ๋ก ํ๋ผ๋ฏธํฐ ๋๊ธฐ๊ธฐ
1. GET ๋ฐฉ์
[๋ฐฉ๋ฒ1] a ํ๊ทธ ์ด์ฉ
<a href="๋ฐ์์๋ฒ์ฃผ์?์ด๋ฆ1=๊ฐ1&์ด๋ฆ2=๊ฐ2">๋ด์ฉ</a>
[๋ฐฉ๋ฒ2] ๋ฒํผ์ผ๋ก ์ ์ก
<button type="button"
onclick="javascript:location.href='๋ฐ์์๋ฒ์ฃผ์?์ด๋ฆ1=๊ฐ1&์ด๋ฆ2=๊ฐ2';">๋ณด๋ด๊ธฐ</button>
[๋ฐฉ๋ฒ3] ์๋ฐ์คํฌ๋ฆฝํธ ์ด์ฉ
<script type="text/javascript">
function send() {
location.href='๋ฐ์์๋ฒ์ฃผ์?์ด๋ฆ1=๊ฐ1&์ด๋ฆ2=๊ฐ2';
}
</script>
<button type="button" onclick="send();">๋ณด๋ด๊ธฐ</button>
[๋ฐฉ๋ฒ4] form ํ๊ทธ ์ด์ฉ - GET์์๋ ๊ฑฐ์ ์ฌ์ฉ ์ํจ
form์ ์ผ๋ฐ์ ์ผ๋ก post ๋ฐฉ์์์ ์ฌ์ฉ
<form name="ํผ์ด๋ฆ" action="๋ฐ์์๋ฒ์ฃผ์" method="get">
<input type="text" name="์ด๋ฆ1"><br>
<input type="text" name="์ด๋ฆ2"><br>
<button>๋ณด๋ด๊ธฐ</button>
</form>
POST ๋ฐฉ์
[๋ฐฉ๋ฒ1] form ํ๊ทธ ์ด์ฉ
<button>, <button type="submit">, <input type="submit"...>, <input type="image"...>
submit ๊ธฐ๋ฅ์ด ์๋ submit ๋ฒํผ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
: submit ๋ฒํผ์ form ํ๊ทธ ์์ ์์ด์ผ ๋์ํ๋ค.
(1) ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํ์ง ์๋ ๊ฒฝ์ฐ
<form name="ํผ์ด๋ฆ" action="๋ฐ์์๋ฒ์ฃผ์" method="post">
<input type="text" name="์ด๋ฆ1"><br>
<input type="text" name="์ด๋ฆ2"><br>
<button type="submit">๋ฑ๋กํ๊ธฐ</button>
</form>
<form name="ํผ์ด๋ฆ" action="๋ฐ์์๋ฒ์ฃผ์" method="post">
<input type="text" name="์ด๋ฆ1"><br>
<input type="text" name="์ด๋ฆ2"><br>
<input type="image" src="์ด๋ฏธ์ง์ฃผ์">
</form>
(2) ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํ๋ ๊ฒฝ์ฐ
<script type="text/javascript">
function check() {
var f=document.ํผ์ด๋ฆ;
if(! f.์ด๋ฆ1.value) {
return false; // ์๋ฒ๋ก ์ ์ก ์ํจ
}
return true; // ์๋ฒ๋ก ์ ์ก
}
</script>
<form name="ํผ์ด๋ฆ" action="๋ฐ์์๋ฒ์ฃผ์" method="post" onsubmit="return check();">
<input type="text" name="์ด๋ฆ1"><br>
<input type="text" name="์ด๋ฆ2"><br>
<button type="submit">๋ณด๋ด๊ธฐ</button>
</form>
(3) <button type="submit">, <input type="submit"...>, <input type="image"...>
๋ฑ submit ๊ธฐ๋ฅ์ด ์๋ submit ๋ฒํผ์
์๋ฐ์คํฌ๋ฆฝํธ์์ f.submit(); ์ฒ๋ผ submit() ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋๋ฒ ์๋ฒ๋ก ์ ์ก๋๋ฏ๋ก ์ฃผ์ ํด์ผ ํ๋ค.
๋ฐฉ๋ฒ2] form ํ๊ทธ ์ด์ฉ
: <button type="button">, <input type="button"...> ๋ฑ
submit ๊ธฐ๋ฅ์ด ์๋ ์ผ๋ฐ ๋ฒํผ๋ฑ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
(1) ์ฌ์ฉ ์
<script type="text/javascript">
function sendOk() {
var f=document.ํผ์ด๋ฆ;
if(! f.์ด๋ฆ1.value) {
return;
}
f.submit(); // ์๋ฒ๋ก ์ ์ก
}
</script>
<form name="ํผ์ด๋ฆ" action="๋ฐ์์๋ฒ์ฃผ์" method="post">
<input type="text" name="์ด๋ฆ1"><br>
<input type="text" name="์ด๋ฆ2"><br>
<button type="button" onclick="sendOk();">๋ณด๋ด๊ธฐ</button>
</form>
(2) <button type="button">, <input type="button"...>, <select> ๋ฑ์
์๋ฒ๋ก ์ ์กํ ์ ์๋ ๊ธฐ๋ฅ์ด ์๋ ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ ์๋ฒ๋ก ์ ์กํ๊ธฐ ์ํด์๋ ๋ฐ๋์
์๋ฐ์คํฌ๋ฆฝํธ์์ f.submit(); ์ฒ๋ผ submit() ํจ์๋ฅผ ํธ์ถํด์ผ ์๋ฒ๋ก ์ ์ก๋๋ค.
---------------------------------------------------
โป ์๋ฒ์์ ํด๋ผ์ด์ธํธ(์น๋ธ๋ผ์ฐ์ ) ๊ฐ ๋๊ธด ํ๋ผ๋ฏธํฐ ๋ฐ๊ธฐ - ๋ฌด์กฐ๊ฑด String์ผ๋ก ๋์ด์จ๋ค.
๋ฐฉ๋ฒ1] ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ด ๋์ผํ๊ฒ์ด ์๋ ๊ฒฝ์ฐ
String ๋ณ์ = request.getParameter("ํ๋ผ๋ฏธํฐ์ด๋ฆ");
๋ฐฉ๋ฒ2] ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ด ๋์ผํ๊ฒ์ด ์๋ ๊ฒฝ์ฐ ์ ์๋ก ๋ฐ๊ธฐ
int ๋ณ์ = Integer.parseInt(request.getParameter("ํ๋ผ๋ฏธํฐ์ด๋ฆ"));
๋ฐฉ๋ฒ3] ํ๋ผ๋ฏธํฐ ์ด๋ฆ์ด ๋์ผํ๊ฒ์ด ํ๋ ์ด์์ธ ๊ฒฝ์ฐ
String []๋ฐฐ์ด๋ช
= request.getParameterValues("ํ๋ผ๋ฏธํฐ์ด๋ฆ");
getParameter();
get, post ๋ฐฉ์์ผ๋ก ๋์ด์จ ํ๋ผ๋ฏธํฐ ๋ฐ๊ธฐ
์กด์ฌํ์ง ์๋ ํ๋ผ๋ฏธ๋ null์ ๋ฐํ
์ ๋ ฅ ํผ์ ๋น์ด์๋ ์นธ์ผ๋ก ์๋ฒ์ ๋ณด๋๋ค๋ฉด, ๋ณด๋์ผ๋๊น "" ๋ก ๊ฐ . null์ด ์๋!
String subject = request.getParameter("subject");
๋์ผํ ์ด๋ฆ์ผ๋ก ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์กํ ๊ฒฝ์ฐ getParameter()๋ ํ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ๋๋ค.
๋์ผํ ์ด๋ฆ์ ํ๋ผ๋ฏธํฐ๊ฐ ๋ ๊ฐ ์ด์์ธ ๊ฒฝ์ฐ ๋ฐ๋์ getParameterValues()๋ก ๋ฐ๋๋ค.
checkbox์ ๊ฒฝ์ฐ, ์ ํํ ๊ฒ์ด ์์ผ๋ฉด null์ ๋ฐํํ๋ค.
getParmeterNames
getParameter() ๊ณผ getParameterValues๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๋ค.