๐โ get, post ๋ฐฉ์์ ๊ฐ๊ฐ ์์ฒญํ๊ณ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ์ถํ ๋ค, ์ถ์ถํ ํ๋ผ๋ฏธํฐ๋ฅผ ํฌํจํ ์๋ตํ๋ฉด์ client์๊ฒ ๋ณด์ฌ์ฃผ์ธ์.
<!-- get ๋ฐฉ์์ผ๋ก ์์ฒญ -->
<h3>[ GET ๋ฐฉ์ ]--------------------------------</h3>
<form action="getpractice" method="get">
<h4>๐ช ์ฟ ํค๋ฐ ํน๋ค ๐ช</h4>
<label>โถ ์์ด๋ : </label><input type="text" name="id"> <br>
<label>โถ ๋น๋ฐ๋ฒํธ : </label><input type="text" name="pwd"> <br>
<label>โถ ์๋
์์ผ : </label><input type="date" name="dob"> <br>
<label>โถ ์ฟ ํค ์ ํ : </label>
<input type="radio" name="cookie" id="choco" value="์ด์ฝ๋ง์ฟ ํค"><label for="choco">์ด์ฝ๋ง์ฟ ํค</label>
<input type="radio" name="cookie" id="mint" value="๋ฏผํธ๋ง์ฟ ํค"><label for="mint">๋ฏผํธ๋ง์ฟ ํค</label>
<input type="radio" name="cookie" id="apple" value="์ฌ๊ณผ๋ง์ฟ ํค"><label for="apple">์ฌ๊ณผ๋ง์ฟ ํค</label>
<input type="radio" name="cookie" id="oreo" value="์ค๋ ์ค๋ง์ฟ ํค"><label for="oreo">์ค๋ ์ค๋ง์ฟ ํค</label>
<br>
<label>โถ ์์ดํ
์ ํ : </label>
<input type="checkbox" name="items" id="cap" value="Cap"><label for="cap">๋ชจ์</label>
<input type="checkbox" name="items" id="wing" value="Wing"><label for="wing">๋ ๊ฐ</label>
<input type="checkbox" name="items" id="glove" value="Glove"><label for="glove">์ฅ๊ฐ</label>
<br><br>
<input type="submit">
</form>
<br><br>
<!-- post ๋ฐฉ์์ผ๋ก ์์ฒญ -->
<h3>[ POST ๋ฐฉ์ ]--------------------------------</h3>
<form action="postpractice" method="post">
<h4>๐ถ ์ถ์์ ๊ณ ๐ถ</h4>
<label>โถ ์ด๋ฆ : </label><input type="text" name="name"> <br>
<label>โถ ์๋
์์ผ : </label><input type="date" name="dob"> <br>
<label>โถ ์ถ์ ์ง์ญ : </label>
<select name="area">
<option value="์์ธ">์์ธ</option>
<option value="๊ฒฝ๊ธฐ">๊ฒฝ๊ธฐ</option>
<option value="์ธ์ฒ">์ธ์ฒ</option>
<option value="๊ฐ์">๊ฐ์</option>
<option value="์ถฉ์ฒญ">์ถฉ์ฒญ</option>
<option value="์ ๋ผ">์ ๋ผ</option>
<option value="๊ฒฝ์">๊ฒฝ์</option>
<option value="์ ์ฃผ">์ ์ฃผ</option>
</select>
<br><br>
<input type="submit">
</form>
@WebServlet("/getpractice")
public class GetPractice extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* ================= ์์ฒญ ๋ฐ๊ธฐ ================= */
/* text */
String id = request.getParameter("id");
System.out.println("โถ ID : " + id);
/* text */
String pwd = request.getParameter("pwd");
System.out.println("โถ Password : " + pwd);
/* date */
java.sql.Date dob = java.sql.Date.valueOf(request.getParameter("dob"));
System.out.println("โถ Date of Birth : " + dob);
/* radio */
String cookie = request.getParameter("cookie");
System.out.println("โถ Cookie : " + cookie);
/* checkbox */
String[] items = request.getParameterValues("items");
int i = 0;
for(String item : items) {
i++;
System.out.println("โถ Item[" + i + "] : " + item);
}
/* ================= ์๋ต ํ๊ธฐ ================= */
/* ์๋ต ์ปจํ
์ธ ํ์
์ค์ ๋ฐ ์ธ์ฝ๋ฉ ์ง์ */
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
/* ๋ฌธ์์ด์ ์ด์ฉํด์ ์ฌ์ฉ์์๊ฒ ๋ด๋ณด๋ด๊ธฐ ํ ํ์ด์ง๋ฅผ ์์ฑ */
StringBuilder responseBuilder = new StringBuilder();
responseBuilder.append("<doctype html>\n")
.append("<html>\n")
.append("<head>\n")
.append("</head>\n")
.append("<body>\n")
.append("<h3> " + id + "๋ ํ์ํด์ !! ๐ช</h3>\n")
.append("<h3> ๋น๋ฐ๋ฒํธ๋ " + pwd + "์ด๋ฉฐ, ์์ผ์๋ฉด ์๋ผ์!</h3>\n\n")
.append("<h3>" + id + "๋์ ์ฟ ํค๐ช๋ " + dob + "์ ํ์ด๋ '"+ cookie + "' ์
๋๋ค !</h3>\n")
.append("</body>\n")
.append("</html>");
/* ์คํธ๋ฆผ์ ์ด์ฉํด ๋ด๋ณด๋ด๊ธฐ */
out.print(responseBuilder.toString());
/* ๋ฒํผ์ ์๋ฅํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ๋ก ๋ด๋ณด๋ด๊ธฐ */
out.flush();
/* ์คํธ๋ฆผ ๋ซ๊ธฐ */
out.close();
}
}
@WebServlet("/postpractice")
public class PostPractice extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* ================= ์์ฒญ ๋ฐ๊ธฐ ================= */
/* ํ๋ผ๋ฏธํฐ ๊ฐ์ ๊บผ๋ด๊ธฐ ์ ์ ๋์ฝ๋ฉํ ์ธ์ฝ๋ฉ ๋ฐฉ์์ ์ง์ ํด์ฃผ๋ฉด ํด๋น ๋ฐฉ์์ผ๋ก body ๊ฐ์ ํด์ */
request.setCharacterEncoding("UTF-8"); // ํ๊ธ ํด์ ๊ฐ๋ฅํด์ง
/* text */
String name = request.getParameter("name"); // parameter๋ฅผ ๊บผ๋ด์ค๊ธฐ ์ , ์ธ์ฝ๋ฉ ๋ฐฉ์ ์ค์
System.out.println("โถ ์ด๋ฆ : " + name);
/* date */
java.sql.Date dob = java.sql.Date.valueOf(request.getParameter("dob"));
System.out.println("โถ ์๋
์์ผ : " + dob);
/* select & option */
String area = request.getParameter("area");
System.out.println("โถ ์ถ์์ง์ญ : " + area);
/* ================= ์๋ต ํ๊ธฐ ================= */
/* ์๋ต ์ปจํ
์ธ ํ์
์ค์ ๋ฐ ์ธ์ฝ๋ฉ ์ง์ */
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
/* ๋ฌธ์์ด์ ์ด์ฉํด์ ์ฌ์ฉ์์๊ฒ ๋ด๋ณด๋ด๊ธฐ ํ ํ์ด์ง๋ฅผ ์์ฑ */
StringBuilder responseBuilder = new StringBuilder();
responseBuilder.append("<doctype html>\n")
.append("<html>\n")
.append("<head>\n")
.append("</head>\n")
.append("<body>\n")
.append("<h3> " + dob + ", " + area + "์์ ํ์ด๋,</h3>\n")
.append("<h3> " + name + "๋ ํ์์ ์ถํ๋๋ ค์!</h3>\n")
.append("<h3> ์ถ์์ ๊ณ ๋ ๋ฌด์ฌํ ์๋ฃ๋์์ต๋๋ค :)</h3>\n")
.append("</body>\n")
.append("</html>");
/* ์คํธ๋ฆผ์ ์ด์ฉํด ๋ด๋ณด๋ด๊ธฐ */
out.print(responseBuilder.toString());
/* ๋ฒํผ์ ์๋ฅํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ๋ก ๋ด๋ณด๋ด๊ธฐ */
out.flush();
/* ์คํธ๋ฆผ ๋ซ๊ธฐ */
out.close();
}
}