servlet的请求路径配置笔记
示例前提:项目名默认请求路径为http://localhost:8080/s1/
默认跳转页面为:
项目有两个servlet:一个名为HelloServlet,一个名为Error
知识点:(在web.xml下配置servlet的映射路径)
- 一个servlet可以指定一个映射路径
运行tomcat,打开浏览器输入:localhost:8080/s1/hello
浏览器输入:localhost:8080/s1/error
- 一个servlet可以指定多个映射路径
示例:HelloServlet指定映射路径为/hello、/hello1、/hello2
重启tomcat,浏览器分别输入:localhost:8080/s1/hello1和localhost:8080/s1/hello2,显示的都为HelloServlet的内容
- 一个servlet可以指定通用路径(’*'为通配符,即任何字符都匹配)
示例:HelloServlet映射路径为/hello/*
重启tomcat,浏览器输入:localhost:8080/s1/hello或localhost:8080/s1/hello/ASFD,都指向的是HelloServlet的内容
示例:HelloServlet映射路径为/*
重启tomcat,自动弹出页面:localhost:8080/s1/,默认页面会指向的是HelloServlet的内容
(/*表明优先级大于默认路径,要谨慎使用) - servlet也可以指定后缀 或前缀路径:(“*.ayi”为后缀.ayi的字符串都可以匹配,如asdf.ayi或.ayi都可以匹配)
示例:HelloServlet映射路径为*.ayi
(后缀路径不可以加‘/’,加了会报错)
重启tomcat,浏览器输入:http://localhost:8080/s1/asdf.ayi和http://localhost:8080/s1/.ayi,指向的是HelloServlet的内容。
输入http://localhost:8080/s1/helfe寻找不到该页面
- 路径的优先级问题:越精确的路径优先级越高
示例:HelloServlet映射路径为/*, Error映射路径为/error
重启tomcat,浏览器输入:http://localhost:8080/s1/显示的是HelloServlet的内容
输入http://localhost:8080/s1/error,指向的是Error的内容。
(表明路径为“/error”的优先级高于“/*”)