JSP
URL Parameters
Web Development
Java
Servlet

How to get URL parameters in JSP with request.getParameter

Codemia Knowledge Hub ·

Use the implicit request object: request.getParameter("name") returns the first value of a query string parameter (or null if it is absent), and request.getParameterValues("name") returns every value when the name repeats.

Query string format

Parameters sent with a form submission or page request arrive in the URL's query string:

plaintext
http://example.com/page.jsp?param1=value1&param2=value2

request.getParameter

In JSP, request is an instance of javax.servlet.http.HttpServletRequest. Call request.getParameter(String name) for each value:

jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2<html>
3<head><title>Parameter Example</title></head>
4<body>
5<%
6    String param1 = request.getParameter("param1");
7    String param2 = request.getParameter("param2");
8%>
9<p>Param 1: <%= param1 %></p>
10<p>Param 2: <%= param2 %></p>
11</body>
12</html>

For http://example.com/page.jsp?param1=hello&param2=world, the output is:

 
Param 1: hello
Param 2: world

Parameters with the same name

A URL can repeat a parameter name:

plaintext
http://example.com/page.jsp?param=one&param=two&param=three

request.getParameterValues(String name) returns all the values as an array:

jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2<html>
3<head><title>Multiple Parameters Example</title></head>
4<body>
5<%
6    String[] paramValues = request.getParameterValues("param");
7    if (paramValues != null) {
8        for (String val : paramValues) {
9%>
10<p>Param: <%= val %></p>
11<%
12        }
13    }
14%>
15</body>
16</html>

Request methods for parameters

MethodDescriptionReturn Type
request.getParameter(String)Returns first value for a named parameterString
request.getParameterValues(String)Returns all values for a named parameterString[]
request.getParameterMap()Returns a map of parameters and their valuesMap<String,String[]>
request.getQueryString()Retrieves the full query stringString

URL encoding

Make sure parameter values are URL-encoded so special characters in them do not break the query string.

Defaults for missing parameters

Check for null to avoid a NullPointerException, and fall back to a default:

jsp
1<%
2    String param1 = request.getParameter("param1");
3    String defaultValue = (param1 != null) ? param1 : "default";
4%>
5<p>Param 1: <%= defaultValue %></p>

Escaping output to prevent XSS

Printing user-supplied values directly opens you to Cross-Site Scripting (XSS). Sanitize input, and use JSTL's <c:out> to escape HTML characters:

jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<p>Param 1: <c:out value="${param.param1}" /></p>

The implicit EL param object reads the request parameter. A bare ${param1} would look up a scoped attribute named param1, not the request parameter or the scriptlet variable.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms