GET/POST방식으로 동시에 서블릿에 요청하기

Chris·2019년 8월 21일
0

GET

  • 전송할 때 URL 뒤에 name=value 형태로 전송하고, 여러 개일때는 '&'으로 연결한다.
  • 전송 데이터는 최대 255자
  • default 전송방식이며, 보안에 취약하다

POST

  • 전송할 때 TCP/IP Protocal의 HEAD 영역에 숨겨진 채 전송한다.
  • 전송데이터는 무제한
  • 처리속도가 GET 방식보다 느리고, 보안에 유리하다.

GET과 POST를 동시에 처리하는 방법

html 문서와 html 문서 내의 fn_validate()를 자바스크립트를 작성하고 http://localhost:8080/pro07/login.html로 접속한 후, 양식을 입력하면 http://localhost:8080/pro07/login4에 결과값이 나온다.

form 내의 methodpost로 하면 loginServlet.java 내의 doPost()에 요청을 하고, method
get으로 하면 doGet()에 요청하게 된다. doPost()doGet()은 모두 doHandle()를 요청한다.

WebContent/login.html

<form name="frmLogin" method="post" action="login" encType="UTF-8">
	아이디  :<input type="text" name="user_id"><br>
	비밀번호:<input type="password" name="user_pw"><br>
	<input type="button" onClick="fn_validate()" value="로그인">
	<input type="reset" value="다시입력">
	<input type="hidden" name="user_address" value="서울시 성북구" />
</form>

WebContent/login.html 내의 javascript

function fn_validate() {	
	var frmLogin = document.frmLogin;
	var user_id = frmLogin.user_id.value;
	var user_pw = frmLogin.user_pw.value;

	if (
		(user_id.length == 0 || user_id == "") ||
		(user_pw.length == 0 || user_pw =="") 
    ) {
    	alert("아이디와 비밀번호는 필수입니다.");
	} else {
		frmLogin.method = "post";
		frmLogin.action = "login4";
		frmLogin.submit();
	}
}

loginServlet.java

package sec01.ex03;

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("/login4")
public class loginServlet extends HttpServlet {
  
	public void init(){	
		System.out.println("init 메서드 호출");
	}
	
	protected void doGet(
		HttpServletRequest request, 
		HttpServletResponse response
    ) throws ServletException, IOException {
		System.out.println("doGet 메서드 호출");
		doHandle(request, response);
	}

	protected void doPost(
		HttpServletRequest request, 
		HttpServletResponse response
    ) throws ServletException, IOException {
		System.out.println("doPost 메서드 호출");
		doHandle(request, response);
	}
	
  	// doGet(), doPost() 모두 doHandle()로 처리
	private void doHandle(
		HttpServletRequest request,
		HttpServletResponse response
    ) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");   
        response.setContentType("text/html;charset=utf-8");  
        PrintWriter out = response.getWriter();				
        String id = request.getParameter("user_id");  
        String pw = request.getParameter("user_pw");  	
			
		System.out.println("아이디   : "+ id);  
		System.out.println("패스워드 : "+ pw);
		
		if (id!= null && (id.length() != 0)) {
			out.print("<html>");  
			out.print("<body>");
			out.print( id +" 님!! 로그인 하셨습니다." );
			out.print("</html>");
			out.print("</body>");
		} else {
			out.print("<html>");  
			out.print("<body>");
			out.print("아이디를 입력하세요!!!" ) ;
			out.print("<br>");
			out.print("<a href='http://localhost:8080/pro07/login.html'>로그인창으로 이동</a>");
			out.print("</html>");
			out.print("</body>");
	      }
	}

	public void destroy() {
		System.out.println("destroy 메서드 호출");
	}
}

Tomcat의 server.xml에 추가

<Context docBase="ServletFormTest" path="/ServletFormTest" reloadable="true" source="org.eclipse.jst.jee.server:ServletFormTest"/>

위 작성글은 [자바 웹을 다루는 기술]에서 간단히 정리한 것입니다.

profile
올드보이 코더

0개의 댓글