W3schools - JSP_Form
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
Form processing
Get
Sends the encoded user information appended to the page request
The page and the encoded information are separated by the ?
character
https://spongebob53.github.io/lecture?key1=value1&key2=value2
Is the default method to pass information from the browser to the web server and it produces a long string that appears in browser’s Location:box
If you have password or other sensitive information to pass to the server, it is better not used
It has size limitation : only 1024 characters can be in a request string
This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable which can be handled using getQueryString() and getParameter() methods of request object
Post
More reliable method of passing information to a backend program
This method packages the information in exactly the same way as the GET method,
But instead of sending it as a text string after a ?
in the URL it sends it as a separate message
This message comes to the backend program in the form of the standard input which you can parse and use for your processing
JSP handles this type of requests using getParameter() to read simple parameters and getInputStream() to read binary stream coming from the client
Reading form data
JSP handles form data parsing automatically using the following methods depending on the situation
getParameter() : Call request.getParameter() to get the value of a form parameter
getParameterValues() : If the parameter appears more than once and returns multiple values, for example checkbox
getParameterNames() : If you want a complete list of all parameters in the current request
getInputStream() : To read binary data stream coming from the client
Example
Following is an example that passes two values using the HTML FORM and the submit button
<form action = "main.jsp" method = "GET"> <!-- Can use ‘post’ -->
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
The following URL will pass two values to Form program using the GET method
https://spongebob53.github.io/lecture.html?first_name=sponge&last_name=bob
Below is the main.jsp JSP program to handle input given by web browser
<body>
<h1>Using GET Method to Read Form Data</h1>
<b>First Name:</b>
<%= request.getParameter("first_name")%> <!-- output will be “sponge” -->
<b>Last Name:</b>
<%= request.getParameter("last_name")%> <!-- output will be “bob” -->
</body>
Checkbox
Are used when more than one option is required to be selected
Uses getParameterNames() method of HttpServletRequest to read all the available form parameters
This method returns an Enumeration that contains the parameter names in an unspecified order
<table>
<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>