A simple program using EL in JSP

By: Daniel Malcolm Viewed: 153370 times  Printer Friendly Format    


Now you’ll learn how you can use the EL within the body of a JSP page so that you can produce dynamic content. Listing below shows an example of a JSP page with some dynamic content generated by the EL. This page displays the value of a parameter (passed to the page) called name. The user is then given a text field in which to enter a new name, and a button to submit the name back to the page for another greeting.

templateText.jsp
<html>
<head>
<title>EL and Template Text</title>
<style>
body, td {font-family:verdana;font-size:10pt;}
</style>
<head>
<body>
<h2>EL and Template Text</h2>
<table border="1">
<tr>
<td colspan="2">Hello ${param['name']}</td>
</tr>
<tr>
<form action="templateText.jsp" method="post">
<td><input type="text" name="name"></td>
<td><input type="submit"></td>
</form>
</tr>
</table>
</body>
</html>

To run this example, you need to deploy it into a JSP 2.0– or JSP 2.1–compliant web container. As with all examples in this book, we will be using Tomcat 5.5, so you’ll need to create the deployment descriptor shown below.

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= ➥
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
</web-app>

Here is the complete list of steps needed to create, deploy, and run this example:

  1. Create the directory %TOMCAT_HOME%\webapps\expressionLanguage\WEB-INF.
  2. Create the web.xml file shown in Listing 3-2. Save it to the webapps\expressionLanguage\
    WEB-INF folder.
  3. Create the JSP page in Listing 3-1 and save it to the webapps\expressionLanguage folder.
  4. Start Tomcat, if needed, open your web browser, and go to
    http://localhost:8080/expressionLanguage/templateText.jsp.

Figure shows the page that should appear in the web browser.

The templateText.jsp displays the value submitted by the user. As you can see, this page is a very simple, personalized greeting. When the page first loads, there will be no request parameter, so the greeting will be only the word “Hello.” When the Submit Query button is clicked, the request is submitted with the parameter name. The JSP page accesses this parameter and uses an EL statement to print the greeting. You’ll look at how the request variable is accessed later, in the "Expression-Language Implicit Objects" section. For now, try entering different values within the text box and clicking Submit Query.



Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial