The Context element represents a web application, which is run within a particular virtual host. Each web application is based on a Web Application Archive (WAR) file, or a corresponding directory containing the corresponding unpacked contents, as described in the Servlet Specification (version 2.2 or later).
从上述描述看出,Context元素代表的是一个web application。
You may define as many Context elements as you wish. Each such Context MUST have a unique context path, which is defined by the path
attribute. In addition, you MUST define a Context with a context path equal to a zero-length string. This Context becomes the default web application for this virtual host, and is used to process all requests that do not match any other Context's context path.
从上面的描述可以看出Context的数量自己定,但是必须定义一个path属性为空的Context,用来出来匹配不上其它path的用户请求。
In addition to nesting Context elements inside a Host element, you can also store them:
- in the individual
$CATALINA_HOME/conf/context.xml
file: the Context element information will be loaded by all webapps
- in the individual
$CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default
file: the Context element information will be loaded by all webapps of that host
- in individual files (with a ".xml" extension) in the
$CATALINA_HOME/conf/[enginename]/[hostname]/
directory
-
if the previous file was not found for this application, in individual file at
/META-INF/context.xml
inside the application files
以上的文字,是我写这篇文档的根源。
Context element的放置位置:有5个。
1、conf/server.xml的host元素之下。
2、conf/context.xml
中,被所有的应用共享。
3、conf/[enginename]/[hostname]/context.xml中,被所有的应用共享。
4、conf/[enginename]/[hostname]/
directory.xml中,就是以目录名作为文件名的配置文件。
5、/META-INF/context.xml 在应用的目录中。
其中:1、2、3都是全局共享的。
4、5都是针对独立应用的。
最后一种5生效的唯一理由是前面的4的文件找不到,如果有4,则5的配置将会完全被屏蔽掉。
This method allows dynamic reconfiguration of the web application, since the main conf/server.xml
file cannot be reloaded without restarting Tomcat. Please note that for tomcat 5, unlike tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. Instead, put them in the META-INF/context.xml directory of your WAR file or the conf directory as described above.
以上的描述可以发现,conf/server.xml file cannot be reloaded without restarting Tomcat。
反之如果配置到其他地方却可以自动reload。自动重载意味着什么我就不多说了。
为了联系配置JNDI,我安装了mysql 5.0.18
在其中建立了一个测试的库,和一张表,插入了一点儿数据。
JNDI的使用,分为两种:全局和局部的。
全局的:
conf/server.xml中配置(注意,配置Resource的方法在Tomcat的以前版本中不是这样的,需要按照这个简化语法来改,否则就和我一样折腾半死也不明白为什么了。)
<
GlobalNamingResources
>
...
<
Resource
name
="student"
type
="javax.sql.DataSource"
driverClassName
="com.mysql.jdbc.Driver"
password
="1"
maxIdle
="2"
maxWait
="5000"
username
="root"
url
="jdbc:mysql://localhost:3306/schoolproject"
maxActive
="4"
/>
...
</
GlobalNamingResources
>
配置完成之后,就是使用它了。
有两个地方,根据前文描述,选择一个。
建立META-INF/context.xml(使用方法5,在局部配置时掩饰使用方法4)
其中的name是程序中引用,其中的global是在全局Resource中定义的name。
写个测试的jsp
index.jsp
<%
@ page language
=
"
java
"
import
=
"
javax.sql.*,java.sql.*,javax.naming.*
"
contentType
=
"
text/html;charset=UTF-8
"
%>
<
html
>
<
head
>
<
title
>
test
</
title
>
<
head
>
<
body
>
Hello olojiang
!<
br
/>
<%
Context initCtx
=
new
InitialContext();
DataSource ds
=
(DataSource)initCtx.lookup(
"
java:comp/env/student
"
);
if
(ds
!=
null
)
...
{
Connection conn
=
ds.getConnection();
Statement st
=
conn.createStatement();
ResultSet rs
=
st.executeQuery(
"
select * from student
"
);
while
(rs.next())
...
{
out.print(rs.getString(
1
)
+
"
"
);
out.print(rs.getString(
2
)
+
"
"
);
out.print(rs.getString(
3
)
+
"
"
);
out.println(rs.getInt(
4
)
+
"
<br/>
"
);
}
st.close();
conn.close();
}
%>
</
body
>
</
html
>
完美输出结果。
局部的:
conf\Catalina\localhost\DBTest.xml(使用方法4,前面演示了使用方法5)
在其中书写可以有动态重载的优势,DBTest时目录名称。
<
Context
path
="/DBTest"
docBase
="DBTest"
debug
="5"
reloadable
="true"
crossContext
="true"
>
<
Resource
name
="jdbc/TestDB"
type
="javax.sql.DataSource"
driverClassName
="com.mysql.jdbc.Driver"
password
="1"
maxIdle
="2"
maxWait
="5000"
username
="root"
url
="jdbc:mysql://localhost:3306/schoolproject"
maxActive
="4"
/>
</
Context
>
配置好了之后,比前面多处一步:需要在应用的WEB-INF/web.xml中写引用。(使用全局的配置不需要作这一部也能成功,这里不作这步我也好像成功了,但是很多地方都写需要这步,不知为何?)
<?
xml version="1.0" encoding="UTF-8"
?>
<
web-app
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version
="2.4"
>
<
description
>
MySQL Test App
</
description
>
<
resource-ref
>
<
description
>
DB Connection
</
description
>
<
res-ref-name
>
jdbc/TestDB
</
res-ref-name
>
<
res-type
>
javax.sql.DataSource
</
res-type
>
<
res-auth
>
Container
</
res-auth
>
</
resource-ref
>
</
web-app
>
使用同样的JSP测试之:
<%
@ page language
=
"
java
"
import
=
"
javax.sql.*,java.sql.*,javax.naming.*
"
contentType
=
"
text/html;charset=UTF-8
"
%>
<
html
>
<
head
>
<
title
>
test
</
title
>
<
head
>
<
body
>
Hello olojiang
!<
br
/>
<%
Context initCtx
=
new
InitialContext();
DataSource ds
=
(DataSource)initCtx.lookup(
"
java:comp/env/jdbc/TestDB
"
);
if
(ds
!=
null
)
...
{
Connection conn
=
ds.getConnection();
Statement st
=
conn.createStatement();
ResultSet rs
=
st.executeQuery(
"
select * from student
"
);
while
(rs.next())
...
{
out.print(rs.getString(
1
)
+
"
"
);
out.print(rs.getString(
2
)
+
"
"
);
out.print(rs.getString(
3
)
+
"
"
);
out.println(rs.getInt(
4
)
+
"
<br/>
"
);
}
st.close();
conn.close();
}
%>
</
body
>
</
html
>
运行完美。
两种方法都说了,自己走了很多弯路,配这么点儿东西折腾了一整天,忘后来者能顺利些。
不足处请多包涵。
开心ing!~