JNDI DataSource SQL Server used in Web Application created in Netbeans and GlassFish server

In this post I describe you how configuring JEE application to connect to SQL Server database through DataSource. In my last post I use Tomact server, in this it will be GlassFish server.
First create in Netbeans new project. Choose from menu File->New Project.

a1From Categories select Java Web and in Projects section choose Web Application.

a2Click the Next button. In Project Name field write MyDataSource and click the Next button.

q1In New Web Application wizard you see server as GlassFish.

q2

Click the Next button.

q5Don’t check any box with framework and click the Finish button.
Your application select data from SQL Server so you must add driver and connection to your database. I described these things in my posts:
How add SQL Server 2012 driver to Netbeans 8?
Add connection in Netbeans 8 to the SQL Server 2012 database.

In next step you must create web.xml file. Right click project node and select New->Others.

q10From Categories choose Web and from File Types select web.xml.

q11Click the Next button.

q12Click the Finish button.

In project tree you see configuration file:

q22It is content of generating web.xml file:

s1Paste into web-app tags this code:

 <resource-ref>
    <description>Ref to SQL Server data source</description>
    <res-ref-name>jdbc/sql2012</res-ref-name>
    <res-type>javax.sql.ConnectionPoolDataSource</res-type>
    <res-auth>Container</res-auth>
 </resource-ref>

It is your new web.xml configuration file:

q23You must used the GlassFish server change content of res-type tag of web.xml from javax.sql.DataSource  to javax.sql.ConnectionPoolDataSource.
Last step it is delete index.xhtml file and create new file: index.jsp.
After delete index.xhtml file, right click project node and select New->Other.

q10

From Categories choose Web and from File Types choose JSP.

s3Click the Next button. In File Name field write index and click Finish button.

s4In tree of project you can see it file.

s5It is content this file:

s6Paste into this code:

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <html><head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>People</title>
      </head>    
<body>
            <sql:query var="db" dataSource="jdbc/sql2012">
                    select name from person;
            </sql:query>
    <h2>People</h2>  
            <c:forEach var="person" items="${db.rows}">   
                    <p><c:out value="${person.name}" /></p>
            </c:forEach>
</body>
</html>

It is new content index.jsp file:

s7Run application.

p4