二、Servlet中的方法

一、综述

image.png

注:
1、init()方法和destroy()方法只会执行一次。

二、Service()方法

new出来的Servlet中继承了HttpServlet类,但是没有重写service()方法,此时添加service()方法,然后再部署项目运行看结果:
代码:

package cn.kgc.web.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet3 extends HttpServlet {
    
    public MyServlet3() {
        super();
    }

    public void destroy() {
        System.out.println("销毁Servlet");
    }

    protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("进入Service");
        super.service(arg0, arg1);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("进入doGet()");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("进入doPost()");
    }

    public void init() throws ServletException {
        System.out.println("初始化Servlet");
    }

}

运行结果:


image.png

分析:
1、可以看到浏览器访问的时候,先初始化,然后进入Service,再之后进入doGet方法;然后刷新页面的时候,也是先进入Service,再之后进入doGet方法。
2、Service其实是派发器,根据用户请求类型调用相应的doGet()和doPost()(这也是继承了HttpServlet 类之后没有重写该方法的原因)。可以去查看源码,会发现它就是个派发器。直接ctrl加左键点击的话,会发现没有资源,这是因为没有关联源码资源,这时候关联之后才能看得到。
3、同时这个地方也对应这Servlet的生命周期:


image.png

实例化没见到过,但是这是一个java对象,一定是有实例化的,不然根本调用不了其中的方法。创建实例化对象的实际是Servlet容器启动的时候或者检测到客户端请求的时候。
4、init()方法:在使用实现Servlet接口创建Servlet的时候,init()方法会有一个参数,如下:
public void init(ServletConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
}

这个ServletConfig 指的是Servlet的配置信息(配置在web.xml中),是去操作Servlet初始化信息的。而继承HttpServlet类创建Servlet的时候,init()方法并没有参数,不过也可以使用这个有参数的init方法。
如下:

package cn.kgc.web.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet3 extends HttpServlet {
    
    public MyServlet3() {
        super();
    }

    public void destroy() {
        System.out.println("销毁Servlet");
    }

    protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("进入Service");
        super.service(arg0, arg1);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("进入doGet()");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("进入doPost()");
    }

//  public void init() throws ServletException {
//      System.out.println("初始化Servlet");
//  }

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        System.out.println("初始化Servlet");
        String name = config.getInitParameter("charsetname");
        System.out.println(name);
    }

}

其中web.xml中保存的配置信息如下:



  
  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    MyServlet3
    cn.kgc.web.Servlet.MyServlet3
    
        charsetname
        utf-8
    
  

  
    MyServlet3
    /servlet/MyServlet3
  
  
    index.jsp
  

此时部署项目运行之后的结果为:


image.png

你可能感兴趣的:(二、Servlet中的方法)