[TIL] 2020.07.16_TIL-200716

To-Do

  • [복습] Servlet, JSP 기본구조 복습
  • [복습] Servlet, JSP 코드복습

Servlet

servlet

1) 클라이언트가 URL을 클릭하면 HTTP Request를 Servlet Container로 전송
2) HTTP Request를 전송받은 Servlet Container는 HttpServletRequest, HttpServletResponse 두 객체를 생성
2) Web.xml은 사용자가 요청한 URL을 분석하여 어느 서블릿에 대해 요청한 것인지 찾음
4) 해당 서블릿에서 service메소드를 호출한 후 클라이언트의 POST, GET 여부에 따라 doGet( ), doPost( )를 호출
5) doGet( ), doPost( ) 메소드는 동적 페이지를 생성한 후 HttpServletResponse객체에 응답을 보낸다
6) 응답이 끝나면 HttpServletRequest, HttpServletResponse 두 객체를 소멸시킨다

get방식, post방식 실습 (test.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet Test</title>
</head>
<body>
	<!-- get 방식 -->
	<div style="text-align: center">
	<h2>로그인_get</h2>

	<form action="http://localhost:9999/Study-WEB/test" method="get">
		ID  	 : <input type="text" name="id"><br>
		password : <input type="text" name="password"><br>
		<input type="submit">
	</form>
	</div>

	<!-- post 방식  -->
	<div style="text-align: center">
	<h2>로그인_post</h2>
	
	<form action="http://localhost:9999/Study-WEB/test" method="post">
		ID  	 : <input type="text" name="id"><br>
		password : <input type="text" name="password"><br>
		<input type="submit">
	</form>
	</div>

</body>
</html>

Servlet 코드

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getMethod();
		String uri = req.getRequestURI();
		StringBuffer url = req.getRequestURL();

		String id = req.getParameter("id");
		String password = req.getParameter("password");

		resp.setContentType("text/html; charset=utf-8");
		PrintWriter out = resp.getWriter();

		out.println("<html>");
		out.println("<head>");
		out.println("<title>로그인 페이지</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("=======================<br>");
		out.println("&nbsp;&nbsp;출력 결과<br>");
		out.println("=======================<br>");
		out.println("파라미터(id) : " + id + "<br>");
		out.println("파라미터(password) : " + password + "<br>");
		out.println("전송방식(method) : " + method + "<br>");
		out.println("URI : " + uri + "<br>");
		out.println("URL : " + url + "<br>");
		out.println("=======================");
		out.println("</body>");
		out.println("</html>");

		out.flush();
		out.close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		
		String method = req.getMethod();
		String uri = req.getRequestURI();
		StringBuffer url = req.getRequestURL();

		String id = req.getParameter("id");
		String password = req.getParameter("password");

		resp.setContentType("text/html; charset=utf-8");
		PrintWriter out = resp.getWriter();

		out.println("<html>");
		out.println("<head>");
		out.println("<title>로그인 페이지</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("=======================<br>");
		out.println("&nbsp;&nbsp;출력 결과<br>");
		out.println("=======================<br>");
		out.println("파라미터(id) : " + id + "<br>");
		out.println("파라미터(password) : " + password + "<br>");
		out.println("URI : " + uri + "<br>");
		out.println("URL : " + url + "<br>");
		out.println("=======================");
		out.println("</body>");
		out.println("</html>");

		out.flush();
		out.close();
	}
}

실행결과


doGet() 방식

servlet

doPost() 방식

servlet

태그: ,

카테고리:

업데이트:

댓글남기기