本文主要解释服务器在收到客户端的请求后在服务器内部的执行过程。
客户端通过URL地址访问服务器中的资源,所以Servlet程序要是想被外界访问,必须把servlet映射到一个URL地址上,这个工作在web.xml文件中使用元素和元素完成。
元素用于注册Servlet
它包含两个主要的子元素:
设置Servlet的注册名称
:设置Servlet的完整类名。
元素用于映射一个已注册的Servlet的一个对外访问路径
它含有两个子元素:
:指定Servlet的注册名
:Servlet的对外访问路径。
index.html用来设置第一页面。
<welcome-file-list>
<welcome-file>index.html</welcome-file> <!-- 设置访问首页,index.html可以改成你要进入的网站首页-->
<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>
<servlet>
<description></description>
<display-name>StudentListServlet</display-name>
<servlet-name>StudentListServlet</servlet-name>
<servlet-class>com.it.servlet.StudentListServlet</servlet-class> <!-- 本类在工程中完整的包名 -->
</servlet>
<servlet-mapping>
<servlet-name>StudentListServlet</servlet-name>
<url-pattern>/StudentListServlet</url-pattern>
</servlet-mapping>
当浏览器发出访问服务器资源的请求时,用户主要通过一下步骤来访问服务器的资源。
上述就是Servlet内部执行流程,主要是通过servlet容器对url进行匹配。
注意: url-pattern属性内容可以自定义 两个子元素中的servlet-name属性必须一致,否则报错 获取工程中完整的包名,可右键该servlet的java文件,选择copy qualified name然后粘贴即可。 在开发时,新建servlet的同时,软件就已经在web.xml文件中自动注册,所以在开发中尽量不要动web.xml文件,否则可能因误操作出异常。
精确匹配:/first
/first.index.html
模糊匹配:
/* http://localhost:8080/myshop/任意路径
/test/* http://localhost:8080/myshop/test/任意路径
.后缀名 http://localhost:8080/myshop/任意路径后缀名