상세 컨텐츠

본문 제목

자바 서블릿 API, Servlet API

IT/프로그래밍

by James Lee. 2015. 12. 6. 20:35

본문

Servlet에는 request 파라미터와 response파라미터가 있다.


이중 request 파라미터의 API를 알아보기 위해 직접 타이핑하여 결과들을 확인했다.


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

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 TestServlet
 */
@WebServlet("/TestServlet1")
public class TestServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public TestServlet() {
 }

 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  response.setCharacterEncoding("UTF-8");
  PrintWriter printWriter = response.getWriter();

  printWriter.write("<!DOCTYPE html>");
  printWriter.write("<html>");
  printWriter.write("<head>");
  printWriter.write("<meta charset=\"UTF-8\">");
  printWriter.write("<title>Insert title here</title>");
  printWriter.write("</head>");

  printWriter.write("<body>");
  printWriter.write("TestServlet 1<br/>");

  printWriter.write("request.getAsyncContext():"
    + request.getAsyncContext() + "<br/>");
  printWriter.write("request.getAttribute(name):"
    + request.getAttribute("name") + "<br/>");
  Enumeration<String> attributeNames = request.getAttributeNames();
  // 콜렉션을 프린트,
  printWriter.write("request.getAttributeNames() : " + attributeNames
    + "<br/>");

  while (attributeNames.hasMoreElements()) {
   System.out.println("실행?");
   printWriter.write(attributeNames.nextElement());
   printWriter.write("<br/>");
  }

  printWriter.write("request.getAuthType() : " + request.getAuthType()
    + "<br/>");
  printWriter.write("request.getCharacterEncoding() : "
    + request.getCharacterEncoding() + "<br/>");
  printWriter.write("request.getContentLength() : "
    + request.getContentLength() + "<br/>");

  printWriter.write("request.getContentType() : "
    + request.getContentType() + "<br/>");
  printWriter.write("request.getDispatcherType() : "
    + request.getDispatcherType() + "<br/>");

  printWriter.write("request.getLocalAddr() : " + request.getLocalAddr()
    + "<br/>");

  printWriter.write("request.getLocale() : " + request.getLocale()
    + "<br/>");

  Enumeration<Locale> locales = request.getLocales();
  printWriter.write("request.getLocales() : <br/>");

  while (locales.hasMoreElements()) {
   printWriter.write((locales.nextElement().toString() + "|"));
  }
  printWriter.write("<br/>");
  printWriter.write("request.getLocalName() : " + request.getLocalName()
    + "<br/>");

  printWriter.write("request.getLocalPort() : " + request.getLocalPort()
    + "<br/>");

  printWriter.write("request.getParameter(\"name\"): "
    + request.getParameter("name") + "<br/>");

  Map<String, String[]> parameterMap = request.getParameterMap();
  printWriter.write("request.getParameterMap(): <br/>");

  Set key = parameterMap.keySet();

  for (Iterator<String> iterator = key.iterator(); iterator.hasNext();) {
   String keyName = (String) iterator.next();
   String[] valueName = parameterMap.get(keyName);
   printWriter.write(keyName);
   for (int j = 0; j < valueName.length; j++) {
    printWriter.write(" : " + valueName[j]);
   }
   printWriter.write("<br/>");
  }

  Enumeration<String> parameterNames = request.getParameterNames();

  printWriter.write("request.getParameterNames(): <br/>");

  while (parameterNames.hasMoreElements()) {
   String parameterName = parameterNames.nextElement();
   printWriter.write("파라미터 명 : " + parameterName + "/value 값"); // 이름
                   // 가져옴
   String[] parameterValues = request
     .getParameterValues(parameterName);
   for (int i = 0; i < parameterValues.length; i++) {
    printWriter.write(" , " + parameterValues[i]); // 값을 출ㄹ
   }
   printWriter.write("<br/>");
  }
  printWriter.write("request.getProtocol(): " + request.getProtocol()
    + "<br/>");
  printWriter.write("request.getReader(): " + request.getReader()
    + "<br/>");
  printWriter.write("request.getRealPath(\"path\"): " + request.getRealPath("path") + "<br/>");
  
  printWriter.write("request.getRemoteAddr(): " + request.getRemoteAddr()
    + "<br/>");

  printWriter.write("request.getRemoteHost(): " + request.getRemoteHost()
    + "<br/>");

  printWriter.write("request.getRemotePort(): " + request.getRemotePort()
    + "<br/>");

  printWriter.write("request.getRequestDispatcher(\"path\"): "
    + request.getRequestDispatcher("path") + "<br/>");

  printWriter.write("request.getScheme(): " + request.getScheme()
    + "<br/>");

  printWriter.write("request.getServerName(): " + request.getServerName()+ "<br/>");

  printWriter.write("request.getServerPort(): " + request.getServerPort()+ "<br/>");

  printWriter.write("request.getServerPort(): " + request.getServerPort()+ "<br/>");

  printWriter.write("request.getServletContext(): "+ request.getServletContext() + "<br/>");

  printWriter.write("request.getAsyncContext(): "+ request.getAsyncContext() + "<br/>");

  printWriter.write("request.isSecure(): " + request.isSecure() + "<br/>");
  printWriter.write("</body>");
  printWriter.write("</html>");
 }

 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
 }

}
결과는 아래와 같다.
URL을 http://localhost:8080/servletStudy/TestServlet1?test1=1&test2=2&test1=%22testVal%22&test3=3 로 입력했을 때의 결과
TestServlet 1
request.getAsyncContext():null
request.getAttribute(name):null
request.getAttributeNames() : java.util.Collections$3@7e1efee4
request.getAuthType() : null
request.getCharacterEncoding() : null
request.getContentLength() : -1
request.getContentType() : null
request.getDispatcherType() : REQUEST
request.getLocalAddr() : 127.0.0.1
request.getLocale() : ko_KR
request.getLocales() : 
ko_KR|ko|en_US|en|ja|
request.getLocalName() : localhost
request.getLocalPort() : 8080
request.getParameter("name"): null
request.getParameterMap(): 
test1 : 1 : "testVal"
test2 : 2
test3 : 3
request.getParameterNames(): 
파라미터 명 : test1/value 값 , 1 , "testVal"
파라미터 명 : test2/value 값 , 2
파라미터 명 : test3/value 값 , 3
request.getProtocol(): HTTP/1.1
request.getReader(): org.apache.catalina.connector.CoyoteReader@7859c77f
request.getRealPath("path"): null
request.getRemoteAddr(): 127.0.0.1
request.getRemoteHost(): 127.0.0.1
request.getRemotePort(): 41293
request.getRequestDispatcher("path"): org.apache.catalina.core.ApplicationDispatcher@70d90f57
request.getScheme(): http
request.getServerName(): localhost
request.getServerPort(): 8080
request.getServerPort(): 8080
request.getServletContext(): org.apache.catalina.core.ApplicationContextFacade@74e69594
request.getAsyncContext(): null
request.isSecure(): false

알게 된 것
request.parameterValues(parameterName)에서 반환형이 String[]인 이유는 똑같은 parameterName으로 여러 개의 값이 들어왔을 때의 경우를 처리하기 위함이다. (test1이라는 이름으로 1, "testVal"이 들어옴)
<pre>map에서 key, val을 확인하는 방법</pre><pre> </pre>
값을 확인할 수 있었던 메소드들
<pre>request.getDispatcherType() : REQUEST // 어떤 메소드인지 아직 모르겠다.
request.getLocalAddr() : 127.0.0.1 // 현재 접속한 주소 파악 (local, loopback은 저렇게, 자신 IP주소로 들어가면 자신의 IP주소를 보여준다)
request.getLocale() : ko_KR // 현재 어디에서 접속했는지
request.getLocales() : ko_KR | ko | en_US | en | ja | // Locales의 값들을 가져오는 것 같은데 왜 이렇게 나온지는 나도 잘 모르겠다.
request.getLocalName() : localhost // loopback(127.0.0.1) 혹은 로컬 호스트로 들어갔을때는 저렇게 나오고 아이피 주소로 입력시 입력한 IP로 나온다.</pre><pre>request.getLocalPort() : 8080 //요청의 Port Number를 보여주는 것 같다.
request.getParameterMap(): // 파라미터로 가져온
test1 : 1 : "testVal"
test2 : 2
test3 : 3
request.getParameterNames(): 
파라미터 명 : test1/value 값 , 1 , "testVal"
파라미터 명 : test2/value 값 , 2
파라미터 명 : test3/value 값 , 3
request.getProtocol(): HTTP/1.1
request.getRemoteAddr(): 127.0.0.1
request.getRemoteHost(): 127.0.0.1
request.getRemotePort(): 41293
request.getScheme(): http
request.getServerName(): localhost
request.getServerPort(): 8080
request.isSecure(): false</pre>


'IT > 프로그래밍' 카테고리의 다른 글

이클립스 Fommater 설정(자동 줄바꿈, 줄 간격 설정)  (2) 2015.12.06
선형과 비선형의 차이  (0) 2015.12.06
리눅스 ps 명령어  (0) 2015.12.06
cmd DNS 주소 확인하기  (0) 2015.12.06
컴퓨터 운영체제의 구조  (0) 2015.12.06

관련글 더보기

댓글 영역