Tomcat简介
一、tomcat背景
自从jsp发布之后,推出了各式各样的jsp引擎。apache group在完成gnujsp1.0的开发以后,开始考虑在sun的jswdk基础上开发一个可以直接提供web服务的jsp服务器,当然同时也支持servlet, 这样tomcat就诞生了。tomcat是jakarta项目中的一个重要的子项目,其被javaworld杂志的编辑选为2001年度最具创新的java产品,同时它又是sun公司官方推荐的servlet和jsp容器,因此其越来越多的受到软件公司和开发人员的喜爱。servlet和jsp的最新规范都可以在tomcat的新版本中得到实现。其次,tomcat是完全免费的软件,任何人都可以从互联网上自由地下载。tomcat与apache的组合相当完美。
二、tomcat目录
tomcat
|---bin tomcat:存放启动和关闭tomcat脚本;
|---conf tomcat:存放不同的配置文件(server.xml和web.xml);
|---doc:存放tomcat文档;
|---lib/japser/common:存放tomcat运行需要的库文件(jars);
|---logs:存放tomcat执行时的log文件;
|---src:存放tomcat的源代码;
|---webapps:tomcat的主要web发布目录(包括应用程序示例);
|---work:存放jsp编译后产生的class文件;
三、tomcat类加载
bootstrap($java_home/jre/lib/ext/*.jar)
system($classpath/*.class和指定的jar)
common($catalina_home/common 下的classes,lib,endores三个子目录)
catalina ($catalina_home/server/下的classes和lib目录仅对tomcat可见)
&shared($catalina_home/shared/下的classes和lib目录以及$catalina_home/lib目录)仅对web应用程序可见,对tomcat不可见webapp($webapp/web-inf/*仅对该web应用可见classes/*.class lib/*.jar)
加载类和资源的顺序为:
1、/web-inf/classes
2、/web-inf/lib/*.jar
3、bootstrap
4、system
5、$catalina_home/common/classes
6、$catalina_home/common/endores/*.jar
7、$catalina_home/common/lib/*.jar
8、$catalina_home/shared/classes
9、$catalina_home/shared/lib/*.jar
四、server.xml配置简介:
下面讲述这个文件中的基本配置信息,更具体的配置信息请参考tomcat的文档:
server:
1、port 指定一个端口,这个端口负责监听关闭tomcat的请求
2、shutdown 指定向端口发送的命令字符串
service:
1、name 指定service的名字
connector (表示客户端和service之间的连接):
1、port 指定服务器端要创建的端口号,并在这个断口监听来自客户端的请求
2、minprocessors 服务器启动时创建的处理请求的线程数
3、maxprocessors 最大可以创建的处理请求的线程数
4、enablelookups 如果为true,则可以通过调用request.getremotehost()进行dns查
询来得到远程客户端的实际主机名,若为false则不进行dns查询,而是返回其ip
地址
5、redirectport 指定服务器正在处理http请求时收到了一个ssl传输请求后重定向的
端口号
6、acceptcount 指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理
队列中的请求数,超过这个数的请求将不予处理
7、connectiontimeout 指定超时的时间数(以毫秒为单位)
engine (表示指定service中的请求处理机,接收和处理来自connector的请求):
1、defaulthost 指定缺省的处理请求的主机名,它至少与其中的一个host元素的name
属性值是一样的
context (表示一个web应用程序):
1、docbase 应用程序的路径或者是war文件存放的路径
2、path 表示此web应用程序的url的前缀,这样请求的url为
http://localhost:8080/path/****
3、reloadable 这个属性非常重要,如果为true,则tomcat会自动检测应用程序的
/web-inf/lib 和/web-inf/classes目录的变化,自动装载新的应用程序,我们可
以在不重起tomcat的情况下改变应用程序
host (表示一个虚拟主机):
1、name 指定主机名
2、appbase 应用程序基本目录,即存放应用程序的目录
3、unpackwars 如果为true,则tomcat会自动将war文件解压,否则不解压,直接
从war文件中运行应用程序
logger (表示日志,调试和错误信息):
1、classname 指定logger使用的类名,此类必须实现org.apache.catalina.logger 接口
2、prefix 指定log文件的前缀
3、suffix 指定log文件的后缀
4、timestamp 如果为true,则log文件名中要加入时间,如下
例:localhost_log.2001-10-04.txt
realm (表示存放用户名,密码及role的数据库):
1、classname 指定realm使用的类名,此类必须实现org.apache.catalina.realm接口
valve (功能与logger差不多,其prefix和suffix属性解释和logger 中的一样):
1、classname 指定valve使用的类名,如用org.apache.catalina.valves.accesslogvalve
类可以记录应用程序的访问信息
directory(指定log文件存放的位置):
1、pattern 有两个值,common方式记录远程主机名或ip地址,用户名,日期,第一行
请求的字符串,http响应代码,发送的字节数。combined方式比common方式记
录的值更多
五、web.xml配置简介:
1、默认(欢迎)文件的设置
在tomcat4/conf/web.xml中,<welcome-file-list>与iis中的默认文件意思相同。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
2、报错文件的设置
<error-page>
<error-code>404</error-code>
<location>/notfilefound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.nullpointerexception</exception-type>
<location>/null.jsp</location>
</error-page>
如果某文件资源没有找到,服务器要报404错误,按上述配置则会调用/webapps/root/notfilefound.jsp。
如果执行的某个jsp文件产生nullpointexception ,则会调用/webapps/root/null.jsp
3、会话超时的设置
设置session 的过期时间,单位是分钟;
<session-config>
<session-timeout>30</session-timeout>
</session-config>
4、过滤器的设置
<filter>
<filter-name>filtersource</filter-name>
<filter-class>project4. filtersource </filter-class>
</filter>
<filter-mapping>
<filter-name>filtersource</filter-name>
<url-pattern>/wwwservlet</url-pattern>
(<url-pattern>/haha/*</url-pattern>)
</filter-mapping>
过滤:
1) 身份验证的过滤authentication filters
2) 日志和审核的过滤logging and auditing filters
3) 图片转化的过滤image conversion filters
4) 数据压缩的过滤data compression filters
5) 加密过滤encryption filters
6) tokenizing filters
7) 资源访问事件触发的过滤filters that trigger resource access events xsl/t 过滤xsl/t filters
9) 内容类型的过滤mime-type chain filter 注意监听器的顺序,如:先安全过滤,然后资源,
然后内容类型等,这个顺序可以自己定。
六、管理
1、用户配置
在进行具体tomcat管理之前,先给tomcat添加一个用户,使这个用户有权限来进行管理。
打开conf目录下的tomcat-users.xml文件,在相应的位置添加下面一行:
<user name="user" password="user" roles="standard,manager"/>
然后重起tomcat,在浏览器中输入http://localhost:8080/manager/,会弹出对话框,输入上面的用户
名和密码即可。
2、应用程序列表
在浏览器中输入http://localhost:8080/manager/list,浏览器将会显示如下的信息:
ok - listed applications for virtual host localhost
/ex:running:1
/examples:running:1
/webdav:running:0
/tomcat-docs:running:0
/manager:running:0
/:running:0
上面显示的信息分别为:应用程序的路径、当前状态、连接这个程序的session数
3、重新装载应用程序
在浏览器中输入 http://localhost:8080/manager/reload?path=/examples,浏览器显示如下:
ok - reloaded application at context path /examples
这表示example应用程序装载成功,如果我们将server.xml的context元素的reloadable属性设为true,则没必要利用这种方式重新装载应用程序,因为tomcat会自动装载。
4、显示session信息
在浏览器中输入http://localhost:8080/manager/sessions?path=/examples,浏览器显示如下:
ok - session information for application at context path /examples default maximum session inactive
interval 30 minutes
5、启动和关闭应用程序
在浏览器中输入http://localhost:8080/manager/start?path=/examples 和
http://localhost:8080/manager/stop?path=/examples分别启动和关闭examples应用程序。
浙公网安备 33010602011771号