java—有人能帮助我理解如何在tomcat中使用上下文文件并从程序中调用它吗?

wfsdck30  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(346)

我正在使用eclipse用java编写一个web应用程序,使用struts2框架连接到与mysql数据库通信的tomcat7服务器。我有程序工作,但我有我的代码硬编码的连接值。我想使用context.xml或我创建的context.xml来连接,这样它就可以移植并且不需要登录页面来连接。我没有完全遵循上下文配置。我创建了一个名为os1.xml的上下文xml文件,并将其放在c:\users\rich\tomcat\conf\catalina\localhost中。该应用程序从webapps目录c:\users\rich\tomcat\webapps启动,其中包含os1.war文件。
有人能帮助我理解我的上下文文件是如何在tomcat中使用的,以及如何从我的程序中调用的吗?
这是我的connectionmodel类中应该调用它的部分。

public class ConnectionModel {  

public ArrayList<Table> Systems() throws SQLException                       {               
    Connection con = null;          
    ResultSet rs = null;   
    try {
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/os");

        con = ds.getConnection();

这是我的web.xml文件

<display-name>OS1</display-name>

<welcome-file-list>
    <welcome-file>enter.jsp</welcome-file>      
</welcome-file-list>

<filter>
    <filter-name>struts2</filter-name>
     <filter-class>
                  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
             </filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>   

<resource-ref>
    <description>MySQL Datasource </description>
    <res-ref-name>jdbc/os</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

这是我的上下文xml文件,名为os1.xml,位于
c:\users\rich\tomcat\conf\catalina\localhost

<Context docBase="C:/Users/Rich/Tomcat/webapps/OS1.war" path="/OS1" reloadable="true">
<Resource>
        name="jdbc/os"
            auth="Container"
            type="javax.sql.DataSource"
            maxActive="100"
            maxIdle="30"
            maxWait="10000"
            username="someone"
            password="password"
            driverClassName="com.mysql.jdbc.Driver"                                 

    url="jdbc:mysql://localhost:3306/os"
</Resource>
</Context>

我得到下面的错误。

WARNING: A docBase C:\Users\Rich\Tomcat\webapps\OS1.war inside the host appBase has
been specified, and will be ignored

Jun 05, 2013 12:43:54 PM org.apache.catalina.deploy.NamingResources initInternal
WARNING: Failed to create MBean for naming resource [null]
java.lang.NullPointerException
at javax.management.ObjectName.quote(ObjectName.java:1833)
at org.apache.catalina.mbeans.MBeanUtils.createObjectName(MBeanUtils.java:569)
at org.apache.catalina.mbeans.MBeanUtils.createMBean(MBeanUtils.java:183)
at 
org.apache.catalina.deploy.NamingResources.initInternal(NamingResources.java:934)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
at 
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5163)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
at 
org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1636)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

Jun 05, 2013 12:43:54 PM org.apache.catalina.core.ContainerBase addChildInternal
SEVERE: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component 
 [StandardEngine[Catalina].StandardHost

[localhost].StandardContext[/OS1]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
at 
org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1636)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:199)
at java.util.StringTokenizer.<init>(StringTokenizer.java:221)
at
org.apache.catalina.core.NamingContextListener.createSubcontexts(NamingContextListener.
java:1272)
at 
org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.
java:1083)
at

org.apache.catalina.core.NamingContextListener.createNamingContext(
NamingContextListener.java:672)
at org.apache.catalina.core.NamingContextListener.lifecycleEvent(
NamingContextListener.java:271)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(
LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(
StandardContext.java:5269)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 11 more

Jun 05, 2013 12:43:54 PM org.apache.catalina.startup.HostConfig deployDescriptor
SEVERE: Error deploying configuration descriptor
    C:\Users\Rich\Tomcat\conf\Catalina\localhost\OS1.xml
java.lang.IllegalStateException: ContainerBase.addChild: start:
 org.apache.catalina.LifecycleException: Failed to 

start component
[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/OS1]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
at    org.apache.catalina.startup.HostConfig$DeployDescriptor.run(
HostConfig.java:1636)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

然后我得到这个错误,我假设是因为我的上下文文件没有被正确使用:
无法为连接url“null”创建类“”的jdbc驱动程序
感谢您的帮助!
提前谢谢,里奇

relj7zay

relj7zay1#

上下文xml文件应如下所示:

<Context docBase="C:/Users/Rich/Tomcat/webapps/OS1.war" path="/OS1" reloadable="true">
<Resource
        name="jdbc/os"
        auth="Container"
        type="javax.sql.DataSource"
        maxActive="100"
        maxIdle="30"
        maxWait="10000"
        username="someone"
        password="password"
        driverClassName="com.mysql.jdbc.Driver"                                 
    url="jdbc:mysql://localhost:3306/os"
/>

使用资源节点的参数而不是资源正文文本。我对没有参数的资源也有类似的错误

java.lang.NullPointerException
    at javax.management.ObjectName.quote(ObjectName.java:1833)

看起来catalina没有强制的字段验证。

相关问题