简体   繁体   中英

Can someone help me understand how my context file is utilized within Tomcat and called from my program?

I am using Eclipse to write a web application program in Java using the Struts2 framework connecting to a Tomcat 7 server that communicates to a MySQL database. I have the program working, but I have the values for the connection hard coded in my code. I want to use the context.xml or one I create to connect so it can be portable and not need a sign in page for the connection. I am not completely following the context configuration. I have created a context xml file called OS1.xml and put it in C:\\Users\\Rich\\Tomcat\\conf\\Catalina\\localhost. The app is launched from the webapps directory C:\\Users\\Rich\\Tomcat\\webapps which has the OS1.war file in it.

Can someone help me understand how my context file is utilized within Tomcat and called from my program?

This is the portion of my ConnectionModel class where this should be called.

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();           

This is my web.xml file

<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> 

This is my Context xml file called OS1.xml and is locate in
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>

I am getting the errors below.

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)

Then I get this error I assume because my context file is not being used correctly:

Cannot create JDBC driver of class '' for connect URL 'null'

Any help is appreciated!

Thanks in advance, Rich

Your context xml file should look like:

<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"
/>

with parameters of Resource node instead of Resource body text. I had similar error with Resource without parameters

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

Looks like Catalina doesn't have mandatory field validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM