Conditional JSPs
Home ] Up ] What's  a Java Server Page? ] How Does a JSP Work? ] Do JSPs Replace Servlets? ] JSP Expressions vs. Scriplets ] [ Conditional JSPs ] JSP Declarations ] A JSP Hit Counter ]

 

 

We can use scriptlets to build JSP pages that contain conditional code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
    
    <h1>JSP Page</h1>
    
    <% 
    String userAgent = request.getHeader("User-Agent");
    if (userAgent != null && userAgent.indexOf("MSIE") == -1)
    { %>
    <h2>You are a civilized person!</h2>
    <% }
    else
    { %>
    <h2>Still hanging around with Bill, huh?</h2>
    <% } %>
    
  </body>
</html>

When invoked from a non-Internet Explorer browser, this JSP produces the following result:

However, when invoked from Microsoft Internet Explorer, here's what it produces:

 
The page was last updated February 19, 2008