第一个Servlet 如何开发一个Servlet Servlet的映射路径 Servlet缺省路径
如何开发一个Servlet
步骤:
- 编写java类,继承HttpServlet类
- 重新doGet和doPost方法
- Servlet程序交给tomcat服务器运行!!
3.1 servlet程序的class码拷贝到WEB-INF/classes目录
3.2 在web.xml文件中进行配置
编写java类
package day0226;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyFirstServlet extends HttpServlet {
@Override
//覆盖doGet
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
//向浏览器输出内容
resp.getWriter().write("This is My First Servlet");
//resp.getWriter().println("This is My First Servlet");
}
}
配置servlet
问题:访问URL: http://localhost:8080/day0226/first
前提: tomcat服务器启动时,首先加载webapps中的每个web应用的web.xml配置文件。
http://: http协议
localhost: 到本地的hosts文件中查找是否存在该域名对应的IP地址 127.0.0.1
8080: 找到tomcat服务器
/day0226 : 在tomcat的webapps目录下找 day0226的目录
1)在day0226的web.xml中查找是否有匹配的url-pattern的内容(/first)
2)如果找到匹配的url-pattern,则使用当前servlet-name的名称到web.xml文件中查询是否相同名称的servlet配置
3)如果找到,则取出对应的servlet配置信息中的servlet-class内容: 字符串: day0226.MyFirstServlet
通过反射:
1)构造MyFirstServlet的对象
2)然后调用MyFirstServlet里面的方法
web.xml
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>day0226</display-name>
<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>
<!-- 配置一个servlet -->
<!-- servlet的配置 -->
<servlet>
<!-- servlet的内部名称,自定义,尽量有意义 -->
<servlet-name>MyFirstServlet</servlet-name>
<!-- servlet的类全名:包名+简单类名 *.java-Copy Qualifild Name -->
<servlet-class>day0226.MyFirstServlet</servlet-class>
</servlet>
<!-- servler的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,和上面一致!! -->
<servlet-name>MyFirstServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>
<url-pattern>/*</url-pattern>
Servlet的映射路径
<!-- servler的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,和上面一致!! -->
<servlet-name>MyFirstServlet</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/first</url-pattern>
</servlet-mapping>
url-pattern | 浏览器输入 | |
---|---|---|
精确匹配 | /first | http://localhost:8080/day0226/firs |
精确匹配 | /day/demo1 | http://localhost:8080/day0226/day/demo1 |
– | – | – |
模糊匹配 | /* | http://localhost:8080/day0226/任意路径 |
模糊匹配 | /*/demo1 | http://localhost:8080/day0226/day/任意路径 |
模糊匹配 | /*.后缀名 | |
模糊匹配 | /*.do | http://localhost:8080/day0226/任意路径.do |
模糊匹配 | /*.action | |
模糊匹配 | /*.html(伪静态) |
注意:
1)url-pattern要么以 / 开头,要么以* 开头。 例如, itcast是非法路径。
2)不能同时使用两种模糊匹配,例如 /day/ * .do是非法路径
3)当有输入的URL有多个servlet同时被匹配的情况下:
3.1 精确匹配优先。(长的最像优先被匹配)
3.2 以后缀名结尾的模糊url-pattern优先级最低!!!
mappingDemo1.java
package day0226;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class mappingDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
System.out.println("Demo1.doGet()");
}
}
web.xml
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>day0226</display-name>
<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>
<!-- 配置一个servlet -->
<!-- servlet的配置 -->
<servlet>
<!-- servlet的内部名称,自定义,尽量有意义 -->
<servlet-name>mappingDemo1</servlet-name>
<!-- servlet的类全名:包名+简单类名 *.java-Copy Qualifild Name -->
<servlet-class>day0226.mappingDemo1</servlet-class>
</servlet>
<!-- servler的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,和上面一致!! -->
<servlet-name>mappingDemo1</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>
mappingDemo2.java
package day0226;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class mappingDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
System.out.println("Demo2.doGet()");
}
}
web.xml
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>day0226</display-name>
<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>
<!-- 配置一个servlet -->
<!-- servlet的配置 -->
<servlet>
<!-- servlet的内部名称,自定义,尽量有意义 -->
<servlet-name>mappingDemo</servlet-name>
<!-- servlet的类全名:包名+简单类名 *.java-Copy Qualifild Name -->
<servlet-class>day0226.mappingDemo2</servlet-class>
</servlet>
<!-- servler的映射配置 -->
<servlet-mapping>
<!-- servlet的内部名称,和上面一致!! -->
<servlet-name>mappingDemo</servlet-name>
<!-- servlet的映射路径(访问servlet的名称) -->
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>
servlet缺省路径
servlet的缺省路径(/)是在tomcat服务器内置的一个路径。该路径对应的是一个DefaultServlet(缺省Servlet)。这个缺省的Servlet的作用是用于解析web应用的静态资源文件。
问题: URL输入http://localhost:8080/day0226/index.html 如何读取文件????
1)到当前day10应用下的web.xml文件查找是否有匹配的url-pattern。
2)如果没有匹配的url-pattern,则交给tomcat的内置的DefaultServlet处理
3)DefaultServlet程序到day10应用的根目录下查找是存在一个名称为index.html的静态文件。
4)如果找到该文件,则读取该文件内容,返回给浏览器。
5)如果找不到该文件,则返回404错误页面。
结论: 先找动态资源,再找静态资源。