1,基本配置
<!-- Servlet类的配置 --><servlet><servlet-name>sq</servlet-name><servlet-class>beyond.servlet.QuickStartServlet</servlet-class></servlet><!-- Servlet的虚拟路径的配置 --> <servlet-mapping><servlet-name>sq</servlet-name><url-pattern>/beyond</url-pattern> <!-- 最后访问的就是这个 --></servlet-mapping>
其中url-pattern的配置方式
1)完全匹配
访问的资源与配置的资源完全相同才能访问的到
<url-pattern>/beyond</url-pattern>
2)目录匹配
/虚拟的目录…/*
<url-pattern>/wsq/qb/yy/*</url-pattern> //只要是访问的是wsq/qb/yy下的任何资源都可以访问到
3)扩展名匹配
*.扩展名
<url-pattern>*.wsq</url-pattern>
注意:第二种跟第三种不要混用
例如:/wsq/qb/yy/*.wsq 这是错误的
2,服务器启动实例化Servlet配置
Servlet默认第一次访问的时候创建
当servlet的配置时,加上一个配置<load-on-startup>3</load-on-startup>
的时候,对象在服务器启动时就创建,数字(正整数)代表优先级,越小优先级越高。
3,缺省Servlet
可以将url-pattern配置一个/,<url-pattern>/</url-pattern>
,代表该servlet 是缺省的servlet(当你访问资源地址所以的servlet都不匹配时,缺省的servlet负责处理),其实,web应用中所有的资源响应都是servlet负责的,包括静态资源
4,欢迎界面
在WEB13这个项目工程下面的WebContent下创建一个index.html
,当地址访问http://localhost:8080/WEB13/
,只访问到项目名称的时候,就会自动访问index.html
该页面。
优先级如下所示:
<welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
其实当把项目工程放到TomCat上,访问的地址为:http://localhost:8080/WEB13/wsq
这里的WEB13是项目的文件名;
这里的wsq是WEB13 ----->WebContent ------>WEB-INF ----->web.xml -----><url-pattern>/wsq</url-pattern>
这个路径名称,它找<servlet-name>sq</servlet-name>
,
然后再找
<servlet-class>beyond.servlet.QuickStartServlet</servlet-class>
最后找到该包。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>WEB13</display-name><!-- Servlet类的配置 --><servlet><servlet-name>sq</servlet-name><servlet-class>beyond.servlet.QuickStartServlet</servlet-class><init-param><param-name>url</param-name><param-value>beyondsq</param-value></init-param><load-on-startup>3</load-on-startup></servlet><!-- Servlet的虚拟路径的配置 --> <servlet-mapping><servlet-name>sq</servlet-name><url-pattern>/wsq</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>