์ธ๊ณ ๋ฉ๊ณผ ๋์ฝ๋ฉ
๋ฌธ์๋ฅผ ์ปดํจํฐ์ ์ ์ฅํ๊ฑฐ๋ ํต์ ์ ์ฌ์ฉํ ๋ชฉ์ ์ผ๋ก ๋ถํํ๋ ๊ฒ์ ์ธ์ฝ๋ฉ์ด๋ผ ํ๋ฉฐ,
๋ฐ๋๋ก ๋ฌธ์๋ฅผ ์๋๋๋ก ๋๋๋ฆฌ๋ ๊ฒ์ ๋์ฝ๋ฉ์ด๋ผ ํ๋ค.
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 |
---|