redconservatory
redconservatory

Reputation: 21934

JSP set a variable with GET vars

I know this is a simple syntax thing, but I can't figure it out.

/* url = index.jsp?topic=whatever */
Path <%= request.getParameter('topic') %> <!-- works -->
<c:set var="myVar" value="${ request.getParameter('topic') }" />
<c:out value="${myVar}" /> <!-- doesn't print out onto my web page -->

All I want to do is set myVar with the GET parameter 'topic'. How can I do it ?

Upvotes: 0

Views: 6614

Answers (3)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280178

You should be able to get it with

${param.topic}

The documentation says

One external data source that a value attribute can refer to is an implicit object.

[...]

param - A Map of the request parameters for this request, keyed by parameter name

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

This is because scriptlets in <%= %> are java expressions whereas those in ${ } are EL expressions. A different language. To get the value of a parameter, you would do: ${param.topic}

Upvotes: 3

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62874

You can get the request param with:

<c:set var="myVar" value="${param.topic}" />

and then print it

<c:out value="${myVar}" /> 

More info:

Upvotes: 2

Related Questions