๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[JSP/Servlet] ๋ฐ์ดํ„ฐ ์ „์†ก GET/POST(2023.05.12) ๋ณธ๋ฌธ

full stack/Back-End

[JSP/Servlet] ๋ฐ์ดํ„ฐ ์ „์†ก GET/POST(2023.05.12)

Shinjio 2023. 5. 12. 18:23

Quiz. ์ด์ƒํ•œ ์  ์ฐพ์•„๋ณด๊ธฐ

 

 

์ฃผ์†Œ์ฐฝ์— ํŒจ์Šค์›Œ๋“œ๊ฐ€ ๋…ธ์ถœ๋จ

์›์ธ : get ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ. get ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์ด ์ฃผ์†Œ์ฐฝ์— ๋…ธ์ถœ๋จ

 

๐ŸŽˆ HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ

์›น ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €)๊ฐ€ ์›น ์„œ๋ฒ„์— ์š”์ฒญํ•˜๋Š” ์„œ๋น„์Šค ์ฒ˜๋ฆฌ ๋ฐฉ์‹ ์ง€์ •

 

 

์œ„์˜ ์š”์ฒญ ๋ฉ”์„œ๋“œ ์˜ˆ์‹œ๋Š” ์•ฝ์†์˜ ๋Š๋‚Œ์œผ๋กœ ๊ผญ ์œ„์˜ ๊ฒƒ์„ ๊ผญ ๋”ฐ๋ผ์•ผํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๊ฐœ๋ฐœ์ž๋“ค ๊ฐ„์˜ ๊ทœ์น™๊ณผ ๊ฐ™์Œ  

๊ธฐ๋Šฅ๋“ค์ด ํŠน์ • ์š”์ฒญ์— ํ•œ์ •๋˜์–ด ์žˆ์ง„ ์•Š์ง€๋งŒ ๋ฌด์—‡์„ ์š”์ฒญํ•˜๋Š”์ง€ ์•Œ๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด ๊ตฌ๋ถ„ํ•˜์—ฌ ์‚ฌ์šฉํ•จ

 

 

์ฝ์–ด๋ณด๊ธฐ!

 

REST API ์ œ๋Œ€๋กœ ์•Œ๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ : NHN Cloud Meetup

REST API ์ œ๋Œ€๋กœ ์•Œ๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ

meetup.nhncloud.com

 

๐Ÿ“– GET

 

 

โš™ URI / URL

 

 

ํŒจํ‚ท(packet)
๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ „์†กํ•˜๊ธฐ ์‰ฝ๋„๋ก ์ž๋ฅธ ๋ฐ์ดํ„ฐ์˜ ์ „์†ก ๋‹จ์œ„
ํŒจํ‚ท์€ ๋ฐ์ดํ„ฐ ์ „์†ก์—์„œ ์†ก์‹ ์ธก๊ณผ ์ˆ˜์‹ ์ธก์— ์˜ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ์ทจ๊ธ‰๋˜์–ด ์ „์†ก๋˜๋Š” ์ง‘ํ•ฉ์ฒด๋กœ, ๋ถ„ํ• ๋œ ๊ฐ๊ฐ์˜ ํŒจํ‚ท์—๋Š” ๋ณ„๋„์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์—ฌ์ง€๊ณ  ๋ชฉ์ ์ง€์˜ ์ธํ„ฐ๋„ท ์ฃผ์†Œ๊ฐ€ ๊ธฐ๋ก๋˜๋ฉฐ, ์—๋Ÿฌ ์ฒดํฌ ๋ฐ์ดํ„ฐ๋„ ํฌํ•จ๋จ

ํŒจํ‚ท์˜ ๊ตฌ์กฐ :  head + body

++ ํŒจํ‚ท์—๋Š” ์ˆœ์„œ ์ •๋ณด๋„ ๋“ค์–ด์žˆ๋‹ค. 
     >> ํŒจํ‚ท์„ ์ˆœ์„œ๋Œ€๋กœ ๋ณด๋‚ด๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํŒจํ‚ท์„ ํ•œ๋ฒˆ์— ๋„คํŠธ์›Œํฌ์ƒ์œผ๋กœ ๋ฟŒ๋ฆผ
     >> ํŒจํ‚ท๋“ค์ด ์„œ๋ฒ„๋กœ ๋„์ฐฉํ–ˆ์„ ๋•Œ ์ˆœ์„œ๊ฐ€ ๋’ค์ฃฝ๋ฐ•์ฃฝ์ด๋˜์–ด ๋„์ฐฉ๋  ์ˆ˜ ์žˆ์Œ
     >> ํŒจํ‚ท์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žฌ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ 

 

- ์ „์†กํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ธธ์ด์˜ ํ•œ๊ณ„

๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ์ฃผ์†Œ์ฐฝ์— ์“ธ ์ˆ˜์žˆ๋Š” ๊ธธ์ด๋ฅผ ์ œํ•œํ•˜๊ณ  ์žˆ์Œ. 

์ž…๋ ฅํ•œ ๊ฐ’์ด ์ตœ๋Œ€๊ธธ์ด๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ์ฃผ์†Œ๊ฐ€ ์ž˜๋ฆด ์ˆ˜ ์žˆ์Œ

๋”ฐ๋ผ์„œ ๋ณด๋‚ด๋Š” ๊ธธ์ด๊ฐ€ ๊ธธ ๋•Œ get ๋ฐฉ์‹์€ ๋งž์ง€ ์•Š์Œ

 

- ์บ์‹ฑํ•  ์ˆ˜ ์žˆ์Œ

<์บ์‹œ(Cache)>
์ปดํ“จํ„ฐ์˜ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋กœ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ๊ธฐ์–ต
Cache๋ž€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋‚˜ ๊ฐ’์„ ๋ฏธ๋ฆฌ ๋ณต์‚ฌํ•ด ๋†“๋Š” ์ž„์‹œ ์žฅ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด
์บ์‹œ๋Š” ์ €์žฅ ๊ณต๊ฐ„์ด ์ž‘๊ณ  ๋น„์šฉ์ด ๋น„์‹ผ ๋Œ€์‹  ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•จ

<์›น์บ์‹œ(Web Cache)> 
์‚ฌ์šฉ์ž(Client)๊ฐ€ ์›น ์‚ฌ์ดํŠธ(Server)์— ์ ‘์†ํ•  ๋•Œ ์ •์  ์ปจํ…์ธ (์ด๋ฏธ์ง€, JS, CSS ๋“ฑ)์„ ํŠน์ • ์œ„์น˜์— ์ €์žฅํ•˜์—ฌ ์›น ์„œ๋ฒ„์— ํ•ด๋‹น ์ปจํ…์ธ ๋ฅผ ๋งค๋ฒˆ ์š”์ฒญํ•˜์—ฌ ๋ฐ›๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํŠน์ • ์œ„์น˜์—์„œ ๋ถˆ๋Ÿฌ์˜ด์œผ๋กœ์จ ์‚ฌ์ดํŠธ ์‘๋‹ต์‹œ๊ฐ„์„ ์ค„์ด๊ณ  ์„œ๋ฒ„ ํŠธ๋ž˜ํ”ฝ ๊ฐ์†Œ ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Œ

์‘๋‹ตํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์˜์ƒ, ์ด๋ฏธ์ง€, ์Œ์„ฑ ํŒŒ์ผ๊ณผ ๊ฐ™์ด ํฌ๊ธฐ๊ฐ€ ํฐ ๋ฐ์ดํ„ฐ๋“ค์ผ ๋•Œ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์‘๋‹ต์„ ๋ฐ›๊ฒŒ ๋˜๋ฉด ์„œ๋ฒ„์— ๋ถ€ํ•˜๊ฐ€ ๋ฐœ์ƒํ•จ. ๋”ฐ๋ผ์„œ ์ž์ฃผ ํ•˜๋Š” ์š”์ฒญ์ผ ๋•Œ๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ํด๋ผ์ด์–ธํŠธ pc์— ์ €์žฅํ•ด ๋†“์Œ. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋˜‘๊ฐ™์€ ์š”์ฒญ์„ ํ–ˆ์„ ๋•Œ ๊ทธ ๋ฆฌ์†Œ์Šค๋ฅผ ์„œ๋ฒ„์—์„œ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ pc์— ์ €์žฅ๋œ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ฐ€์ ธ์™€์„œ ์”€. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ›จ์”ฌ ๋น ๋ฅด๊ณ  ์„œ๋ฒ„์ชฝ์— ๋ถ€ํ•˜๊ฐ€ ์ค„์–ด๋“ฆ.

<์บ์‹ฑ>
์บ์‹ฑ ์˜์—ญ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹

 

๐Ÿ“– POST

 

- ๋ณด์•ˆ์— ๊ฐ•ํ•˜๋‹ค >> GET์— ๋น„ํ•ด ๊ฐ•ํ•˜๋‹ค๋Š” ๋œป!

 

 

โš™ Query String โœจ

url?name=value&name2=value2&...

get๋ฐฉ์‹์ผ ๋•Œ query String ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•จ

์ž„์˜์ ์œผ๋กœ Query String์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ์ „๋‹ฌํ• ์ˆ˜๋„ ์žˆ์Œ

 

๐ŸŽˆ GET/POST ์‹ค์Šต

GET ๋ฐฉ์‹์ผ ๋•Œ๋Š” ํ•œ๊ธ€์ด ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜์ง€๋งŒ, POST ๋ฐฉ์‹์ผ ๊ฒฝ์šฐ์—๋Š” ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ์ด ์•ˆ๋จ

์ด์œ  : ๋ฉ”์„œ๋“œ์— ๋”ฐ๋ผ์„œ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ

 

 

package com.smhrd.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/Ex02")
public class Ex02 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		GET๊ณผ POST์— ๋”ฐ๋ฅธ ํ•œ๊ธ€ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹
//		1) POST : ํŒจํ‚ท์˜ BODY ๋ถ€๋ถ„์— ๋ฐ์ดํ„ฐ๊ฐ€ ์‹ค์–ด์ ธ์„œ ์ „์†ก๋จ
//			>> ํŒจํ‚ท์ด ๋„˜์–ด์˜ฌ๋•Œ๋งˆ๋‹ค(์š”์ฒญ์ด ๋“ค์–ด์˜ฌ๋•Œ๋งˆ๋‹ค) ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ์ง€์ •
//			>> request.setCharacterEncoding("UTF-8");	
//			>> ๊ผญ getParameter ์œ„์— ์ž‘์„ฑํ•ด์•ผ ํ•จ! 
//			>> ์ž๋ฐ”๋Š” ์œ„์—์„œ ์•„๋ž˜๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฏ€๋กœ!
		
//		2) GET : URL์— ์‹ค์–ด์ ธ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•จ
//					>> URI๋ฅผ ์ธ์ฝ”๋”ฉํ•ด์ค˜์•ผ ํ•จ!
//					>> Server(Tomcat9) / server.xml > URIEncoding=UTF-8 ์ง€์ •

		request.setCharacterEncoding("UTF-8"); //*์œ„์น˜: getParameter ํ˜ธ์ถœ ์ด์ „!
		
		String name = request.getParameter("name");
		System.out.println(name);
		
		response.setContentType("text/html; charset=UTF-8;"); //์‘๋‹ต๋ฐ์ดํ„ฐ ํ˜•์‹(์ธ์ฝ”๋”ฉ๋ฐฉ์‹) ์ง€์ •
		PrintWriter out = response.getWriter(); // ํ…์ŠคํŠธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ
		out.print(name); // ์ŠคํŠธ๋ฆผ์„ ํ†ตํ•ด ํ…์ŠคํŠธ ์ถœ๋ ฅ
	}

}

 

๐Ÿ“– GET ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹

 

 

๐Ÿ“– POST ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹

๋ฐ์ดํ„ฐ๋ฅผ ํŒจํ‚ท์˜ ๋ฐ”๋””์— ํฌํ•จํ•ด์„œ ์š”์ฒญ (์ฆ‰, ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ์ธ์ฝ”๋”ฉ ํ•„์š”)

** POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์‹œ ๋งค๋ฒˆ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•จ

** getParameter๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์ „์— ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ๋จผ์ € ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•จ

 

 

๐Ÿ“– request.getParameter() vs request.getParameterValues()

getParamenter()๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘์—์„œ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ์Œ

>> ๋”ฐ๋ผ์„œ checkbox ํƒ€์ž…์ผ ๊ฒฝ์šฐ์—๋Š” ๋‹ค๋ฅธ ์†์„ฑ์„ ์‚ฌ์šฉ

 

ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋Š” getParameterValues() ์‚ฌ์šฉ

getParameterValues()์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์€ String[]

 

package com.smhrd.servlet;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Ex03")
public class Ex03 extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String job = request.getParameter("job");
		String gender = request.getParameter("gender");
//		String hobby = request.getParameter("hobby");
		String[] hobby = request.getParameterValues("hobby");
		
		System.out.println(job);
		System.out.println(gender);
		System.out.println(Arrays.toString(hobby));
		
	}

}

 

โ€ป check box ์™€ radio์˜ ๊ธฐ๋ณธ๊ฐ’ : on

    >> value๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด on์„ ๋ฐ˜ํ™˜

 

 

โš™ ์‹ค์Šต

 

 

package com.smhrd.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Ex04")
public class Ex04 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String id = request.getParameter("id");
		
		String pw = request.getParameter("pw");
		
		String gender = request.getParameter("gender");
		
		String blood = request.getParameter("blood");
		
		String birth = request.getParameter("birth");
		
		String color = request.getParameter("color");
		
		String text = request.getParameter("text");
		
		response.setContentType("text/html; charset=UTF-8;"); //์‘๋‹ต๋ฐ์ดํ„ฐ ํ˜•์‹(์ธ์ฝ”๋”ฉ๋ฐฉ์‹) ์ง€์ •
		PrintWriter out = response.getWriter(); // ํ…์ŠคํŠธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ
		out.print("์•„์ด๋”” : " + id);
		out.print("<br>๋น„๋ฐ€๋ฒˆํ˜ธ : " + pw);
		
		if(request.getParameter("pwcheck").equals(pw)) {
			out.print("<br>๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.");
		}else {
			out.print("<br>๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		}
		
		out.print("<br>์„ฑ๋ณ„ : " + gender);
		out.print("<br>ํ˜ˆ์•กํ˜• : " + blood);
		out.print("<br>์ƒ์ผ : " + birth);
		
		out.print("<br>");
		String[] hobby = request.getParameterValues("hobby");
		out.print("์ทจ๋ฏธ : ");
		for(int i = 0 ; i < hobby.length ; i++) {
			out.print(hobby[i] + " ");
		}
		
		out.print("<br>์ข‹์•„ํ•˜๋Š” ์ƒ‰ : " + color);
		out.print("<br>๋‚จ๊ธฐ๊ณ  ์‹ถ์€ ๋ง : " + text);
	}

}