JNDI – select data from database through JSTL tags.

In last post I describe how configure GlassFish server for JNDI resource ( SQL Server 2012 database ). So In this post we test it in application.

Open Netbeans IDE. Create new project. Click File and choose New Project.

a1The New Project window appear. From Categories select Java Web and from Projects section choose Web Application.

a2Click the Next button. In New Web Application window in Project Name field write JSTLTest.

a3Click the Next button.

a4In next step click Next button.

a5Click Finish button. In Projects window You see your JSTLTest project.

a6Delete from Web Pages node index.html file.

a7a8

And then You must configure your application adding DataSource for SQL Server database. So right click your project node and choose New->Other.

b1In New File window from Categories choose Web and form File Types select Standard Deployment Descriptor(web.xml)

b2Click the Next button. New configuration data appear.

b3Click the Finish button.

The web.xml file opened.

b4

It is configuration file so it places in Configuration Files node.b5

Click the References tab.

b6After click the Add button You see dialog window.

b8

In Resource Name write name of JDBC Resource configured in GlassFish server.

b9It was in GlassFish jdbc/sql2012:

16In Resource References display row with data.

b10Click the Source tab.

b11Configuration file is prepared. Create index.jsp file. Right click Web Pages node and choose New->JSP.

b20In File Name field write index.

b21Click the Finish button.

You see content:

b22Change this html content. Paste into it this new content:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Persons</title>
    </head>
    <body>
        <h1>List of persons:</h1>
        <sql:query var="persons" dataSource="jdbc/sql2012">
            SELECT id,name FROM mytest
        </sql:query>
        <table border="1">
            <thead>
                <tr>
                    <th>id</th>
                    <th>name</th>
                </tr>
            </thead>
            <tbody>
            <c:forEach var="person" items="${persons.rows}">            
                <tr>
                    <td>${person.id}</td>
                    <td>${person.name}</td>
                </tr>
             </c:forEach>   
            </tbody>
        </table>
    </body>
</html>

Your index.jsp file:

c1Run application.

c2Browser display list of person from SQL Server.