Search

JSP ์ž…๋ ฅ ์š”์ฒญ ์ฒ˜๋ฆฌ

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
๋ณต์‚ฌ