第一个Servlet 如何开发一个Servlet Servlet的映射路径 Servlet缺省路径

如何开发一个Servlet

步骤:

  1. 编写java类,继承HttpServlet类
  2. 重新doGet和doPost方法
  3. 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的目录

/first 资源名称。
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>

在这里插入图片描述


在这里插入图片描述


http://localhost:8080/day0226/first

在这里插入图片描述

<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浏览器输入
精确匹配/firsthttp://localhost:8080/day0226/first
精确匹配/day/demo1http://localhost:8080/day0226/day/demo1
模糊匹配/*http://localhost:8080/day0226/任意路径
模糊匹配/*/demo1http://localhost:8080/day0226/day/任意路径
模糊匹配/*.后缀名
模糊匹配/*.dohttp://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错误页面。

		结论: 先找动态资源,再找静态资源。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值