Passing Parameters with GET request
Learn how to pass parameters from the browser to the servlet with a GET request.
A parameter is something that is passed between the browser and the web server.
Passing a parameter in the URL
Suppose we want to pass the name of a tennis player as a parameter. The way to pass a parameter is to put a question mark at the end of the URL and provide the name of the parameter. The ?
separates the URL from the parameter:
http://localhost:8080?name=sampras
Here, we are passing a parameter called name
. The value of the parameter is sampras
.
To pass multiple parameters, the &
symbol is used as follows:
http://localhost:8080/?fname=novak&lname=djokovic
The above snippet shows how to pass two parameters, fname
and lname
, in the URL string.
Receiving parameter in servlet
To receive the parameter passed from the browser in the doGet()
method of the PlayerServlet
class, we will use the getParameter()
method and provide the name of the parameter that we wish to receive.
String playerName = request.getParameter("name");
We are storing the parameter in the String
variable playerName
.
Passing the attribute to JSP
Now that our servlet has received the parameter, the next step is to forward it to the JSP. A parameter is what is passed between the browser and the web server. An attribute is what gets passed from the web server to the JSP.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.