spring--整合springMVC--01--JavaWeb基础--02--Servlet

一、什么是Servlet

(1)Servlet 是运行在服务器上的一个 java 小程序, 它可以接收客户端发送过来的请求, 并响应数据给客户端。

(3)jsp 页面本质上是一个 Servlet 程序。当我们第一次访问 jsp 页面的时候。 Tomcat 服务器会帮我们把 jsp 页面翻译成为一个 java 源文件。 并且对它进行编译成
为.class 字节码程序。 我们打开 java 源文件不难发现其继承了 HttpServlet 类。

二、Servlet生命周期

1、 执行 Servlet 构造器方法
2、 执行 init 初始化方法
       第一、 二步, 是在第一次访问的时候创建 Servlet 程序会调用。
3、 执行 service 方法
       第三步, 每次访问都会调用。
4、 执行 destroy 销毁方法
      第四步, 在 web 工程停止的时候调用。

三、Servlet 类的继承体系【如何自定义实现Servlet】

3.1 如何自定义实现Servlet

一般在实际项目开发中, 都是继承 HttpServlet 类的方式去实现 Servlet 程序。
1、 编写一个类去继承 HttpServlet 类
2、 根据业务需要重写 doGet 或 doPost 方法
3、 到 web.xml 中的配置 Servlet 程序的访问地址

3.2 为什么继承HttpServlet就可以自定义实现Servlet【术语:Servlet继承体系】

spring--整合springMVC--01--JavaWeb基础--02--Servlet_第1张图片

3.3 Servlet在web.xml中最基本的配置

spring--整合springMVC--01--JavaWeb基础--02--Servlet_第2张图片

 

四、ServletConfig

4.1 什么是ServletConfig

(1)ServletConfig是 Servlet 程序的配置信息类。
(2)Servlet 程序和 ServletConfig 对象都是由 Tomcat 负责创建, 我们负责使用。
(3)Servlet 程序默认是第一次访问的时候创建, ServletConfig 是每个 Servlet 程序创建时, 就创建一个对应的 ServletConfig 对象。

注意:每个Servlet持有自己的ServletConfig;所有Servlet持有一个共同的ServletContext

4.2 ServletConfig 类的三大作用

1、 可以获取 Servlet 程序的别名 servlet-name 的值
2、 获取初始化参数 init-param
3、 获取 ServletContext 对象

4.3 举例

(1)web.xml配置


    
        myServlet
        com.fuping3.servlet.Myservlet
        
        
            userName
            fuping3
        
        
            password
            123456
        
    

    
        myServlet
        /servlet
    

    
        myServlet2
        com.fuping3.servlet.Myservlet2
        
            userName
            fuping3-2
        
        
            password
            123456-2
        
        1
    

    
        myServlet2
        /servlet2
    

(2)Servlet程序

package com.fuping3.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Myservlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Myservlet...");
        ServletConfig config = getServletConfig();
        System.out.println("ServletConfig对象="+config);
        System.out.println("Servlet别名="+config.getServletName());
        System.out.println("Servlet初始化参数-userName="+config.getInitParameter("userName"));
        System.out.println("Servlet初始化参数-password="+config.getInitParameter("password"));
        System.out.println("ServletContext对象="+config.getServletContext());
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/my.jsp");
        requestDispatcher.forward(req,resp);
    }
}
package com.fuping3.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Myservlet2 extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("Myservlet2 init...");
        super.init(config);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Myservlet2...");

        /**
         * 如果重写init方法,须要调用父类init(config)方法,否则此处获取不到ServletConfig对象
         * 原因:super.init(config)方法调用GenericServlet类中init(ServletConfig config)方法,初始化GenericServlet持有的ServletConfig
         * 1、GenericServlet类中init(ServletConfig config)方法源码
         *    public void init(ServletConfig config) throws ServletException {
         *         this.config = config;
         *         this.init();
         *     }
         * 2、GenericServlet类中getServletConfig()方法源码
         *     public ServletConfig getServletConfig() {
         *         return this.config;
         *     }
         *
         */
        ServletConfig config = getServletConfig();
        System.out.println("ServletConfig对象="+config);
        System.out.println("Servlet别名="+config.getServletName());
        System.out.println("Servlet初始化参数-userName="+config.getInitParameter("userName"));
        System.out.println("Servlet初始化参数-password="+config.getInitParameter("password"));
        System.out.println("ServletContext对象="+config.getServletContext());
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/my.jsp");
        requestDispatcher.forward(req,resp);
    }
}

(3)运行结果

(a)访问http://localhost:8080/mvcDemo2/servlet   运行结果

Myservlet...
ServletConfig对象=org.apache.catalina.core.StandardWrapperFacade@18f7000
Servlet别名=myServlet
Servlet初始化参数-userName=fuping3
Servlet初始化参数-password=123456
ServletContext对象=org.apache.catalina.core.ApplicationContextFacade@1488981

(b)访问http://localhost:8080/mvcDemo2/servlet2   运行结果

Myservlet2...
ServletConfig对象=org.apache.catalina.core.StandardWrapperFacade@dd1c31
Servlet别名=myServlet2
Servlet初始化参数-userName=fuping3-2
Servlet初始化参数-password=123456-2
ServletContext对象=org.apache.catalina.core.ApplicationContextFacade@1488981

4.4 web.xml中load-on-startup的作用【多个Servlet如何执行】

        load-on-startup 元素标记容器是否应该在web应用程序启动的时候就加载这个servlet(实例化并调用其init()方法),它的值必须是一个整数,表示servlet被加载的先后顺序。
如果该元素的值为负数或者没有设置,则容器会当Servlet被请求时再加载。如果值为正整数或者0时,表示容器在应用启动时就加载并初始化这个servlet,值越小,servlet的优先级越高,就越先被加载。值相同时,容器就会自己选择顺序来加载

五、ServletContext

5.1 基本概念

spring--整合springMVC--01--JavaWeb基础--02--Servlet_第3张图片

 

5.2 举例

(1)web.xml配置


    
    
        city
        Beijing
    

    
        myServlet
        com.fuping3.servlet.Myservlet
        
            userName
            fuping3
        
        
            password
            123456
        
    

    
        myServlet
        /servlet
    
    
        myServlet2
        com.fuping3.servlet.Myservlet2
        
            userName
            fuping3-2
        
        
            password
            123456-2
        
        1
    

    
        myServlet2
        /servlet2
    

(2)Servlet程序

package com.fuping3.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Myservlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Myservlet...");
        ServletConfig config = getServletConfig();
        System.out.println("ServletConfig对象="+config);
        System.out.println("Servlet别名="+config.getServletName());
        System.out.println("Servlet初始化参数-userName="+config.getInitParameter("userName"));
        System.out.println("Servlet初始化参数-password="+config.getInitParameter("password"));

        ServletContext context = config.getServletContext();
        System.out.println("ServletContext对象="+context);
        System.out.println("上下文参数-city="+context.getInitParameter("city"));
        System.out.println("当前工程路径="+context.getContextPath());
        System.out.println("当前工程在服务器上的物理路径="+context.getRealPath("/"));
        System.out.println("当前工程/imgdir目录在服务器上的物理路径="+context.getRealPath("/imgdir"));
        System.out.println("当前工程/imgdir目录下2.png在服务器上的物理路径="+context.getRealPath("/imgdir/2.png"));
        System.out.println("当前工程/WEB-INF/imgs/1.png目录在服务器上的物理路径="+context.getRealPath("/WEB-INF/imgs/1.png"));
        String country = context.getAttribute("country") + "";
        System.out.println("context中获取的域对象-country的值为"+country);
        context.setAttribute("country","china");

        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
        requestDispatcher.forward(req,resp);
    }
}
package com.fuping3.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;

public class Myservlet2 extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("Myservlet2 init...");
        super.init(config);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Myservlet2---------------");

        ServletContext context =getServletContext();
        System.out.println("ServletContext对象="+context);
        System.out.println("上下文参数-city="+context.getInitParameter("city"));
        System.out.println("当前工程路径="+context.getContextPath());
        System.out.println("当前工程在服务器上的物理路径="+context.getRealPath("/"));
        System.out.println("当前工程/imgdir目录在服务器上的物理路径="+context.getRealPath("/imgdir2"));
        System.out.println("当前工程/imgdir目录下2.png在服务器上的物理路径="+context.getRealPath("/imgdir/aa.png"));
        System.out.println("当前工程/WEB-INF/imgs/1.png目录在服务器上的物理路径="+context.getRealPath("/WEB-INF/imgs/aa.png"));
        String country = context.getAttribute("country") + "";
        System.out.println("context中获取的域对象-country的值为"+country);
        /**
         * web目录或WEB-INF目录下子目录,都可以写入,简单示例如下(未完善处理异常)
         */
        //String realPath=context.getRealPath("/imgdir");
        String realPath=context.getRealPath("/WEB-INF/imgs");
        FileOutputStream fos=new FileOutputStream(realPath+"/fuping3.txt");
        byte[] bytes=realPath.getBytes();
        fos.write(bytes);
        fos.close();

        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/my.jsp");
        requestDispatcher.forward(req,resp);
    }
}

(3)访问 http://localhost:8080/mvcDemo2/servlet 运行结果

Myservlet...
ServletConfig对象=org.apache.catalina.core.StandardWrapperFacade@1b33bd9
Servlet别名=myServlet
Servlet初始化参数-userName=fuping3
Servlet初始化参数-password=123456
ServletContext对象=org.apache.catalina.core.ApplicationContextFacade@174da9f
上下文参数-city=Beijing
当前工程路径=/mvcDemo2
当前工程在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\
当前工程/imgdir目录在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\imgdir
当前工程/imgdir目录下2.png在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\imgdir\2.png
当前工程/WEB-INF/imgs/1.png目录在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\WEB-INF\imgs\1.png
context中获取的域对象-country的值为null



Myservlet2---------------
ServletContext对象=org.apache.catalina.core.ApplicationContextFacade@174da9f
上下文参数-city=Beijing
当前工程路径=/mvcDemo2
当前工程在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\
当前工程/imgdir目录在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\imgdir2
当前工程/imgdir目录下2.png在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\imgdir\aa.png
当前工程/WEB-INF/imgs/1.png目录在服务器上的物理路径=E:\ideaProject\mvcDemo2\target\mvcDemo2-1.0-SNAPSHOT\WEB-INF\imgs\aa.png
context中获取的域对象-country的值为china

六、请求转发与请求重定向

(1)请求转发特点

spring--整合springMVC--01--JavaWeb基础--02--Servlet_第4张图片

 

请求转发关键代码

        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
        requestDispatcher.forward(req,resp);

(2)请求重定向特点

spring--整合springMVC--01--JavaWeb基础--02--Servlet_第5张图片

 

重定向代码

spring--整合springMVC--01--JavaWeb基础--02--Servlet_第6张图片

 

你可能感兴趣的:(spring,javaweb,spring)