1

I'm trying to create a dynamic web page showing different messages according to query string parameter, such as

http://localhost:8080/example?param=10

In my jsp page, to get the parameter i use

<%= request.getParameter("param") %> 

and that correctly evaluates to the correct value. Then i try to combine this with some IFs clauses to switch to different content:

<% if (request.getParameter("param") == "10") { %>
<!-- show something -->
<%
}
%>

but this doesn't seem to work.

If i try

<%= if (request.getParameter("param") == "10") { %>
<!-- show something -->
<%
}
%>

i get an internal server error.

Which is the correct approach to show dynamic content evaluating the query string parameter?

Thanks

AlterEchoes
  • 33
  • 1
  • 6

1 Answers1

0

I managed to solve the problem using JSTL libraries.

First download jstl-api.jar and jstl-impl.jar and copy them in tomcat's WEB-INF/lib directory of your webapp.

Restart tomcat and in your .jsp page add

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

at the beginning of the file

Now declare a new variable with

<c:set var="param" value='<%= request.getParameter("param") %>' />

Finally, use directives to change the visualization of content based on param value

<c:choose>
    <c:when test="${param == '10'}"> <!-- content --> </c:when>
    <c:when test="${param == '20' || param == '30'}"> <!-- content --> </c:when>
</c:choose>
AlterEchoes
  • 33
  • 1
  • 6