tomcat的Context配置
2013-12-28 14:20
531 查看
1.把DataSource设置到我们的WEB项目中,下面详细的介绍下:
第一步:在我们的WEB项目中的META-INF文件夹下建立一个context.xml
Xml代码
<?xml
version='1.0'
encoding='utf-8'[b]?>
[/b]
<Context>
<Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/bbs"
username="root"
password="root"
maxActive="50"
maxIdle="20"
maxWait="10000"
/>
</Context>
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/bbs"
username="root"
password="root"
maxActive="50"
maxIdle="20"
maxWait="10000" />
</Context>
第二步:在我们的WEB项目下的WEB-INF文件夹下建立一个web.xml(如果存在了就不用了,直接修改就行了)
(这几天测试了一下,不做这步也可以,O(∩_∩)O哈哈~省事了)
Xml代码
<resource-ref>
<description>DB
Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
第三步:我们就可以用代码来获取Connection对象了
Java代码
package xushun.util;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DBHelper {
public static Connection
getConnection() throws SQLException,NamingException
{
//
初始化查找命名空间
Context
initContext = new InitialContext();
Context
envContext = (Context)initContext.lookup("java:/comp/env");
//
找到DataSource
DataSource
ds = (DataSource)envContext.lookup("jdbc/mysql");
return
ds.getConnection();
}
}
package xushun.util;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DBHelper {
public static Connection getConnection() throws SQLException,NamingException
{
// 初始化查找命名空间
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
// 找到DataSource
DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
return ds.getConnection();
}
}
2.把DataSource设置到我们的Tomcat中,下面详细的介绍下(测试用的JAVA代码和上面的一样就不帖出了):
这里我查到的设置方法就有了一点区别了。有的人把DataSource设置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就写在context.xml中了
先说下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到
Xml代码
<GlobalNamingResources>
<!-- Editable user database
that can also be used by
UserDatabaseRealm
to authenticate users
-->
<Resource
name="UserDatabase"
auth="Container"
type="org.apache.catalina.UserDatabase"
description="User
database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"
/>
</GlobalNamingResources>
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
修改为
Xml代码
<GlobalNamingResources>
<!-- Editable user database
that can also be used by
UserDatabaseRealm
to authenticate users
-->
<Resource
name="UserDatabase"
auth="Container"
type="org.apache.catalina.UserDatabase"
description="User
database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"
/>
<Resource
name="jdbc/bbs"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
</GlobalNamingResources>
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/bbs"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
</GlobalNamingResources>
第二步:在Tomcat的conf文件夹下的context.xml中加入
Xml代码
<ResourceLink
name="jdbc/bbs"
global="jdbc/bbs"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>
第三步:就是在WEB项目的WEB-INF中的web.xml添加
Xml代码
<resource-ref>
<description>DB
Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
还有就是在Tomcat文档中提到的方法,直接修改context.xml文件了
在Tomcat的conf文件夹下的context.xml中加入
Xml代码
<Resource
name="jdbc/bbs"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
<Resource name="jdbc/bbs"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
然后就是在WEB项目的WEB-INF中的web.xml添加
Xml代码
<resource-ref>
<description>DB
Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type[b]>
[/b]
<res-auth>Container</res-auth[b]>
[/b]
</resource-ref[b]>
[/b]
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
就是这些了,如果有什么不太清楚的就留言,一起研究下。等以后我在搜集下资料整理出上面用到的XML文件中各个标签的属性及其代表的意思。有兴趣的也可以自己先查下。:-)
JNDI 查找名称 关联的引用
java:comp/env 应用程序环境条目
java:comp/env/jdbc JDBC 数据源资源管理器连接工厂
java:comp/env/ejb EJB 引用
java:comp/UserTransaction UserTransaction 引用
java:comp/env/mail JavaMail 会话连接工厂
java:comp/env/url URL 连接工厂
java:comp/env/jms JMS 连接工厂和目标
java:comp/ORB 应用程序组件之间共享的 ORB 实例
————————————————————————————以上内容是转载的————————————————————————————————
————————————————————————————以下是我自己做的————————————————————————————————
1、配置tomcat中的context.xml文件(tomcat目录\conf\context.xml)
<?xml version='1.0'encoding='utf-8'?>
<Context>
<!--
name
: 指定Resource的JNDI名字
type
: 指定Resource所属的java类名
auth
: 指定管理Resource的Manager,Containei表示由容器来管理Resource
maxActice
: 处于活动状态的数据库连接的最大数目
maxIdle
: 处于空闲状态的数据库连接的最大数目
maxWait
: 指定数据库连接池中处于空闲状态连接的最长时间,以毫秒为单位
username
: 连接数据库的用户名
password
: 连接数据库的密码
driverClassName
: 连接数据的JDBC驱动程序
url
: 连接数据库的URL
-->
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="连接数据库的用户名"
password="连接数据库的密码"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://服务器地址:端口号默认是3306/数据库名?useUnicode=true&characterEncoding=UTF-8" />
<ResourceLink global="jdbc/mysql"name="jdbc/mysql"
type="javax.sql.DataSource"/>
<!-- Default set of monitoredresources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
2、将mysql-connector-java-5.0.8-bin.jar
复制到 tomcat目录\lib
目录中
3、在javabean中获取数据库连接池
DBConnection.java
privatestatic Connection
conn=null;
public
void newGetCon(){
try{
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn=ds.getConnection();
}catch(Exception ex){
ex.printStackTrace();
}
}
第一步:在我们的WEB项目中的META-INF文件夹下建立一个context.xml
Xml代码
<?xml
version='1.0'
encoding='utf-8'[b]?>
[/b]
<Context>
<Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/bbs"
username="root"
password="root"
maxActive="50"
maxIdle="20"
maxWait="10000"
/>
</Context>
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/bbs"
username="root"
password="root"
maxActive="50"
maxIdle="20"
maxWait="10000" />
</Context>
第二步:在我们的WEB项目下的WEB-INF文件夹下建立一个web.xml(如果存在了就不用了,直接修改就行了)
(这几天测试了一下,不做这步也可以,O(∩_∩)O哈哈~省事了)
Xml代码
<resource-ref>
<description>DB
Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
第三步:我们就可以用代码来获取Connection对象了
Java代码
package xushun.util;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DBHelper {
public static Connection
getConnection() throws SQLException,NamingException
{
//
初始化查找命名空间
Context
initContext = new InitialContext();
Context
envContext = (Context)initContext.lookup("java:/comp/env");
//
找到DataSource
DataSource
ds = (DataSource)envContext.lookup("jdbc/mysql");
return
ds.getConnection();
}
}
package xushun.util;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DBHelper {
public static Connection getConnection() throws SQLException,NamingException
{
// 初始化查找命名空间
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
// 找到DataSource
DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
return ds.getConnection();
}
}
2.把DataSource设置到我们的Tomcat中,下面详细的介绍下(测试用的JAVA代码和上面的一样就不帖出了):
这里我查到的设置方法就有了一点区别了。有的人把DataSource设置在Tomcat的server.xml文件的GlobalNamingResources下面,然后在context.xml中去映射。有的直接就写在context.xml中了
先说下在server.xml添加DataSource
第一步:在Tomcat的conf中的server.xml文件中找到
Xml代码
<GlobalNamingResources>
<!-- Editable user database
that can also be used by
UserDatabaseRealm
to authenticate users
-->
<Resource
name="UserDatabase"
auth="Container"
type="org.apache.catalina.UserDatabase"
description="User
database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"
/>
</GlobalNamingResources>
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
修改为
Xml代码
<GlobalNamingResources>
<!-- Editable user database
that can also be used by
UserDatabaseRealm
to authenticate users
-->
<Resource
name="UserDatabase"
auth="Container"
type="org.apache.catalina.UserDatabase"
description="User
database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"
/>
<Resource
name="jdbc/bbs"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
</GlobalNamingResources>
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/bbs"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
</GlobalNamingResources>
第二步:在Tomcat的conf文件夹下的context.xml中加入
Xml代码
<ResourceLink
name="jdbc/bbs"
global="jdbc/bbs"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/bbs" global="jdbc/bbs" type="javax.sql.DataSource"/>
第三步:就是在WEB项目的WEB-INF中的web.xml添加
Xml代码
<resource-ref>
<description>DB
Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
还有就是在Tomcat文档中提到的方法,直接修改context.xml文件了
在Tomcat的conf文件夹下的context.xml中加入
Xml代码
<Resource
name="jdbc/bbs"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
<Resource name="jdbc/bbs"
auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="5000"
username="root"
password="admin"
url="jdbc:mysql://localhost:3306/bbs"
maxActive="100"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"/>
然后就是在WEB项目的WEB-INF中的web.xml添加
Xml代码
<resource-ref>
<description>DB
Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type[b]>
[/b]
<res-auth>Container</res-auth[b]>
[/b]
</resource-ref[b]>
[/b]
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
就是这些了,如果有什么不太清楚的就留言,一起研究下。等以后我在搜集下资料整理出上面用到的XML文件中各个标签的属性及其代表的意思。有兴趣的也可以自己先查下。:-)
JNDI 查找名称 关联的引用
java:comp/env 应用程序环境条目
java:comp/env/jdbc JDBC 数据源资源管理器连接工厂
java:comp/env/ejb EJB 引用
java:comp/UserTransaction UserTransaction 引用
java:comp/env/mail JavaMail 会话连接工厂
java:comp/env/url URL 连接工厂
java:comp/env/jms JMS 连接工厂和目标
java:comp/ORB 应用程序组件之间共享的 ORB 实例
————————————————————————————以上内容是转载的————————————————————————————————
————————————————————————————以下是我自己做的————————————————————————————————
1、配置tomcat中的context.xml文件(tomcat目录\conf\context.xml)
<?xml version='1.0'encoding='utf-8'?>
<Context>
<!--
name
: 指定Resource的JNDI名字
type
: 指定Resource所属的java类名
auth
: 指定管理Resource的Manager,Containei表示由容器来管理Resource
maxActice
: 处于活动状态的数据库连接的最大数目
maxIdle
: 处于空闲状态的数据库连接的最大数目
maxWait
: 指定数据库连接池中处于空闲状态连接的最长时间,以毫秒为单位
username
: 连接数据库的用户名
password
: 连接数据库的密码
driverClassName
: 连接数据的JDBC驱动程序
url
: 连接数据库的URL
-->
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="连接数据库的用户名"
password="连接数据库的密码"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://服务器地址:端口号默认是3306/数据库名?useUnicode=true&characterEncoding=UTF-8" />
<ResourceLink global="jdbc/mysql"name="jdbc/mysql"
type="javax.sql.DataSource"/>
<!-- Default set of monitoredresources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
2、将mysql-connector-java-5.0.8-bin.jar
复制到 tomcat目录\lib
目录中
3、在javabean中获取数据库连接池
DBConnection.java
privatestatic Connection
conn=null;
public
void newGetCon(){
try{
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn=ds.getConnection();
}catch(Exception ex){
ex.printStackTrace();
}
}
相关文章推荐
- tomcat7.0中外置的Context配置
- Tomcat配置篇---context.xml
- tomcat Server.xml Context配置
- 如何理解tomcat的server.xml中host、context的标签配置
- grails配置tomcat-->server.xml Context>path>docBase
- Tomcat中server.xml配置及Context的配置直接引用工程
- Tomcat中Context的配置
- 在tomcat下context.xml中配置各种数据库连接池
- tomcat配置文件分析之context.xml
- Tomcat配置——context
- Tomcat中server.xml配置及Context的配置直接引用工程
- Tomcat Context配置元素Loader属性介绍
- Tomcat Server.xml配置了Context后访问url不起作用
- tomcat中我们来配置我们的contextPath
- Tomcat中 server.xml 的Context的配置
- 试用 Tomcat7.x 与 Tomcat6.x 的明显不同 + Context 填写方法 + 默认应用配置方法 .
- tomcat 的server 中context的配置
- Tomcat中Context的配置
- 在tomcat下context.xml中配置各种数据库连接池
- Tomcat 配置Context