[Sevlet] Exercise

Joy๐ŸŒฑยท2023๋…„ 2์›” 19์ผ
0

๐Ÿค– Exercise

๋ชฉ๋ก ๋ณด๊ธฐ
5/6
post-thumbnail

๐Ÿงฉ GET, POST ๋ฐฉ์‹ ํ™œ์šฉ

๐Ÿ’โ€ get, post ๋ฐฉ์‹์„ ๊ฐ๊ฐ ์š”์ฒญํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”์ถœํ•œ ๋’ค, ์ถ”์ถœํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํฌํ•จํ•œ ์‘๋‹ตํ™”๋ฉด์„ client์—๊ฒŒ ๋ณด์—ฌ์ฃผ์„ธ์š”.

1) index.html

<!-- 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>

2) GetPractice

@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();
	}
}

3) PostPractice

@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();
	}
}

profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€