• JavaWeb基础之Servlet


    一、目录

    • 什么是Servlet
    • 怎么实现一个Servlet程序
    • Servlet生命周期
    • HttpServlet类实现Servlet程序
    • ServletConfig类
    • ServletContext类

    二、什么是Servlet

    1. Servlet是JavaEE规则之一。规则就是接口。
    2. Servlet是JavaWeb三大组件之一。三大组件分别是:Servlet程序、Filter过滤器、Listener监听器。
    3. Servlet是运行在服务器上的一个java小程序。它可以接收客户端发送来的请求,并响应数据给客户端

    Servlet程序从2.5版本是现在市面使用最多的版本。
    Servlet2.5是xml配置,Servlet3.0之后就是注解版本的Servlet使用。

    三、怎么实现一个Servlet程序

    1. 编写一个类去实现Servlet接口。
    2. 实现service方法,处理请求,并响应数据。
    3. 到web.xml中去配置servlet程序的访问地址。

    Servlet程序会自动调用service方法。

    web.xml中关于servlet的配置如下:

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        
        <servlet>
            
            <servlet-name>FirstServletservlet-name>
            
            <servlet-class>com.example.javaservlet.FirstServletservlet-class>
        servlet>
    
        
        <servlet-mapping>
            
            <servlet-name>FirstServletservlet-name>
            
            
            
            <url-pattern>/hellourl-pattern>
        servlet-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    四、Servlet的生命周期

    1. 执行Servlet构造器方法。
    2. 执行init初始化方法。
    3. 执行service方法。
    4. 执行destroy销毁方法。

    1、2两个方法是在第一次访问的时候,创建Servlet程序会调用,也就是只调用一次,service方法每次访问都会调用,destroy方法在web工程停止的时候调用。

    package com.example.javaservlet;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class FirstServlet implements Servlet {
       
        public FirstServlet() {
       
            System.out.println("1. This is construct");
        }
    
        @Override
        public void init(ServletConfig servletConfig) throws ServletException {
       
            System.out.println("2. This is init");
        }
    
        @Override
        public ServletConfig getServletConfig() {
       
            return null;
        }
    
        @Override
        public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    从 IPv4 向 IPv6 的迁移
    CSS中z-index不生效的原因和解决办法
    ArcGIS软件制作双变量等值区域地图(Bivariate Choropleth Maps)
    Vue源码之数据响应式
    CSS 滚动驱动动画 scroll-timeline ( scroll-timeline-name ❤️ scroll-timeline-axis )
    OPC学习笔记一
    语法糖
    【C++学习第二讲】开始学习C++
    sql注入基本概念
    Qt学习总结之QMessageBox
  • 原文地址:https://blog.csdn.net/qq_37466661/article/details/126576045