카테고리 없음

퇴근 후 JSP

제이아이엔 2023. 4. 28. 17:38
반응형

https://www.tutorialspoint.com/jsp/jsp_client_request.htm

<%@ page import = "java.io.*,java.util.*" %>

<html>
   <head>
      <title>HTTP Header Request Example</title>
   </head>

   <body>
      <center>
         <h2>HTTP Header Request Example</h2>
         
         <table width = "100%" border = "1" align = "center">
            <tr bgcolor = "#949494">
               <th>Header Name</th>
               <th>Header Value(s)</th>
            </tr>
            <%
               Enumeration headerNames = request.getHeaderNames();
               while(headerNames.hasMoreElements()) {
                  String paramName = (String)headerNames.nextElement();
                  out.print("<tr><td>" + paramName + "</td>\n");
                  String paramValue = request.getHeader(paramName);
                  out.println("<td> " + paramValue + "</td></tr>\n");
               }
            %>
         </table>
      </center>
   
   </body>
</html>

 

1. HTTP 프로토콜 무엇인가?

데이터를 전송하는 통신규약 

예를 들어서 무역을 할때 다른나라 향신료를 살때 우리나라로 가져온다고 해보자

배, 항공, 기차 등을 이용해서 가져올 수 있다.

즉, 향신료를 데이터라고 하면 이걸 이동시키는 수단 중 하나를 HTTP 프로토콜이라고 할 수 있다.

 

2.HTTP Header 가 무엇인가?

클라이언트와 서버가 요청 또는 응답으로 부가적인 정보를 전송할 수 있도록 해준다라고 하는데

위 결과 처럼 HTTP를 위한 정보를 제공해준다.

 

<%@ page import = "java.io.*,java.util.*" %>

JSP 문법적 기능은 <% ~%>로 이루어져 있다고 한다. JAVA 언어를 쓸 수 있게 만든 동적 서버 페이지이기 때문이다.

<body>를 같이 공부해보자.

 

 <body>
      <center>
         <h2>HTTP Header Request Example</h2>
         
         <table width = "100%" border = "1" align = "center">
            <tr bgcolor = "#949494">
               <th>Header Name</th>
               <th>Header Value(s)</th>
            </tr>
            <%
               Enumeration headerNames = request.getHeaderNames();
               while(headerNames.hasMoreElements()) {
                  String paramName = (String)headerNames.nextElement();
                  out.print("<tr><td>" + paramName + "</td>\n");
                  String paramValue = request.getHeader(paramName);
                  out.println("<td> " + paramValue + "</td></tr>\n");
               }
            %>
         </table>
      </center>
   
   </body>

 

    <table width = "100%" border = "1" align = "center">

 

여기서 <table> 태그의 속성을 살펴 보면,

width "100%"  // 즉, 비율로 하면 브라우저 크기따라서 동적으로 바꿀 수 있는 장점이 있다.

border = "1"  // 경계선

align = "center"

 

 <tr bgcolor = "#949494">
               <th>Header Name</th>
               <th>Header Value(s)</th>
            </tr>

<tr> 은 테이블의 행이라는 걸 알 수 있죠

<th>는 그 행의 열을 만들때 씁니다.

<th>Jin</th> 을 추가하면 다음과 같이 확인 할 수 있습니다.

 <%
               Enumeration headerNames = request.getHeaderNames();
               while(headerNames.hasMoreElements()) {
                  String paramName = (String)headerNames.nextElement();
                  out.print("<tr><td>" + paramName + "</td>\n");
                  String paramValue = request.getHeader(paramName);
                  out.println("<td> " + paramValue + "</td></tr>\n");
               }
            %>
Enumeration headerNames = request.getHeaderNames();

request는 HttpServletRequest 의 객체다.

브라우저에서 서버로 보내려면 이 객체가 필요하다.

request.getHeaderNames();  Header의 각 속성 이름을 가져오는 메소드를 통해서 이름을 paramName에 넣고,

paramValue에 paramName 속성 이름을 가진 값을 getHeader(paramName) 메소드를 통해서 가져온다.

hasMoreElements() 열거형 Enumeration의 메소드 hasNext()와 비슷한 개념으로 사용되는 걸 알 수 있다.

 

 

 

 

728x90