๐ปJSP ๋ด์ฅ ๊ฐ์ฒด, JSP Implicit Object
- ๊ฐ๋ฐ์๊ฐ ์ง์ ์์ฑํ์ง ์์๋ JSP(ํฐ์บฃ)์ด ๋ฏธ๋ฆฌ ๋ง๋ค์ด์ ์ ๊ณตํ๋ ๊ฐ์ฒด์ด๋ค.
- ์์ฝ์ด๋ก ์ ๊ณตํ๋ค.
- request, response, session, pageContext, out, application, config, page, exception ๋ฑ์ด ์๋ค.
- request, pageContext, session, application ์ ๊ฐ์ฒด ๋ด๋ถ์ ์ฌ์ฉ์(๊ฐ๋ฐ์) ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ ์๋ ์ปฌ๋ ์
์ ๊ฐ์ง๊ณ ์๋ค.
- Map
- void setAttribute(String key, Object value)
- Object getAttribute(String key)
๐ปrequest ๊ฐ์ฒด
- HttpServletRequest ๋ฅผ ์๋ฏธํ๋ค.
- ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
- ์ ๋ฌ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ์ถํ๊ฑฐ๋ ์์คํ ์ ๋ณด๋ฅผ ์ถ์ถํ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
- ํด๋ผ์ด์ธํธ์์ ์๋ฒ ๋ฐฉํฅ์ผ๋ก ํ๋์ด ์ด๋ค์ง๋ฉด request๊ฐ ๋ด๋นํ๋ค.
- ํ๋์ ํฌ๊ฒ 3๊ฐ์ง๋ก ๋ณผ ์ ์๋ค.
- 1. ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์ ์กํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ
- 2. ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์ ์กํ ๋ฐ์ดํฐ ์ธ์ฝ๋ฉ ์ฒ๋ฆฌ
- 3. ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์ ์กํ ๋์ ์ฌ๋ฌ๊ฐ์ง ๊ด๋ จ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
์์ฑ
- setCharacterEncoding(charset) : ํด๋ผ์ด์ธํธ๋ก๋ถํฐ POST ๋ฐฉ์์ผ๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ๋ฅผ ์ธ์ฝ๋ฉํ ๋ฌธ์ ์งํฉ์ ์ง์ ํ๋ค.
- getParameterValues(String name) : ํ๋์ ํ๋ผ๋ฏธํฐ๊ฐ ํ๋ ์ด์์ ์๋ก ๋ค๋ฅธ ๊ฐ์ ๊ฐ์ง๋ ๊ฒฝ์ฐ, ๋ณ์๋ก ์ฒ๋ฆฌํ๊ธฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด๋ก ์ฒ๋ฆฌํด์ผํ๋ค.
- getParameterValues : ํ๋์ ํ๋ผ๋ฏธํฐ๊ฐ ๊ฐ์ง๋ ์ฌ๋ฌ ๊ฐ์ ๊ฐ์ ์ถ์ถํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํํ๋ค.
๐ป์ค์ตํด๋ณด๊ธฐ
ex07.jsp : ์ฌ์ฉ์๋ก๋ถํฐ ๋ฐ์ดํฐ ์ ๋ ฅ ๋ฐ ์๋ฒ๋ก ์ ์กํ๋ ์ญํ
ex07_ok.jsp : ๋ฐ์ดํฐ ์์ ๊ณผ ๊ฐ๊ณต ๋ฐ ์ฒ๋ฆฌ, ํผ๋๋ฐฑํ๋ ์ญํ
ex08.jsp : ๋ชจ๋ ์ ๋ ฅ ์ปจํธ๋กค์ ์ก์ ์ ํ ์คํธ
ex08_ok.jsp : ๋ชจ๋ ์ ๋ ฅ ์ปจํธ๋กค์ ์์ ์ ํ ์คํธ
์ ์ก ๋ฐฉ๋ฒ์ ๋ฐ๋ฅธ ๋ฐ์ดํฐ ์ธ์ฝ๋ฉ
1.POST ๋ฐฉ์
- ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ ํ์ค์ ์ธ ๋ฐฉ๋ฒ์ด๋ค.
- ํจํท ๋ณธ๋ฌธ(body)์์ ๋ฃ์ด์ ์ ์กํ๋ค.
- ์๋ฐ๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ธฐ์ ์๋ฐ๋ฅผ ํตํด ์ธ์ฝ๋ฉ์ด ์งํํ๋ค.
- ๋ธ๋ผ์ฐ์ (UTF-8)๊ฐ ์ธํฐ๋ท์ ํตํด ๋ฐ์ดํฐ ์ ์ก๋ ๋ ํฐ์บฃ(UTF-8)์ ๊ฑฐ์ณ ์๋ฐJSP(UTF-8)๋ก ์ ์ก๋๋ค.
- ์ธํฐ๋ท์ด ISO-8859-1๋ก ํ๊ฒฝ์ด ๋ง์ถฐ์ ธ์์ด ์ ์ก๋ ๋ ํ๊ธ์ด ๊นจ์ง๋ ๊ฒ์ด๋ค.
- POST ๋ฐฉ์์ผ๋ก ๋์ด ์จ ๋ฐ์ดํฐ๋ ๋ฌด์กฐ๊ฑด ์ธ์ฝ๋ฉ์ ํด์ผํ๋ค.
2.GET ๋ฐฉ์
- ๋ฐ์ดํฐ๊ฐ URL ๋ค์ ๋ถ์ด์ ์ ์ก๋๋ค.
- ๋๊ฒจ์ง๋ ๋ฐ์ดํฐ์ ์ธ์ฝ๋ฉ์ URL ๊ท์น์ ๋ฐ๋ผ ์ธ์ฝ๋ฉ๋๋ค.
- ํฐ์บฃ์ (UTF-8) ํ๊ฒฝ์ผ๋ก GET๋ฐฉ์์ ์ธ์ฝ๋ฉ ๋จ๊ณ๊ฐ ํ์์๋ค.
ex07.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="http://pinnpublic.dothome.co.kr/cdn/example-min.css">
</head>
<body>
<h1>๋ฐ์ดํฐ ์ ์กํ๊ธฐ</h1>
<h2>ํผํ๊ทธ + POST ๋ฐฉ์์ผ๋ก ๋ณด๋ด๊ธฐ</h2>
<form method="POST" action="ex07_ok.jsp">
<div>
๋ฌธ์: <input type="text" name="txt">
</div>
<div>
์ซ์: <input type="text" name="num">
</div>
<div>
<input type="submit" value="๋ณด๋ด๊ธฐ">
</div>
</form>
<h2>ํผํ๊ทธ + GET ๋ฐฉ์์ผ๋ก ๋ณด๋ด๊ธฐ</h2>
<form method="GET" action="ex07_ok.jsp">
<div>
๋ฌธ์: <input type="text" name="txt">
</div>
<div>
์ซ์: <input type="text" name="num">
</div>
<div>
<input type="submit" value="๋ณด๋ด๊ธฐ">
</div>
</form>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="http://pinnpublic.dothome.co.kr/cdn/example-min.js"></script>
</body>
</html>
ex07_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//POST ๋ฐฉ์ ์์ ๋ฐ์ดํฐ ์ธ์ฝ๋ฉ
request.setCharacterEncoding("UTF-8");
String txt = request.getParameter("txt");
String num = request.getParameter("num");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="http://pinnpublic.dothome.co.kr/cdn/example-min.css">
</head>
<body>
<h1>๊ฒฐ๊ณผ</h1>
<div>๋ฌธ์: <%=txt %></div>
<div>์ซ์: <%=num %></div>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="http://pinnpublic.dothome.co.kr/cdn/example-min.js"></script>
</body>
</html>
์ ๋ ฅ ์ปจํธ๋กค์ ์ก์์
1.ํ ์คํธ ๋ฐ์ค
- ์ ๋ ฅ๊ฐ์ด ์์ผ๋ฉด ๋น๋ฌธ์์ด์ ๋ฐํํ๋ค.(null ์๋)
- ์ปจํธ๋กคx(key ์ค๋ฅ) null ๋ฐํ
ex08.jsp
String txt1 = request.getParameter("txt1");
System.out.println(txt1==null); //false
System.out.println(txt1.equals("")); //true
ex08_ok.jsp
String txt1 = request.getParameter("txt1");
2. ์ํธ๋ฐ์ค
ex08.jsp
<input type="password" name="txt2">
ex08_ok.jsp
String txt2 = request.getParameter("txt2");
3. ๋ค์ค ํ ์คํธ ๋ฐ์ค
ex08.jsp
<textarea name="txt3"></textarea>
ex08_ok.jsp
String txt3 = request.getParameter("txt3");
์ํฐ์ธ \r\n์ html์์ ์ธ์ํ์ง ๋ชปํด ํ์ค๋ก ์ถ๋ ฅ๋๋ค.
txt3 = txt3.replace("\r\n", "<br>");
replace๋ก \r\n๋ฅผ <br>ํ๊ทธ๋ก ๋ฐ๊ฟ์ค๋ค.
4. ์ฒดํฌ ๋ฐ์ค
- ์ฒดํฌ๊ฐ ๋์ด์์ผ๋ฉด on / ์๋์ด ์์ผ๋ฉด null ๋ฐํํ๋ค.
- ๋ง์ฝ checkbox์ value๋ฅผ ์ง์ ํ๋ค๋ฉด, value๊ฐ์ด ๋์ด๊ฐ๋ค. ๊ด๋ฆฌ ์ฐจ์์์๋ value๋ฅผ ์ง์ ํ์ฌ ๊ด๋ฆฌํ๋ ๊ฒ์ด ์ข๋ค.
ex08.jsp
<input type="checkbox" name="cb1">
ex08_ok.jsp
String cb1 = request.getParameter("cb1");
5. ์ฌ๋ฌ๊ฐ์ ์ฒดํฌ๋ฐ์ค ๊ฐ ๋๊ธฐ๊ธฐ
- name์ ํต์ผํ๋ฉด ๋ฐฐ์ด๋ก ์ ์ก๋๋ค.
- getParameterValues์ ํ๋์ ํ๋ผ๋ฏธํฐ๊ฐ ํ๋ ์ด์์ ์๋ก ๋ค๋ฅธ ๊ฐ์ ๊ฐ์ง๋ ๊ฒฝ์ฐ, ๋ณ์๋ก ์ฒ๋ฆฌํ๊ธฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด๋ก ์ฒ๋ฆฌํ ์ ์๊ฒ ํ๋ค.
ex08.jsp
<h3>์ทจ๋ฏธ๋ฅผ ์ ํํ์ธ์</h3>
<label><input type="checkbox" name="cd5" value="๋
์">๋
์</label>
<label><input type="checkbox" name="cd5" value="์ด๋">์ด๋</label>
<label><input type="checkbox" name="cd5" value="์ฝ๋ฉ">์ฝ๋ฉ</label>
ex08_ok.jsp
String[] cd5 = request.getParameterValues("cd5");
6. ๋ผ๋์ค๋ฒํผ
- ์ค๋ณต ์ ํ์ ํ ์ ์์ด ๋จ์ผ๊ฐ์ผ๋ก ์ ์ก๋๋ค.
- checked ๋ฉ์๋๋ก ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํด์ฃผ๋ฉด null ๊ฐ ์ ํจ์ฑ๊ฒ์ฌ๋ฅผ ์๋ตํ ์ ์๋ค.
ex08.jsp
<h3>์ฑ๋ณ์ ์ ํํ์ธ์</h3>
<label><input type="radio" name="rb" value="male" >๋จ์</label>
<label><input type="radio" name="rb" value="female" checked>์ฌ์</label>
ex08_ok.jsp
String rb = request.getParameter("rb");
7. ์ ๋ ํธ ๋ฐ์ค
์ ํํ ๊ฐ์ value ๊ฐ์ด ์ ์ก๋๋ค.
ex08.jsp
<select name="sel1">
<option value="1">์ฌ๊ณผ</option>
<option value="2">๋ฐ๋๋</option>
<option value="3">ํฌ๋</option>
</select>
ex08_ok.jsp
String sel1 = request.getParameter("sel1");
8. ๋ค์ค ์ ๋ ํธ ๋ฐ์ค
ex08.jsp
<select name="sel2" multiple>
<option value="1">์ฌ๊ณผ</option>
<option value="2">๋ฐ๋๋</option>
<option value="3">ํฌ๋</option>
</select>
ex08_ok.jsp
String[] sel2 = request.getParameterValues("sel2");
9. ํ๋ ํ๊ทธ
- ๋์ ๋ณด์ด์ง์๋ ํ ์คํธ๋ฐ์ค์ด๋ค.
- ์ฌ์ฉ์๊ฐ ์ ๋ ฅ์ ๋ชปํ๊ธฐ ๋๋ฌธ์ value๋ฅผ ์ง์ ํ๋ค.
- ๋์๋ฐฉ๋ฒ์ ์ผ๋ฐ ํ ์คํธ๋ฐ์ค์ ๋์ผํ๋ค.
- ์ ์กํ ๋งํ ๋ฐ์ดํฐ๊ฐ ์๋ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ ๋, ๊ตณ์ด ๋ณด์ด์ง ์๋ ๋ฐ์ดํฐ์ผ ๋ ์ฌ์ฉํ๋ค.
- <form> ์์ญ์ ์์ฑํ๋ฉด ๋์์ธ ์์๊ฐ ๋๊ธฐ ๋๋ฌธ์ ๋ณดํต ์์ญ ๋ฐ์์ ์์ฑํ๋ค.
ex08.jsp
<input type="hidden" name="txt4" value="hong">
ex08_ok.jsp
String txt4 = request.getParameter("txt4");
10. ๋ ์ง
์ ํํ ๋ ์ง๊ฐ 'YYYY-MM-DD' ํ์์ผ๋ก ์ ์ก๋๋ค.
ex08.jsp
<input type="date" name="regdate">
ex08_ok.jsp
String regdate = request.getParameter("regdate");
11. ๋ฒ์
์ ํํ ์์น์ ์ซ์๊ฐ์ด ์ ์ก๋๋ค.
ex08.jsp
<input type="range" name="min" min="10" max="100" step="5">
ex08_ok.jsp
String min = request.getParameter("min");
12. ์์ ์ ํ
์ ํํ ์์์ด 10์ง์๋ก ์ ์ก๋๋ค.
ex08.jsp
<input type="color" name="color" style="height: 30px">
ex08_ok.jsp
String color = request.getParameter("color");
13. URL
๋งํฌ์ QueryString์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋๊ธธ ์ ์๋ค.
์๋ณธ URL๋ค์ ? ๋ฅผ ๋ถ์ฌ key=value ๋ก ์์ฑํ๋ค.
ex08.jsp
<div><a href="ex08_ok.jsp?id=hong&pw=1234">ex08_ok ๋งํฌ์
๋๋ค.</a></div>
ex08_ok.jsp
String id = request.getParameter("id");
String pw = request.getParameter("pw");
'SERVER(Servlet, JSP)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JSP] JSP ๋ด์ฅ ๊ฐ์ฒด (request, pageContext, session, application ) (0) | 2023.10.18 |
---|---|
[JPS] request : ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์ ์กํ ๋์ ์ฌ๋ฌ๊ฐ์ง ๊ด๋ จ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ (0) | 2023.10.18 |
[JSP] JSP ๊ตฌ์ฑ์์ (JSP ์ง์์, ์คํฌ๋ฆฝํธ ์์, ์ก์ ํ๊ทธ) (0) | 2023.10.17 |
[JSP] JSP ํ๋ก์ ํธ ์์ฑ (0) | 2023.10.17 |
[Servlet] ๋ฐ์ดํฐ ์ ์ก ๋ฐ ์์ (0) | 2023.10.17 |