Java – Get URL Parameters in JSP page
This post shows you how to get value from URL parameters in a JSP page.
To do this, we simply use request.getParameter
method, it takes a parameter name as an argument. The method returns the value of a request parameter as a String
, or null
if the parameter does not exist.
The code looks simple as below:
<%
String value = request.getParameter("parameter-name");
%>
Here is a full example showing how to get value from URL parameters in JSP page.
example.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Java - Get URL Parameters in JSP page</title>
</head>
<body>
<%
String value = request.getParameter("animal");
if (value != null) {
out.write(value);
} else {
out.write("The parameter does not exist.");
}
%>
</body>
</html>
Assuming that we are opening this url: https://yourdomain.com/example.jsp?animal=Elephant
The output should be:
Elephant