• Servlet


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

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

    package com.xt.servlet;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    
    /**
     * ClassName: helloServlet
     * Description:
     * date: 2022/6/23 12:57
     *
     * @author tongy
     * @since JDK 1.8
     */
    public class helloServlet implements Servlet {
        public helloServlet() {
            System.out.println("1 构造器方法 ");
        }
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            System.out.println("2 init方法");
        }
    
        @Override
        public ServletConfig getServletConfig() {
            return null;
        }
        //service是专门为了处理请求和响应的
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            //强制类型转换(因为它有getMethod())
            HttpServletRequest httpServletRequest= (HttpServletRequest) req;
            //获取请求的方式
            String method = httpServletRequest.getMethod();
            if (method=="POST"){
                doPost();
                System.out.println("post方式");
            }if (method=="GET"){
                doGet();
                System.out.println("GET方式");
            }
    
        }
        //做get请求的操作
        public void doGet(){
    
        }
        //做post请求的操作
        public void doPost(){
    
        }
        @Override
        public String getServletInfo() {
            return null;
        }
    
        @Override
        public void destroy() {
            System.out.println("4 destroy方法");
        }
    }
    
    
    • 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    <?xml version="1.0" encoding="UTF-8"?>
    <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标签给Tomcat配置Servlet程序-->
        <servlet>
            <!--servlet-name给servlet起一个别名(一般是类名)-->
            <servlet-name>helloServlet</servlet-name>
            <!--servlet-name是servlet程序的全类名-->
            <servlet-class>com.xt.servlet.helloServlet</servlet-class>
        </servlet>
    <!--servlet-mapping标签给servlet程序配置访问地址-->
        <servlet-mapping>
            <!--servlet-name标签的作用是告诉服务器,我当前配置的地址是给哪个servlet使用-->
            <servlet-name>helloServlet</servlet-name>
            <!--url-pattern标签配置访问地址
                / 斜杠在服务器解析时,表示地址为 http://ip:port/工程路径
                /hello 表示地址为:http://ip:port/工程路径/hello
            -->
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
        
        <servlet>
            <servlet-name>helloServlet2</servlet-name>
            <servlet-class>com.xt.servlet.helloServlet2</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>helloServlet2</servlet-name>
            <url-pattern>/hello2</url-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
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    servlet过程
    在这里插入图片描述
    servlet的生命周期
    1、执行servlet构造器方法
    2、执行init初始化方法
    第1、2步是在第一次访问的时候,创建servlet程序会调用
    3、执行service方法
    第3步,每次访问都会调用
    4、执行destroy方法
    第4步,在web工程停止的时候调用
    通过继承HttpServlet实现Servlet程序
    一般在实际项目开发中,都是使用继承HttpServlet的方式去实现Servlet程序。
    1、编写一个类去继承HttpServlet
    2、根据业务需要重写doGet()、doPost()
    3、到web.xml中配置servlet程序的访问地址

    package com.xt.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     * ClassName: helloServlet2
     * Description:
     * date: 2022/6/23 15:09
     *
     * @author tongy
     * @since JDK 1.8
     */
    public class helloServlet2 extends HttpServlet {
        /**
         * doGet() 在get请求的时候调用
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("hello servlet2的doGet()");
        }
    
        /**
         * doPost() 在post请求的时候调用
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("hello servlet2的doPost()");
        }
    }
    
    
    • 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    使用IDEA创建Servlet程序
    New->Servlet
    在这里插入图片描述
    Servlet类的继承体系:
    在这里插入图片描述
    ServletConfig类
    是Servlet程序的配置信息类
    ServletConfig类的三大作用:
    1、可以获取Servlet程序的别名,即:Servlet-name的值
    2、获取初始化参数init-param
    3、获取ServletContext对象

    public void init(ServletConfig config) throws ServletException {
            System.out.println("2 init方法");
    //        1、可以获取Servlet程序的别名,即:Servlet-name的值
            System.out.println("helloServlet的别名是:"+ config.getServletName());
    //        2、获取初始化参数init-param
            System.out.println("初始化参数username的值是"+config.getInitParameter("username"));
            System.out.println("初始化参数url的值是"+config.getInitParameter("url"));
    //        3、获取ServletContext对象
            System.out.println(config.getServletContext());
    
        }
    //注:servlet程序和servletConfig对象都是由Tomcat创建的,我们只是使用。Servlet程序默认是第一次访问的时候创建,ServletConfig是每个Servlet程序创建时,就负责创建一个ServletConfig对象。传给Init
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ServletContext类
    1、ServletConfig类其实是一个接口,它表示Servlet上下文对象
    2、一个web工程只有一个ServletConfig对象
    3、ServletContext对象是一个域对象(域对象,是可以像Map一样存取数据的对象,叫做域对象,这里的域指的是存取数据的操作范围。与、域对象也有一说是作用范围是整个工程的对象)
    4、ServletContext是在web工程部署启动的时候创建。在web工程停止时销毁。

    存数据取数据删除数据
    Mapput()get()remove
    域对象putAttribute()getAttribute()removeAttribute()

    ServletContent类的四个常见的作用
    1、获取web.xml配置的上下文参数context-param
    2、获取当前的工程路径,格式:/工程路径
    3、获取工程部署后在服务器硬盘上的绝对路径
    4、像Map一样存取数据

        <context-param>
            <param-name>username</param-name>
            <param-value>context</param-value>
        </context-param>
        <context-param>
            <param-name>password</param-name>
            <param-value>context</param-value>
        </context-param>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //        1、获取web.xml配置的上下文参数context-param
            ServletContext context = getServletConfig().getServletContext();
            String username = context.getInitParameter("username");
            System.out.println("context-param的参数username的值是:"+username);
            String password = context.getInitParameter("password");
            System.out.println("context-param的参数username的值是:"+password);
    //        2、获取当前的工程路径,格式:/工程路径
            String contextPath = context.getContextPath();
            System.out.println("获取当前工程路径"+contextPath);
    //        3、获取工程部署后在服务器硬盘上的绝对路径
            //斜杠被服务器解析地址为:http://ip:port/工程名/   映射到IDEA代码的web目录
            String realPath = context.getRealPath("/");
            System.out.println("工程部署的路径是:"+realPath);
            realPath = context.getRealPath("/css");
            System.out.println("css的绝对路径是:"+realPath);
            realPath = context.getRealPath("/img");
            System.out.println("img的绝对路径是:"+realPath);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

  • 相关阅读:
    安装PyCharm(最完整版)
    手写RPC框架 第六天 负载均衡
    异常与自定义异常【Java】
    各类深度学习框架详解+深度学习训练环境搭建-GPU版本
    k8s的网络插件Flannel和Calico(the hard way)
    关于卫星电话的冷知识
    基于SpringBoot的招聘信息管理系统
    LeetCode 2605. Form Smallest Number From Two Digit Arrays【数组,哈希表,枚举;位运算】1241
    微服务和kafka
    【JVM】之常见面试题
  • 原文地址:https://blog.csdn.net/qq_41037176/article/details/125424246