05 Servlet_体系结构与urlpartten配置

Servlet:

1. 概念
2. 步骤
3. 执行原理
4. 生命周期
5. Servlet3.0 注解配置
6. Servlet的体系结构    
    Servlet -- 接口
        |
    GenericServlet -- 抽象类
        |
    HttpServlet  -- 抽象类

    * GenericServlet:将Servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象
        * 将来定义Servlet类时,可以继承GenericServlet,实现service()方法即可

    * HttpServlet:对http协议的一种封装,简化操作
        1. 定义类继承HttpServlet
        2. 复写doGet/doPost方法

package cn.itcast.web.servlet;

import javax.servlet.*;

import javax.servlet.annotation.WebServlet;

import java.io.IOException;

@WebServlet("/demo1")

public class ServletDemo1 implements Servlet {

@Override
public void init(ServletConfig servletConfig) throws ServletException {

}

@Override
public ServletConfig getServletConfig() {
    return null;
}

@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    System.out.println("demo1....");
}

@Override
public String getServletInfo() {
    return null;
}

@Override
public void destroy() {

}

}

package cn.itcast.web.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("/demo2")

public class ServletDemo2 extends GenericServlet {

@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    System.out.println("demo2.....");
}

}

package cn.itcast.web.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;

@WebServlet("/demo3")

public class ServletDemo3 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("doGet....");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("doPost...");
}

}

可以在web下创建login.html

7. Servlet相关配置
    1. urlpartten:Servlet访问路径
        1. 一个Servlet可以定义多个访问路径 : @WebServlet({"/d4","/dd4","/ddd4"})
        2. 路径定义规则:
            1. /xxx:路径匹配
            2. /xxx/xxx:多层路径,目录结构
            3. *.do:扩展名匹配

package cn.itcast.web.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 ServletDemo4 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("demo4...");
    System.out.println(req);
}

}

原文 

https://segmentfault.com/a/1190000020204169

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 05 Servlet_体系结构与urlpartten配置

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址