Using web.xml to get JSTL sql dataSource?

谁都会走 提交于 2019-12-23 05:08:20

问题


Attempting to get a dataSource for JSTL SQL operations, and it won't connect.

In my web.xml:

<context-param>
    <param-name>databaseJNDI</param-name>
    <param-value>jdbc/testDS</param-value>
</context-param>

What I'm attempting in my JSP file:

<sql:setDataSource dataSource = "jdbc/testDS"/>

I'm attempting to do this and failing in order to avoid hard-coding the database credentials into the page. The database is running, but I don't know enough about JSTL to tackle this on my own right now.

It is complaining about driver, but I have used the same design for servlets without specifying a driver to access the database.

Any insight into my issue? Knowing my luck it's probably something simple.


回答1:


Using initParam you can get context parameters.

Create context-params in web.xml for database connection properties:

<context-param>
    <param-name>driverClass</param-name>
    <param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
    <param-name>connectionURL</param-name>
    <param-value>jdbc:mysql://localhost/dbname</param-value>
</context-param>
<context-param>
    <param-name>username</param-name>
    <param-value>foo</param-value>
</context-param>
<context-param>
    <param-name>password</param-name>
    <param-value>bar</param-value>
</context-param> 

In JSP use

<sql:setDataSource var="dataSource" 
   driver="${initParam['driverClass']}"
   url="${initParam['connectionURL']}" 
   user="${initParam['username']}"
   password="${initParam['password']}" />


来源:https://stackoverflow.com/questions/35076526/using-web-xml-to-get-jstl-sql-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!