Browse DevX
Sign up for e-mail newsletters from DevX


Go From Zero to JSP 2.0 in No Time at All : Page 4

Java Server Pages 2.0 promises to make it easier for beginners and non-programmers to code highly functional, dynamic Web sites. Learn to use JSP 2.0 to connect to a database and display dynamic results on your very own web page using new features such as the expression language.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Handling the <Form> and Inserting Data
In this section you'll use more of the logic control offered by the core library, as well as another sql library tag, to insert records into your database.

The HTML <form> already does a 'get' back to this same page. You want to insert data only when the user has actually entered some data to insert. If the form is blank when the page is requested there is no action to take. This is the perfect situation for the "core" <c:if> tag. Our example application requires that the user fill out a name before it will insert a new record be inserted into the database. Place the following code toward the top of the .jsp page.

<c:if test="${!empty param.txtName}"> </c:if>

The test variable in the preceding code is the expression you are testing to determine if a certain action should occur. The process to display the HTML occurs between the starting and ending <c:if></c:if> tags.

You can test any expression using the <c:if> tag. The empty operator tests whether a collection or string is empty or null.

Table 4 shows a list of the available operators.
Table 4. Available Operators. Operators will evaluate to a "true" or "false" value.












Less than


Less than


Greater than


Greater than


Less or equal to


Less or equal to


Greater or equal to


Greater or equal to


Test for null and/or empty string

To complete the example, you need to retrieve data from the form but insert it into the table only if the user has filled out the form's txtName parameter. The <c:if> was completed so now you use the <sql:update> tag to insert the record. Insert the following code between the beginning and ending <c:if> tags from the last step.

<sql:update dataSource="${mdb}" var="updateCount"> INSERT INTO collection VALUES ('${param.txtName}', '${param.txtTitle}', '${param.txtRating}', '${param.txtGenre}') </sql:update> <p>INSERTED ${updateCount} row into database</p>

Table 5. The <sql:update> Tag. The table shows the attributes passed in the <sql:update> tag.




Refers to the database connection on which you are performing the update.


The var in this attribute will count the effected rows.

Crossing the Finish Line
Run your .jsp page. You should be able to insert records using the form and immediately see them displayed in the table of query results. If you have seen similar examples using JSP to connect to a database prior to JSP 2.0 and the JSTL you would have noticed a lot more inline Java code and associated overhead to achieve the same result. What we have done here, in effect, is query a database and handle form processing using a small number of tags.

A couple of things worth noting: Remember that, unlike HTML and VBScript, Java and its tags and tag attributes are case-sensitive. For example, you will get an error if you use a 'datasource' attribute instead of a 'dataSource' attribute. Also, in any programming language, limiting the amount of code in the template pages is good practice. This makes the tags easier to work around and easier maintain, particularly for fledgling developers or those who are accustomed to HTML.

While the "sql" library is useful in most cases it is bad practice to include database code or complex business logic in your .jsp pages. In the Java world, accessing the database and performing business logic is better done in servlets, JavaBeans, and EJBs. Once you become familiar with JSP, I recommend learning more about Java servlets, and later a Model View Controller (MVC)-based framework. MVC Frameworks are a set of proven design patterns that help you create more maintainable applications by separating business logic and data access (model) from your templates (view) with requests being marshaled through a tier representing the controller. For a great out of the box framework check out Jakarta Struts.

John D. Haro is a managing Technology Director for AGENCY.COM (Fortune 1000 focused multi-disciplined consulting company ). He also enjoys the title of Java Practice Director for AGENCY.COM where he guides and evangelizes the use of Java technology for the company. He has 8 years of experience on a wide range of applications across both Microsoft and Java languages. Reach him via e-mail at jharo@agency.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date