小编典典

context.xml的说明

tomcat

使用Tomcat时,我一直都被web.xml 视为一种 .htaccess orhttpd.conf 同等的东西。很自然,可能必须有某种配置Web服务器的方法。

但是,我不太了解的目的 context.xml. 例如,
使用JDBC的时候,为什么我有一个增加 resource-ref in web.xml and
also a Resource with even more info in context.xml? Could I eliminate the
context.xml 文件并以某种方式实例化 DataSource 代码吗?我
问是因为这样的假设示例可以帮助我理解。

编辑:我试图了解在这样的配置中正在发生什么/META-INF/context.xml:

<Context>
<Resource name="jdbc/postgres" auth="Container" type="javax.sql.DataSource"
    driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432"
    username="postgres" password="yes" maxActive="20" maxIdle="10"
    maxWait="-1" />
</Context>

and, in WEB-INF/web.xml:

<resource-ref>
    <description>postgreSQL Datasource example</description>
    <res-ref-name>jdbc/postgres</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

为什么必须将两者都放在其中才能使用JDBC?他们到底在做什么?除了Java代码,还有另一种方法可以做同样的事情吗?就像我说的,不是因为我想要,而是因为我想了解他们在做的更好。


阅读 269

收藏
2020-06-16

共1个答案

小编典典

I don’t quite understand the purpose of context.xml

context.xml is called from the web.xml through <context-param> tag. As
you web.xml loads first when an application is created and it will create the
references for thecontexts that are configured in it
.

I don’t quite understand the purpose of context.xml

So, the purpose of context.xml is adding separation of codes. you can
have separate contexts for different purposes . for example For Database
connectivity, using other frameworks etc..,

Could I eliminate the context.xml file and somehow instantiate the
DataSource in my code?

Yes ,you can do that by configuring the datasource in web.xml itself.

Hope this helps !!

2020-06-16