ecsimsw

Servlet / get & post 본문

Servlet / get & post

JinHwan Kim 2020. 5. 10. 08:00

get / post

 

 doPost에서는 request를 받아서 doGet(request, response) 형태로 doGet 호출

 

 get, post 요청 차이는 https://ecsimsw.tistory.com/entry/GET-POST 에서 node.js 공부할 때 정리해두었다. 

 

GET / POST

Get / Post - 지난 포스팅에 node.js에서 filestream을 사용하는 방법을 공부하였고 예제로 파일 목록을 확인하고 내용을 읽는 게시판을 만들어보았다. 지난 포스팅에 이어 파일 생성을 구현하기에 앞서

ecsimsw.tistory.com

 

예시)

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

    public ServletEX() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		System.out.println("id : " + id + "  pw : "+pw);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

 

1. get

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Ecsimsw</title>
</head>
<body>
	<form action= "login">
		<p>
			<strong>아이디</strong>
			<input type="text" name="id" value="아이디 입력">
		</p>
		<p>
			<strong>비밀번호</strong>
			<input type="password" name="pw" value="비밀번호 입력">
		</p>
		<p>
			<input type="submit" value="제출">
		</p>
	</form>
</body>
</html>

  method를 지정하지 않아 get으로 처리

http://localhost:8090/helloJSP/login?id=Jinhwan&pw=kim

 

2. Post

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Ecsimsw</title>
</head>
<body>
	<form action= "login" method ="post">
		<p>
			<strong>아이디</strong>
			<input type="text" name="id" value="아이디 입력">
		</p>
		<p>
			<strong>비밀번호</strong>
			<input type="password" name="pw" value="비밀번호 입력">
		</p>
		<p>
			<input type="submit" value="제출">
		</p>
	</form>
</body>
</html>
http://localhost:8090/helloJSP/login

 

결과 화면 

 

id : Jinhwan  pw : kim

'Server application > Web, Servlet' 카테고리의 다른 글

WEB / HTTP 메시지 구조  (0) 2020.05.11
WEB / URL 예약 문자와 인코딩  (0) 2020.05.11
Servlet 생명 주기  (0) 2020.05.10
Servlet mapping  (0) 2020.05.10
JSP / 초기 설정  (0) 2020.05.09
Comments