사용자가 http/https를 통해 요청을 보낼 때 데이터를 함께 보내는 방식이 있는데
get / post 방식, 두 가지가 있다.
1. get 방식
get방식은 url에 정보를 데이터를 담아서 보내는 방식으로

다음과 같이 주소/파일?변수이름=데이터값 으로 보내지며
여러 데이터를 보낼 경우에는 &를 쓴다.
이를 쿼리스트링이라고 한다.
get 방식은 위와 같이 정보가 url에 노출되기 때문에 보안에 취약하며 url길이가 길면 불편하다.
따라서 간단한 데이터, 노출되어도 상관없는 정보에 대해서는 get 방식을 쓴다.
2. post 방식
post방식은 url에 직접 정보를 기입하지 않고 http구조에서 바디에 담아 보낸다.

데이터를 확인하고 싶으면 웹 브라우저의 개발자 도구에서 network창으로 확인할 수 있다.
post방식은 데이터의 직접적인 노출이 없기 때문에 get 방식보다는 일부 보안 기능을 수행한다.
또한 get방식보다 많은 데이터를 보낼 수 있기 때문에 큰 데이터를 보내는 데 적합하다.
-실습
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>getpost</title>
</head>
<body>
<h2> GET 방식</h2>
<form action = "/JSP02/getpost" method = "get">
아이디 : <input type = "text" name = "id"><br>
비밀번호 : <input type = "passward" name = "pwd"><br>
<input type = "submit" value = "get">
</form>
<h2> post 방식</h2>
<form action = "/JSP02/getpost" method = "post">
아이디 : <input type = "text" name = "id"><br>
비밀번호 : <input type = "passward" name = "pwd"><br>
<input type = "submit" value = "post">
</form>
</body>
</html>
package com.java.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("/getpost")
public class Servlet02 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get으로 서버에 받음
System.out.println("doget 메소드 입니다.");
String id = request.getParameter("id"); //name으로 받음 기본 타입은 String 타입
String pwd = request.getParameter("pwd");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset = UTF-8");
PrintWriter out = response.getWriter();
out.print("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
out.print("<title>parameter 받기</title>");
out.print("</head>");
out.print("<body>");
out.print("<p> id :" + id);
out.print("<p> pwd :" + pwd);
out.print("</body>");
out.print("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); // post방식은 request의 인코딩을 utf-8로 해주어야 함.
System.out.println("dopost 메소드입니다.");
String id = request.getParameter("id"); //name으로 받음 기본 타입은 String 타입
String pwd = request.getParameter("pwd");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset = UTF-8");
PrintWriter out = response.getWriter();
out.print("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
out.print("<title>parameter 받기</title>");
out.print("</head>");
out.print("<body>");
out.print("<p> id :" + id);
out.print("<p> pwd :" + pwd);
out.print("</body>");
out.print("</html>");
//post로 서버에 받음
}
}
-get 방식

-post방식

'컴퓨터 > JAVA' 카테고리의 다른 글
Servlet/JSP - 서블릿 필터(filter) (0) | 2023.03.27 |
---|---|
Servlet/JSP - 상태 유지 방법 (0) | 2023.03.15 |
Servlet/JSP - Servlet 개념 (0) | 2023.03.09 |
Servlet/JSP - 웹 서버(WA)와 웹 어플리케이션 서버(WAS) (0) | 2023.03.08 |
JAVA - 자바 번역기들에 대한 내용(JVM,JRE,JDK 등) (0) | 2023.02.16 |