JSP ์ ๋ ฅ ์์ฒญ ์ฒ๋ฆฌ
jsp ์์ FORM ์์ฒญ์ ํ๊ณ ์์ฒญ ์ ๋ณด๋ฅผ ๋ฐ์์ ์ฒ๋ฆฌํ๋ ์์
์ ํด๋ณด๊ฒ ์ต๋๋ค.
1.
์
๋ ฅ ์์ฒญ ํ๋ฉด JSP
a.
<form> ํ๊ทธ์์ <input> ํ๊ทธ ์
๋ ฅ ์์ฒญ ํผ ์์ฑ
b.
<input> name ์์ฑ์ ํ๋ผ๋ฏธํฐ ์ง์
c.
type=โsubmitโ ์์ฑ์ธ <input>, <button> ํ๊ทธ๋ก ์์ฒญ ์ ์ถ
d.
<form> ํ๊ทธ์ action ์์ฑ๊ณผ method ์์ฑ์ ๋ฐ๋ผ ์์ฒญ
i.
action : ์์ฒญ URL
ii.
method : ์์ฒญ ๋ฐฉ์ (get, post)
1.
ํผ ์์ฒญ์ผ๋ก๋ GET ๋ฐฉ์๊ณผ POST ๋ฐฉ์๋ง ๊ฐ๋ฅํฉ๋๋ค.
2.
์
๋ ฅ ์์ฒญ ์ฒ๋ฆฌ JSP
a.
request.getParameter(โํ๋ผ๋ฏธํฐ๋ช
โ) ๋ฉ์๋๋ก ์
๋ ฅ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
b.
์์ฒญ์ ๋ฐ๋ฅธ ์ฒ๋ฆฌ
์์์ฝ๋
์
๋ ฅ ์์ฒญ ํ๋ฉด jsp
โข
signup.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์
๋ ฅ์์ฒญ ์ฒ๋ฆฌ</title>
</head>
<body>
<h3>ํ์๊ฐ์
</h3>
<form action="signup_pro.jsp" name="member" method="post">
<p> ์์ด๋ : <input type="text" name="id"> <input type="button" value="์์ด๋ ์ค๋ณต๊ฒ์ฌ">
<p> ๋น๋ฐ๋ฒํธ : <input type="password" name="passwd">
<p> ์ด๋ฆ : <input type="text" name="name">
<p> ์ฐ๋ฝ์ฒ : <select name="phone1">
<option value="010">010</option>
<option value="011">011</option>
<option value="016">016</option>
<option value="017">017</option>
<option value="019">019</option>
</select> - <input type="text" maxlength="4" size="4" name="phone2"> -
<input type="text" maxlength="4" size="4" name="phone3">
<p> ์ฑ๋ณ : <input type="radio" name="sex" value="๋จ์ฑ" checked>๋จ์ฑ
<input type="radio" name="sex" value="์ฌ์ฑ">์ฌ์ฑ
<p> ์ทจ๋ฏธ : ๋
์<input type="checkbox" name="hobby" value="๋
์" checked>
์ด๋<input type="checkbox" name="hobby" value="์ด๋" >
์ํ<input type="checkbox" name="hobby" value="์ํ">
<p> <textarea name="comment" cols="30" rows="3" placeholder="๊ฐ์
์ธ์ฌ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"></textarea>
<p> <input type="submit" value="๊ฐ์
ํ๊ธฐ">
<input type="reset" value="๋ค์์ฐ๊ธฐ">
</form>
</body>
</html>
HTML
๋ณต์ฌ
์
๋ ฅ ์์ฒญ ์ฒ๋ฆฌ jsp
โข
signup_pro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์
๋ ฅ ์์ฒญ ์ฒ๋ฆฌ</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
String phone1 = request.getParameter("phone1");
String phone2 = request.getParameter("phone2");
String phone3 = request.getParameter("phone3");
String sex = request.getParameter("sex");
// request.getParameterValues()
// : 2 ๊ฐ์ด์์ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋ฐฐ์ด๋ก ๋ฐํํ๋ ๋ฉ์๋
String[] hobby = request.getParameterValues("hobby");
String comment = request.getParameter("comment");
%>
<p> ์์ด๋ : <%=id%>
<p> ๋น๋ฐ๋ฒํธ : <%=name%>
<p> ์ด๋ฆ : <%=passwd%>
<p> ์ฐ๋ฝ์ฒ : <%=phone1%>-<%=phone2%>-<%=phone3%>
<p> ์ฑ๋ณ : <%=sex%>
<p> ์ทจ๋ฏธ :
<%
if (hobby != null) {
for (int i = 0; i < hobby.length; i++) {
out.println(" " + hobby[i]);
}
}
%>
<p> ๊ฐ์
์ธ์ฌ : <%=comment%>
</body>
</html>
HTML
๋ณต์ฌ
โข
signup_pro2.jsp
์
๋ ฅ ์์ฒญ ์ฑ๊ณต ์คํจ ์ฌ๋ถ์ ๋ฐ๋ผ์ ํ์ด์ง ๋ฆฌ๋๋ ํธ
<%@page import="java.util.Random"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Arrays"%>
<%@ page import="dto.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์
๋ ฅ ์์ฒญ ์ฒ๋ฆฌ</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
String phone1 = request.getParameter("phone1");
String phone2 = request.getParameter("phone2");
String phone3 = request.getParameter("phone3");
String sex = request.getParameter("sex");
// request.getParameterValues()
// : 2 ๊ฐ์ด์์ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋ฐฐ์ด๋ก ๋ฐํํ๋ ๋ฉ์๋
String[] hobby = request.getParameterValues("hobby");
String comment = request.getParameter("comment");
// ์์ฒญ ์ ๋ณด๋ฅผ ๊ฐ์ฒด๋ก ๋งคํ
User user = new User();
user.setId(id);
user.setPasswd(passwd);
user.setName(name);
user.setPhone(phone1+phone2+phone3);
List<String> hobbyList = Arrays.asList(hobby);
user.setHobby(hobbyList);
user.setComment(comment);
// ์๋น์ค๋ก ๋ฐ์ดํฐ ๋ฑ๋ก ์์ฒญ (ํ์๊ฐ์
์ฒ๋ฆฌ)
// boardService.insert(user);
Random random = new Random();
int result = random.nextInt(2);
String root = request.getContextPath();
// ํ์๊ฐ์
์ฑ๊ณต
if( result > 0 ) {
// 1. ๋ฉ์ธ ํ๋ฉด์ผ๋ก ์ด๋
// response.sendRedirect(root + "/");
// 2. ๋ก๊ทธ์ธ ํ๋ฉด์ผ๋ก ์ด๋
response.sendRedirect(root + "/login.jsp");
}
// ํ์๊ฐ์
์คํจ
else {
// - ํ์๊ฐ์
ํ๋ฉด์ผ๋ก ์ด๋
// : /signup2?error=invalid
response.sendRedirect(root + "/signup2.jsp?error=invalid");
}
%>
</body>
</html>
HTML
๋ณต์ฌ