使用web.xml获取JSTL sql数据源? [英] Using web.xml to get JSTL sql dataSource?

查看:44
本文介绍了使用web.xml获取JSTL sql数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图获取用于JSTL SQL操作的数据源,并且它将无法连接.

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

在我的web.xml中:

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

我在JSP文件中尝试执行的操作

What I'm attempting in my JSP file:

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

我正在尝试执行此操作,但为了避免将数据库凭据硬编码到页面中而失败.数据库正在运行,但是我对JSTL的了解还不足以立即自己解决这个问题.

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.

它在抱怨驱动程序,但是我对servlet使用了相同的设计,但没有指定用于访问数据库的驱动程序.

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.

推荐答案

使用initParam您可以获取上下文参数.

Using initParam you can get context parameters.

web.xml中为数据库连接属性创建context-params:

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> 

在JSP中使用

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

这篇关于使用web.xml获取JSTL sql数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆