Request, Response 객체 이해하기 4

JOY·2022년 1월 16일
0

[Java] Servlet

목록 보기
9/9
post-thumbnail

📌 Request, Response 객체 이해하기 4


1. HttpServletRequest, HttpServletResponse

HttpServletRequest : 클라이언트가 서버에게 보낸 요청을 추상화한 객체
HttpServletResponse : 서버가 클라이언트에게 응답하기 위한 정보를 추상화한 객체

2. 그 외의 요청정보 출력

  • 클라이언트가 요청할 때 들어오는 URI, URL, PATH, Remote host 정보를 읽어들여 브라우저에 출력

실습
http://localhost:8080/firstweb/info
package name : examples
class name : InfoServlet
url mapping : /info

  • Servlet 생성

  • 서블릿 파일을 요청할 때 지정할 서블릿 이름을 info 으로 하기 위해 수정

  • InfoServlet.java 생성

package examples;
(생략)

@WebServlet("/info")
public class InfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public InfoServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    						throws ServletException, IOException {
		response.setContentType("text/html");		
		PrintWriter out = response.getWriter();
		
		out.println("<html>");
		out.println("<head><title>Info</title></head>");
		out.println("<body>");
		out.println("<p style='font-size:25px'>");
		
		String uri = request.getRequestURI();
		StringBuffer url = request.getRequestURL();
		String contextPath = request.getContextPath();
		String remoteAddr = request.getRemoteAddr();
		
		out.println("uri : " + uri + "<br>");
		out.println("url : " + url + "<br>");
		out.println("contextPath : " + contextPath + "<br>");
		out.println("remoteAddr : " + remoteAddr + "<br>");
		
		out.println("</p>");
		out.println("</body>");
		out.println("</html>");
		
	}
}

📍 출력

  • contextPath : 웹 어플리케이션과 매핑된 Path
    WAS 내의 웹 어플리케이션을 찾아가는 이름 ( 직접 지정 가능 )

  • remoteAddr : 클라이언트의 주소값

    • 로컬 컴퓨터
      localhost : 127.0.0.1
      운영체제 IPv6 - 0:0:0:0:0:0:0:1 로 출력되는 경우도 있음
profile
Just Do IT ------- 🏃‍♀️

0개의 댓글