Servlet体系结构和Servlet_urlpartten配置

Servlet:

  • 概念

  • 步骤

  • 执行原理

  • 生命周期

  • Servlet3.0 注解配置

  • Servlet的体系结构

    • Servlet -- 接口
      |
    • GenericServlet -- 抽象类
      |
    • HttpServlet -- 抽象类
  • GenericServlet:将Servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象

    • 将来定义Servlet类时,可以继承GenericServlet,实现service()方法即可
package com.ailyt.servlet;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet(value = "/demo2")
public class ServletStudy4 extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("demo2....");
    }
}

  • HttpServlet:对http协议的一种封装,简化操作

    • 定义类继承HttpServlet
    • 复写doGet/doPost方法
  • Servlet相关配置

    • urlpartten:Servlet访问路径
      • 一个Servlet可以定义多个访问路径:@WebServlet({"/d4","/dd4","/ddd4"})
      • 路径定义规则:
        • /xxx
        • /xxx/xxx:多层路径,目录结构
        • *.do
package com.ailyt.servlet;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet路径配置
 */
//@WebServlet({"/d4","/dd4","/ddd4"})
//@WebServlet("/user/demo4")
//@WebServlet("/user/*")
//@WebServlet("/*")
//@WebServlet("*.do")
public class ServletStudy6 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet");
    }
}

posted @ 2022-08-09 09:36  我滴妈老弟  阅读(19)  评论(0编辑  收藏  举报