JSP/Servlet | Passing Values Between Client and Server | Submitting Forms
When receiving input from users, the basic approach is still “form submission”. You put information into a form created with the <form> tag, submit it, and process the result of that submission. This is a basic HTML feature.
Text submitted from a form can be obtained with getParameter, as shown below.
String variable = request.getParameter(name);
This lets you obtain the value of the item with the specified name from the controls provided in the form. Be careful that what you specify is the name, not the ID. If you specify only an ID, such as <input id="hoge">, you cannot obtain the value. You must specify the name attribute.
If there are multiple items with the same name, you can obtain their values all at once. Use the method named getParameterValues for this.
String[] variable = request.getParameterValues(name);
getParameterValues gathers all values for the name specified as the argument and returns them as a String array. For example, it is used to obtain all values from a <select> tag that allows multiple items to be selected.
Now let’s create an example.
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String inpt = request.getParameter("input");
inpt = inpt == null ? "" : inpt;
String chk = request.getParameter("check");
chk = chk == null ? "OFF" : "ON";
String rd = request.getParameter("radio");
rd = rd == null ? "" : rd;
String[] sels = request.getParameterValues("select");
String sel = "";
if (sels != null){
for(int i = 0;i < sels.length;i++)
sel += sels[i] + " ";
}
String str = "INPUT:" + inpt + "<br>" +
"CHECK: " + chk + "<br>" +
"RADIO: " + rd + "<br>" +
"SELECT:" + sel;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample jsp</title>
<style>
h1 {font-size:16pt; background:#AAFFAA; padding:5px; }
</style>
</head>
<body>
<h1>Sample jsp page</h1>
<p>This page is a sample.</p>
<p><%=str %></p>
<table>
<form method="post" action="hello.jsp">
<tr><td>Input</td><td>
<input type="text" id="input" name="input"></td></tr>
<tr><td></td><td>
<input type="checkbox" id="c1" name="check" value="Une">
<label for="c1">Checkbox</label></td></tr>
<tr><td></td><td>
<input type="radio" name="radio" id="r1" value="first">
<label for="r1">Radio button 1</label><br>
<input type="radio" name="radio" id="r2" value="Second">
<label for="r2">Radio button 2</label></td></tr>
<tr><td></td><td>
<select id="select" name="select" multiple>
<option value="Eins">First</option>
<option value="Twei">Second</option>
<option value="Drei">Third</option>
</select></td></tr>
<tr><td></td><td>
<input type="submit" value="Send"></td></tr>
</form>
</table>
</body>
</html>
The example above sends the form to its own address and displays the submitted information together with the page. Here, the file is created as hello.jsp, so if you use a different file name, change the action of the <form> tag.
Here, the submitted form information, except for <select>, is obtained with getParameter.
String inpt = request.getParameter("input");
String chk = request.getParameter("check");
String rd = request.getParameter("radio");
You can see that all of them specify the names assigned with name as arguments. The slightly tricky part is obtaining values from <select>. This example allows multiple items to be selected.
String[] sels = request.getParameterValues("select");
String sel = "";
if (sels != null){
for(int i = 0;i < sels.length;i++)
sel += sels[i] + " ";
}
First, get a String array with getParameterValues. Then iterate through the array and obtain each value one by one. If there are no values at all, getParameterValues becomes null and the for statement causes an error, so do not forget to check that it is not null before running the loop.