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.
From Categories select Java Web and in Projects section choose Web Application.
Click the Next button. In Project Name field write MyDataSource and click the Next button.
In New Web Application wizard you see server as GlassFish.
Don’t check any box with framework and click the Finish button.
In Projects window you can see all files for this application.
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.
Now You open context.xml file from Web Pages/META-INF project node.
Paste into it this code:
You must adapt atributes to settings in your database.
In next step you must create web.xml file. Right click project node and select New->Others.
From Categories choose Web and from File Types select web.xml.
In project tree you see configuration file:
It is content of generating web.xml file:
Paste into web-app tags this code:
Ref to SQL Server data source
jdbc/sql2012
javax.sql.DataSource
Container
It is your new web.xml configuration file:
Tag 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.
Click the Next button. In File Name field write index and click Finish button.
In tree of project you can see it file.
People
select name from person;
People
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.
In Add Library wizard choose JSTL item and click the Add Library button.
In 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.
Select in your localization driver and click the Open button.
In tree of project you can see it.