요청과 응답
WAS는 웹 브라우저로부터 Servlet요청을 받으면,
- 요청할 때 가지고 있는 정보를 HttpServletRequest객체를 생성하여 저장합니다.
- 웹 브라우저에게 응답을 보낼 때 사용하기 위하여 HttpServletResponse객체를 생성합니다.
- 생성된 HttpServletRequest, HttpServletResponse 객체를 서블릿에게 전달합니다.
HttpServletRequest
- http프로토콜의 request정보를 서블릿에게 전달하기 위한 목적으로 사용합니다.
- 헤더정보, 파라미터, 쿠키, URI, URL 등의 정보를 읽어 들이는 메소드를 가지고 있습니다.
- Body의 Stream을 읽어 들이는 메소드를 가지고 있습니다.
HttpServletResponse
- WAS는 어떤 클라이언트가 요청을 보냈는지 알고 있고, 해당 클라이언트에게 응답을 보내기 위한 HttpServleResponse객체를 생성하여 서블릿에게 전달합니다.
- 서블릿은 해당 객체를 이용하여 content type, 응답코드, 응답 메시지등을 전송합니다.
-----------------------------------------------------------------------------------------------------------------------------------
헤더 정보 읽어 들이기
-웹 브라우저가 요청정보에 담아서 보내는 hearder값을 읽어 들여 브라우저
화면에 출력한다.
-http://localhost:8079/firstweb/header
-package name : examples
-class name : HeaderServlet
-url mapping : /header
package examples;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HeaderServlet
*/
@WebServlet("/header")
public class HeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HeaderServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
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>form</title></head>");
out.println("<body>");
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
out.println(headerName + " : " + headerValue + " <br> ");
}
out.println("</body>");
out.println("</html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
----------------------------------------------------------------------------------------------------------------------------------
파라미터 읽어 들이기
- url주소의 파라미터 정보를 읽어 들여 브라우저 화면에 출력한다.
- http://localhost:8079/firstweb/param?name=kim&age=5 //값을 넘겨줘야함
- package name : examples
- class name : ParameterServlet
- url mapping : /param
package examples;
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;
/**
* Servlet implementation class ParameterServlet
*/
@WebServlet("/param")
public class ParameterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ParameterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
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>form</title></head>");
out.println("<body>");
String name = request.getParameter("name");
String age = request.getParameter("age");
out.println("name : " + name + "<br>");
out.println("age : " +age + "<br>");
out.println("</body>");
out.println("</html>");
}
}
폼테그 같은 방법으로 사용
-----------------------------------------------------------------------------------------------------------------------------------
그외 요청정보 출력
- URI, URL, PATH, Remote host등에 대한 정보 출력
- http://localhost:8079/firstweb/info
- package name : examples
- class name : InfoServlet
- url mapping : /info
package examples;
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;
/**
* Servlet implementation class InfoServlet
*/
@WebServlet("/info")
public class InfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
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>");
String uri = request.getRequestURI();
StringBuffer url = request.getRequestURL();
String contentPath = request.getContextPath();
String remoteAddr = request.getRemoteAddr();
out.println("uri : " + uri + "<br>");
out.println("url : " + url + "<br>");
out.println("contentPath : " + contentPath + "<br>");
out.println("remoteAddr : " + remoteAddr + "<br>");
out.println("</body>");
out.println("</html>");
}
}
uri : /firstweb/info
url : http://localhost:8079/firstweb/info
contentPath : /firstweb
remoteAddr : 0:0:0:0:0:0:0:1
contenPath - 웹 어플리케이션을 찾아가는 이름
remoteAddr - ip출력