Updated April 15, 2023
Introduction to JSP request
JSP Request is an object from the HttpServletRequest library. This is present implicit to the main class named in the former sentence. The request object is used to pass a lot of information while data flows from one page to another over the net. Some of the places where JSP requests are used are:
- Parameter passing.
- To retrieve the server name, server address, server port, and other server related information.
- To retrieve remote addresses of web pages over the network.
- To get, set, or delete multiple attributes during the website run.
- To pass information from one page to another via jsp pages using variables.
So JSP requests are one of the major sources of data exchange over the web. We will understand more about this topic with the help of syntax and examples provided below.
Syntax
The syntax used for JSP requests to function is given here. It uses the keyword “request” along with the attribute “getParameter”. The string which is to be stored under the variable “StringName” is passed as a parameter to it in the format below.
String StringName=request.getParameter("Name");
How did requests work in JSP?
JSP requests use some of the core implicit attributes from the HttpServletRequest object to pass the data further as the user progresses in a web search or website exploration. One of the easiest examples to understand is when we log in to any webpage, and we see “welcome userName” on the page as soon as we login. The “userName” is replaced dynamically as per the user who logged in.
IT is different and unique for all users. So here, “JSP request” is used to fetch the user name as per the login details entered by the user. The “Request” object by JSP keeps track of recorded information and responses as per the entered information. This is essential to make the page dynamic and responsive as per the user query.
Example of JSP request
Let’s look into one example, which is followed by an explanation for a better understanding of the concept.
JSPRequest2.jsp :
<jsp:useBean id="obj" class="test1JSP.CountCalculator"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example 1 of JSP Request</title>
</head>
<body>
<h1>This is an example to demonstrate the use of Request handling in JSP.</h1>
<br><br>
<p>
<%
String Username=request.getParameter("name");
int letter_count=obj.countcalculate(Username);
out.print("The number of letters containing in the string is ");
%>
<% out.print(letter_count); %><% out.print("!"); %>
</p>
</body>
</html>
JSPRequest1.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Example 1 of JSP Requests </title>
</head>
<body>
<form action="JSPRequest2.jsp" method="post">
How many number of letters are present in this letter: <input type="text" name="name"><br> <br>
<input name="name" type="submit" value="Find">
</form>
</body>
</html>
CountCalculator.java :
package test1JSP;
public class CountCalculator {
public int countcalculate(String name) {
String str = name;
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (Character.isLetter(str.charAt(i)))
count++;
}
return count;
}
}
Output:
Explanation: This is an example; three files are used to explain how the request parameter is used in the file coming from the remote file. Three files used in this sequence:
1. jsp: This is the landing page of an example provided here. On this page, a form is designed with an input box. This input box can be filled with a word containing letters or numbers. The name of the textbox is “name”. This name is declared under the form input tag. This name will be used in the next pages to retrieve the information which we are inputting on the landing page. Remember “name” as this very same name should be used as a request parameter in the text file.
2. jsp: This is the second file that gets called when the submit button is clicked on the landing page. This page contains request parameters with the parameter “name”. Remember, this should be the same as the input name provided on the first page. The request parameter’s value is captured under a string variable named “Username”. Then the username is passed to the countcalculate function, which is defined in the CountCalculator.Java file. This function counts the number of letters from the string passed initially on the landing page and returns the number of letters. The value returned by this function is recorded under the variable named “letter_count”. Then “letter_count” is displayed on the output screen.
3. java: This is an intermediary page with the business logic. In this example, we wanted to calculate the number of letters passed in the string on the landing page. The logic to calculate the number of letters in the string is written here. The if the loop is used to iterate and check each letter of the string. “Character.isLetter(str.charAt(i))” is used to check if the letter at particular iteration is a character or not. If it is a character, then the count is added with 1 each time. Suppose northern count is not added with 1 and remains as it is. This loop is terminated once it reaches the end of the string passed. Finally, an integer count is returned from this java program enabled by function countcalculate to be printed over the screen.
The JAVA file string which is passed from the landing page should again be declared as “name” so that there is no discrepancy in understanding how data flowed from the landing page to the final page. The data flow is:
JSPRequest1 (“name” under name attribute of input tag) -> JSPRequest2 (“name” with requestParameter object)-> CountCalculator ( “name” declared as a parameter in countcalculate function).
Conclusion
The JSP requests are used as an important tool for passing data values and other important information from one page to another. This enables easy transition of data flow on the web. Many attributes can be used along with request objects to fully use this functionality incorporated under JSP pages.
Recommended Articles
This is a guide to the JSP request. Here we discuss How did requests work in JSP and Example, which is followed by an explanation for a better understanding of the concept. You may also have a look at the following articles to learn more –