Servlet - Method (Get/Post)

화이티 ·2023년 12월 22일
0

Servlet

목록 보기
1/3






package Servlet;

import java.io.IOException;
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("/ex08_getPost")
public class ex08_getPost extends HttpServlet {
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//1. 전송 방식 확인하기
		String method = request.getMethod();
		System.out.println("방식" +method);
		
		//2. 데이터 확인
		String data = request.getParameter("data");
		System.out.println("데이터" +data);
	}

}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1> Get 방식

</h1>
<form action = "ex08_getPost" method = "get">
	Data: <input type = "text" name = "data">
	<input type = "submit">
	
</form>
<ol>
	<li>url에 퀴리스트링 방식으로 데이터 전송
	
	</li>
	<li>데이터 길이의 제한</li>
	<li>url에 데이터 노출 되기 때문에 보안의 취약</li>
	<li>리소스 조회(read)</li>
	<li>캐시(임시저장소)에 저장하기 때문에 데이터를 다시 로딩하지 않아도 속도 빨름</li>
	<li>form태그에 method속성을 통해사 지정하는데 따로 작성하지 않아고 get벙식으로 데이터 점송</li>
</ol>
<h1> Post 방식</h1>
<form action = "ex08_getPost" method = "post">
	Data: <input type = "text" name = "data">
	<input type = "submit">
	
</form>
<ol>
	<li>pakage의 body 부분에 데이터를 담아서 전송
		
	</li>
	<li>데이터 길이의 제한 없음</li>
	<li>직접 노출이 url에 데이터 노출 되기
		get방식보다 보앙ㄴ에 강함
	</li>
	<li>데이처 처리 및 등록</li>
	<li>method속성에 post로 지정</li>
	<li>데이터의 타입을 명시(content-type)
		<ul>
			<li> application/x-www-form-urlencoded(기본값)
				-> 데이터를 key = value 의 형태로 전송
				-> 전송하는 데이터 url encoding해서 처리
			</li>
			<li>
				multipart/form-data-> 차일업로드와 같은 바이너리 데이터 전송 시 사용
				-> multipart이름처럼 다른 종료의 여러 파일을 전송 가능
				
			</li>
			<li>
				application/json
				-> text,xml.json데이터 전송 시 사용
			</li>
			<li> text/plain -> txt 형태로 전송</li>
		</ul>
	</li>
</ol>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
 <style>
        table{
            width: '400px';
            background-color: grey;
        }
    </style>
</head>
<body>
   
        <form action="ex09_userInfo" method = "post">
            <table border ="1">
        <p>Step 1: 아이디/비번 입력</p><br>
        아이디: <input type = "text" name = "id"><br>
        비밀번호 : <input type ="password" name ="pw" ><br>
        비밀번호 확인: <input type ="password" name ="c_pw" ><br>
    </table>
    <table border ="1">
        <p>Step 2: 개인정보</p><br>
       성별: <input type ="radio" name ="gender"><input type = "radio" name = "gender"><br>
혈액형:
<select name = "blood" >
<option > A형</option>
<option >B형 </option>
<option > O형</option></select><br>

생일: <input type ="date" name = "birthday">

    </table>
    <table border ="1">
        <p>Step 3: 선호도</p><br>
        <input type = "checkbox" name = "hobby" value ="soccer"> 축구 
        <input type = "checkbox" name ="hobby" value ="baseball"> 야구 
        <input type = "checkbox" name ="hobby" value ="basket"> 농구 <br>
       종아하는 색깔: <input type ="color" name ="color">
    </table>
    <table border ="1">
        <p>Step 4: 적고 싶은 말</p><br>
        <textarea name ="board" cols="60" erows="5"></textarea>  
    </table>
    <input type ="submit" value="제출">
    <input type = "reset" class = "resetBtn" value ="초기화">
    </form>
    <script>
        $(document).on('click','.resetBtn',(e)=>{
            $('input.resetBtn+input').val('');
            $('select').val('');
            $('textarea').val('');
    
    
})
    </script>
</body>
</html>
package 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("/ex09_userInfo")
public class ex09_userInfo extends HttpServlet {
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getMethod();
		System.out.println("방식 : " +method);
		
	
		String id = request.getParameter("id");
		System.out.println("ID: " +id);
		String pw = request.getParameter("pw");
		System.out.println("PW: " +pw);
		String c_pw = request.getParameter("c_pw");
		System.out.println("Confirm PW: " +c_pw);
		String gender = request.getParameter("gender");
		System.out.println("Gender: " +gender);
		String blood = request.getParameter("blood");
		System.out.println("Blood type: " +blood);
		String birthday = request.getParameter("birthday");
		System.out.println("Birthday: " +birthday);
		
		String[] hobby = request.getParameterValues("hobby");
		System.out.print("Hobby: ");
		for(int i = 0; i < hobby.length; i++)
		{
			System.out.print("" +hobby[i]+ " ");
			
		}
		System.out.println("");
		String color = request.getParameter("color");
		System.out.println("Color: " +color);
		String board = request.getParameter("board");
		System.out.println("Board: " +board);
		
		response.setContentType("text/html;charset = utf-8");
		PrintWriter out = response.getWriter();
		out.print("<style> div{background-color: "+color+";"
				+ "width: 10px;"
				+ "height: 20px;"
				+ "display: inline-block;}</style>");
		out.println("ID: " +id);
		out.println("PW: " +pw);
		out.println("Confirm PW: " +c_pw);
		out.println("Gender: " +gender);
		out.println("Blood type: " +blood);
		out.println("Birthday: " +birthday);
		out.print("Hobby: ");
		for(int i = 0; i < hobby.length; i++)
		{
		out.print("" +hobby[i]+ " ");
			
		}
		out.println("Color: " +color);
		out.println("<div> </div>");
		out.println("Board: " +board);
	}

}

profile
열심히 공부합시다! The best is yet to come! 💜

0개의 댓글