JNDI DataSource SQL Server configuration in Tomcat for only one JSP application

In this post I describe you how configuring JEE application to connect to SQL Server database through DataSource.
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.


In New Web Application wizard you see server as GlassFish.

q2Change it for Tomcat.

q3q4Click the Next button.

q5Don’t check any box with framework and click the Finish button.
In Projects window you can see all files for this application.

q6Your 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.

Now You open context.xml file from Web Pages/META-INF project node.
Paste into it this code:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/MyDataSource">

You must adapt atributes to settings in your database.
q7In 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:


It is content of generating web.xml file:

s1Paste into web-app tags this code:

    <description>Ref to SQL Server data source</description>

It is your new web.xml configuration file:

s2Tag res-ref-name must be the same as name atribute in Resource tag in context.xml file.
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.


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"
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <sql:query var="db" dataSource="jdbc/sql2012">
                    select name from person;
            <c:forEach var="person" items="${db.rows}">   
                    <p><c:out value="${person.name}" /></p>

It is new content index.jsp file:


You need either JSTL library in your project. So in Projects window right click the Library node and choose Add Library.

w1In Add Library wizard choose JSTL item and click the Add Library button.

w2In project tree you can see it in Libraries node:


You must add ito Netbeans IDE either jdbc driver for SQL Server.

Right click Libraries node and choose Add JAR/Folder.

p1Select in your localization driver and click the Open button.

p2In tree of project you can see it.

p3Run application.