Updated April 3, 2023
Introduction to JSP getParameter
The following article provides an outline for JSP getParameter. getParameter is a function name in JSP which is used to retrieve data from an HTML/JSP page and passed into the JSP page. The function is designated as getParameter() function. This is a client-side data retrieval process. The full function can be written as request.getParameter(). This function can have a string as a parameter to extract the value corresponding to the string provided. It calls an API (Application programming interface) method named javax.servlet.http.HttpServletRequest. This API method will get back the value corresponding to the input string and then pass that value as a “string” again to the JSP page.
Syntax:
The syntax for the getParameter function is:
Parameter paramValue1: <%= request.getParameter("paramValue") %>
Here paramValue1 is a variable used to record a value, and paramValue is the name of a variable defined on the source page.
So, in this case, the source page should have:
<form>
Input Text: <input type = "text" name = "paramValue" />
</form>
How getParameter works in JSP?
It is an inbuilt function of JSP which extracts the data on the basis of “name” assigned to value in HTML Page. This value is then passed to another page via the “GET” or “POST” method. There are two ways of pulling out the data. It can be the “GET” method or the “POST” method. This is selected in the HTML page <form> tag. “GET” uses a URL, and the user can see the parameter value in the url. Someone can manipulate the value in the URL, so this is not a very secure method to send data from one page to another. While the “POST” method does not show data in the URL. “Post” is more secure for passing data from one page to another.
There are two more functions linked to get parameter function in JSP, and these are:
- getParameterValues(): This function is used if multiple values are returned as parameters from the page to the JSP page. For example, if a checkbox is used instead of a radio button or multiple option selection is enabled.
- getParameterNames(): This function is used to pull out all the values from the current request of the page. All the parameters will be pulled in using this function.
Examples of JSP getParameter
Given below are the examples to demonstrate the use of JSP getParameters. These examples will help you know how to use JSP getParameter to achieve data flow throughout the process.
Example #1
Code:
Index.html:
<html>
<body>
<form action = "NewFile.jsp" method = "POST">
Parameter 1: <input type = "text" name = "p1">
<br />
Parameter 2: <input type = "text" name = "p2" />
<input type = "submit" value = "Enter values" />
</form>
</body>
</html>
NewFile.jsp:
<html>
<head>
<title>Using GET Parameter function to Read Form Data from a page</title>
</head>
<body>
<center>
<h1>getParameter() function with POST method to pass data from one page to another.</h1>
<ul>
<li><p><b>Parameter 1:</b>
<%= request.getParameter("p1")%>
</p></li>
<li><p><b>Parameter 2:</b>
<%= request.getParameter("p2")%>
</p></li>
</ul>
</body>
</html>
Output:
Explanation:
- In this example, two values are passed from the HTML page with the POST method, so you would not find values in the URL. The names of parameters are “p1” and “p2”. The action tag in HTML form calls for NewFile.jsp. NewFile.jsp is reading the data from API and outputting them on the screen. request.getParameter() function is calling this API from jsp page.
- The name “p1” and “p2” are sent as input parameters to the getParameter() function to check for the values of this name in the Index.html page. If found, then these values are printed.
Example #2
Code:
Index.html:
<html>
<body>
Select your favorite subjects from the options given below:
<form action = "NewFile.jsp" method = "POST" target = "_blank">
<input type = "checkbox" name = "Hindi" checked = "checked" /> Hindi
<input type = "checkbox" name = "English" /> English
<input type = "checkbox" name = "Maths"/> Maths
<input type = "checkbox" name = "Social science" /> Social science
<input type = "checkbox" name = "Arts"/> Arts
<input type = "submit" value = "Select Subject" />
</form>
</body>
</html>
NewFile.jsp:
<html>
<head>
<title>Reading Check-box Data</title>
</head>
<body>
<h1>Hello User: </h1>
<h1>Greetings! below is the list of your favorite subjects with a flag "on":</h1>
<ul>
<li><p><b>Maths is:</b>
<%= request.getParameter("Maths")%>
</p></li>
<li><p><b>English Flag:</b>
<%= request.getParameter("English")%>
</p></li>
<li><p><b>Hindi Flag:</b>
<%= request.getParameter("Hindi")%>
</p></li>
<li><p><b>Social science Flag:</b>
<%= request.getParameter("Social science")%>
</p></li>
<li><p><b>Arts Flag:</b>
<%= request.getParameter("Arts")%>
</p></li>
</ul>
</body>
</html>
Output:
Explanation:
- This example is using checkboxes instead of plain input. So here, we can pass multiple parameters instead of just one. The name of each checkbox should be different, so whichever is selected can be pulled out. The action tag again calls for NewFile.jsp, which has the getParameter function. This function searches on the basis of the input parameter name.
- If it is selected, then this function returns “on” otherwise “null”. This example outputs the flag status. It is “on” or “null” if the subject flag is switched “on” or “null”, respectively.
Conclusion
getParameter() function is one of the very important functions to pass data between changes. To successfully pass the data in a multi-page distributed environment, it is important to establish a common link. This link is the “name” of the parameters. With the help of these parameter names, parameter values are passing from one page to another using an API (javax.servlet.http.HttpServletRequest). This is a very commonly used function while designing dynamic pages.
Recommended Articles
This is a guide to JSP getParameter. Here we discuss the introduction, how getParameter works in JSP? and examples, respectively. You may also have a look at the following articles to learn more –