A simple program using EL in JSP
By: Daniel Malcolm in JSP Tutorials on 2007-09-23
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. 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:
- Create the directory %TOMCAT_HOME%\webapps\expressionLanguage\WEB-INF.
- Create the web.xml file shown in Listing 3-2. Save it to the webapps\expressionLanguage\ WEB-INF folder.
- Create the JSP page in Listing 3-1 and save it to the webapps\expressionLanguage folder.
- 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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
Comments