JSP/개념공부

JSP GET 방식과 POST 방식 차이

푸코잇 2023. 6. 7. 00:10
728x90

JSP에서 데이터를 전송하는 방식 중 가장 대표적인 게 GET 방식과 POST 방식이다.

그럼 GET 방식과 POST 방식의 차이에 대해 배워보자.

 

GET 방식

 

GET 방식은 주소에 데이터를 추가하여 전달하는 방식이다.

다음과 같이 쿼리 문자열(query string)에 데이터가 포함되어 전송된다.

이로 인해 보안상 취약점과 데이터 전송 길이에 제한이 있다.

대신, POST 방식보다 상대적으로 빠른 전송방식이다.

 

GET 방식 사용 예제

 

  • join_get.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GET 방식 사용 예제</title>
</head>
<body>
	<!-- 로그인 정보 입력 폼(GET 방식) -->
	<form action="join_get_ok.jsp" method="get">
		<fieldset>
			<legend>개인 정보</legend>
			<input type="text" name="name">
			<input type="date" name="birthday">
			<input type="submit" value="확인">
		</fieldset>	
	</form>
</body>
</html>

 

form 태그에 method 속성 값을 get으로 설정해줬다.

만약, method 속성 값을 입력하지 않으면 디폴트는 GET 방식이다.

 

  • join_get_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GET 방식 사용 예제</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>이름</th>
			<th>생일</th>
		</tr>
		<tr>
			<!-- 내장 객체 request -->
			<td><%=request.getParameter("name") %>
			<td><%=request.getParameter("birthday") %>
		</tr>
	</table>
</body>
</html>

 

  • 실행결과

GET 방식으로 데이터를 전송하게 되면 다음과 같이 쿼리 문자열에 데이터가 보이게 된다.


POST 방식

 

POST 방식은 데이터를 쿼리 문자열과는 별도로 첨부하여 전달하는 방식이다.

이로 인해 보안성이 높고 데이터 전송 길이에 제한이 없다.

대신, GET 방식보다 상대적으로 느린 전송방식이다.

 

POST 방식 사용 예제

 

  • join_post.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>POST 방식 사용 예제</title>
</head>
<body>
	<!-- 로그인 정보 입력 폼(POST 방식) -->
	<form action="join_post_ok.jsp" method="post">
		<fieldset>
			<legend>개인 정보</legend>
			<input type="text" name="name">
			<input type="date" name="birthday">
			<input type="submit" value="확인">
		</fieldset>	
	</form>
</body>
</html>

 

form 태그에 method 속성 값을 post로 설정해 줬다.

 

  • join_post_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>POST 방식 사용 예제</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>이름</th>
			<th>생일</th>
		</tr>
		<tr>
			<!-- 내장 객체 request -->
			<td><%=request.getParameter("name") %>
			<td><%=request.getParameter("birthday") %>
		</tr>
	</table>
</body>
</html>

 

GET 방식과 비교했을 때 해당 jsp 파일은 변동이 없다.

 

  • 실행결과

POST 방식으로 데이터를 전송했을 때 다음과 같이 쿼리 문자열에 데이터가 포함되지 않는다.


결론

 

  • GET 방식은 전송할 데이터의 양이 작거나 노출되어도 무방할 때 사용
  • POST 방식은 전송할 데이터의 양이 크거나 노출이 되면 안 될 때 사용

출처

따즈아 - 배워서 바로 써먹는 JSP 1

'JSP > 개념공부' 카테고리의 다른 글

JSP Ajax GET 방식과 POST 방식  (0) 2023.06.24
JSP Ajax(Asynchronus Javascript and XML)란?  (0) 2023.06.08
JSP 내장 객체  (0) 2023.06.06
JSP 자바빈즈(Java Beans)  (0) 2023.06.05
JSP DB 연동하기(오라클, DBeaver, JDBC)  (0) 2023.06.04