[Jsp Study] Servlet의 핵심 사항

Noah97·2022년 5월 10일
0

JspStudy

목록 보기
2/18
post-thumbnail

클라이언트에서 서블릿으로 요청하는 방식

웹에서 클라이언트에서 서블릿으로 요청하는 방식은 대표적으로 GET과 POST 두 가지 방식으로 나누어 진다.

📒GET 방식

서버에 존재하는 간단한 페이지를 요청하거나 게시판 글 목록 페이지에서 해당 페이지에 대한 목록 출력을 요청할 때 페이지 번호와 같이 간단한 파라미터를 전송하는 경우 사용되는 방식이다.

★사용방식: <a href="list.jsp?pageNO=2">[2]</a>
  • GET 방식으로 요청이 전송되는 경우
  1. 브라우저 주소 표시줄에 주소를 직접 입력해서 요청을 전송하는 경우
  2. html의 a 태그를 사용해서 링크를 걸어서 전송하는 경우 <a href="list.jsp">목록보기</a>
  3. html 폼 태크에서 method 속성을 GET으로 지정하는 경우 <form action="name" method="GET">

📒POST 방식

단순하게 특정한 페이지를 요청하는 것이 아니라 특정 페이지로 많은 양의 파라미터를 전송하여 파라미터에 관한 처리를 할 때 POST 방식으로 요청을 전송한다. 회원 가입 요청, 게시판 글쓰기 요청, 자료실 업로드 등을 처리할 때 사용하는 방식이 모두 POST 방식에 해당한다. POST 방식으로 요청을 서버로 전송하려면 반드시 Html의 form 태그를 사용하여 method 속성을 POST로 지정하여야 한다.

★사용방식: <form name="action" method="POST">

📝GET과 POST 방식의 한글 인코딩 방법

  • 이클립스의 [Window]-[Preferences]메뉴를 클릭 한다.
  • Preferences 대화 상자에서 [Web]-[CSS Files]메뉴를 선택한다.
  • CSS Files의 Encoding을 ISO 10646/Unicode(UTF-8)로 선택후 Apply버튼을 눌러서 기본 인코딩 방식을 변경한다. HTML, jsp 메뉴도 똑같이 작업한다.

html의 charset은 UTF-8로 설정해 준다.

<meta charset="UTF-8">

GET 방식은 Servlet의 doGet부분에
response.setContentType("text/html;charset=UTF-8")을 작성해준다.

▶ex

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("hangul Process = " + name);

	}

POST 방식은 Servlet의 doPost부분에
request.setCharacterEncoding("UTF-8")을 작성해준다.

▶ex)

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8"); /*post방식 한글 인코딩*/
		String name = request.getParameter("name");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("hangul Process = " + name);
	}
profile
안녕하세요 반갑습니다😊

0개의 댓글