O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

23.10. Accessing Request Parameters with the EL

Problem

You want to access a request parameter using the EL in a JSP.

Solution

Use the param implicit object in your JSP code.

Discussion

The JSTL provides an implicit object named param that you can use to get a request parameter. Simply follow the term "param" with a period and the parameter name. Use this terminology with the EL to output the value of a request parameter with the c:out tag. Example 23-11 displays a greeting with the visitor's name. The request might look like:

http://localhost:8080/home/welcome.jsp?name=Bruce%20Perry

If the URL does not include the "name" parameter, the JSP displays the message "Hello Esteemed Visitor."

Example 23-11. Using the JSTL in a JSP to display the result of a request parameter

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html>
<head><title>Accessing a Scoped Value</title></head>
<body>
<h2>Hello

<c:choose>

<c:when test="${empty param.name}">
 Esteemed Visitor
 </c:when>

<c:otherwise>

<c:out value="${param.name}" />

</c:otherwise>

</c:choose>

</h2>

</body>
</html>

The code tests whether the request contains a value for name by using the empty EL keyword:

<c:when test="${empty param.name}">

The c:choose , c:when, and c:otherwise tags are like if/then/else statements in Java code. If the request parameter name does not have a value, the browser will display "Esteemed Visitor". Otherwise, it displays the value of name.

Figure 23-6 shows a JSP displaying the message, including the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required