Computer >> 컴퓨터 >  >> 프로그램 작성 >> 프로그램 작성

JSP에서 모든 양식 매개변수를 읽는 방법은 무엇입니까?

<시간/>

다음은 getParameterNames()를 사용하는 일반적인 예입니다. 사용 가능한 모든 양식 매개변수를 읽으려면 HttpServletRequest의 메소드를 사용하십시오. 이 메서드는 지정되지 않은 순서로 매개변수 이름을 포함하는 열거형을 반환합니다.

열거형이 있으면 hasMoreElements()를 사용하여 표준 방식으로 열거형을 반복할 수 있습니다. 중지할 시기를 결정하는 메소드 및 nextElement() 사용 각 매개변수 이름을 가져오는 방법입니다.

<%@ 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>Param Name</th>
               <th>Param Value(s)</th>
            </tr>
            <%
               Enumeration paramNames = request.getParameterNames();
               while(paramNames.hasMoreElements()) {
                  String paramName = (String)paramNames.nextElement();
                  out.print("<tr><td>" + paramName + "</td>\n");
                  String paramValue = request.getHeader(paramName);
                  out.println("<td> " + paramValue + "</td></tr>\n");
               }
            %>
         </table>
      </center>
   </body>
</html>

다음은 Hello.htm의 내용입니다. -

<html>
   <body>
      <form action = "main.jsp" method = "POST" target = "_blank">
         <input type = "checkbox" name = "maths" checked = "checked" /> Maths
         <input type = "checkbox" name = "physics" /> Physics
         <input type = "checkbox" name = "chemistry" checked = "checked" /> Chem
         <input type = "submit" value = "Select Subject" />
      </form>
   </body>
</html>

이제 위의 Hello.htm을 사용하여 JSP를 호출해 보십시오. 이것은 제공된 입력을 기반으로 아래와 같은 결과를 생성합니다 -

모든 양식 매개변수 읽기

매개변수 이름 매개변수 값
수학 켜기
화학 켜기