Servlet

Servlet简介

什么是Servlet

Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求、响应给浏览器的动态资源。但servlet的实质就是java代码,通过java的API 动态的向客户端输出内容
servlet规范:包含三个技术点
1)servlet技术
2)filter技术—过滤器
3)listener技术—监听器

Servlet的架构简述

Servlet --GenericServlet--HttpServlet--myClass

这里写图片描述

这里写图片描述

这里写图片描述

Servlet的API(生命周期)

(1)Servlet接口中的方法
1)init(ServletConfig config)
何时执行:servlet对象创建的时候执行
ServletConfig : 代表的是该servlet对象的配置信息

2)service(ServletRequest request,ServletResponse response)
何时执行:每次请求都会执行
ServletRequest :代表请求 认为ServletRequest 内部封装的是 http请求的信息
ServletResponse :代表响应 认为要封装的是响应的信息

3)destroy()
何时执行:servlet销毁的时候执行

(2)HttpServlet类的方法
1)init()
2)doGet(HttpServletRequest request,HttpServletResponse response)
3)doPost(HttpServletRequest request,HttpServletResponse response)
4)destroy()

(3)Servlet的生命周期(面试题)
1)Servlet何时创建
默认第一次访问servlet时创建该对象
为什么是默认?
当在servlet的配置时 加上一个配置 servlet对象在服务器启动 时就创建

2)Servlet何时销毁
服务器关闭servlet就销毁了

3)每次访问必然执行的方法
service(ServletRequest req, ServletResponse res)方法

Servlet接口

public class servletShow implements Servlet {

    //servlet 的生命周期  实例化,初始化,服务,销毁
    //此接口有五个方法,init  service destory 是生命周期方法     
    //getServletConfig是获取任何启动信息,getServletInfo是垃圾方法
    /**
     * 在访问此servelt的时候打印如下信息:
     * (可以修改配置,让服务器一启动就实例化,初始化servlet,还可以  
     *  配置不同的servlet出生的先后顺序) 
     *  老子实例化了,只有第一次访问的时候
        老子初始化了,只有第一次访问的时候
        老子服务了

        当服务器关闭或者项目卸载的时候回打印“老子销毁了”
        第一次访问后,刷新页面只会重复打印“老子服务了”,

--------------------所以Servlet是单实例-----------------------
        第一次访问实例化一个servlet,只有一个,但是service()方法是多线程的


     */
    //构造器
    public servletShow() {
        super();
        System.out.println("老子实例化了");
    }



    //销毁
    @Override
    public void destroy() {
        System.out.println("老子销毁了");

    }



    //获得配置
    @Override
    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return null;
    }

    //垃圾方法,获得信息,作者,版本,版权等等无用信息
    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    //初始化
    @Override
    public void init(ServletConfig arg0) throws ServletException {
        System.out.println("老子初始化了");

    }

    //服务
    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        System.out.println("老子服务了");

    }

}

GenericServlet

GenericServlet使用适配器设计模式,继承Servlet接口和多个其他的类

/**
 * 
 *----------------------- 适配器设计模式adapter模式-----------------------------
 *      class adaptee  {method2();}
 *      class adapter   extends adaptee implements target
 *      class client   {method1(target t);}
 *      interface target   {method3();}
 *      
 *      如上,client类想要在method1中使用adaptee中的方法,但是自己又没有关于adaptee的参数,
 *      target a=new adapter();
 *
 *      将一个类的接口转换成客户希望的另外一个接口。 Adapter模式使得原本由于接口不兼容 而不能一起工作的那些类可以一起工作。 
 */
public class servletShow_1 extends GenericServlet {
    //GenericServlet实现了servlet的接口,不用把servlet中的所以方法都写出来
    public servletShow_1() {
        super();

    }


    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

    }

}

HttpServlet

HttpServlet使用模板方法设计模式,继承GenericServlet

public class servletShow2 extends HttpServlet {

   /**
    *  
    *  -------------模板方法设计模式 template method----------------------
    *  
    *         1.带着炮友出去
    *         2.吃东西   //吃的菜到底是什么?
    *         3.愉快玩耍//到底去什么地方玩什么?
    *         4.开炮
    *  --------------------------------------------------------------
    *  模板方法设计模式:定义好骨架,某些具体步骤在子类中实现,这样子类可以在不改变算法结构的
    *  情况下定义具体实现。httpServlet把构架写好,我们通过重写
    * doget()和doPost()来实现具体的功能。
    *  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_get_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); //返回error
        } else {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);    //返回error
        }
    }
        这是doGet()的源码,如果不重写,那么就会返回405错误信息在浏览器页面上。

        httpServlet重写了和重载了service方法
         public void service(ServletRequest req, ServletResponse res){
                把req强转成HttpServletRequest类型

                调用service(HttpServletRequest req, HttpServletResponse resp)
         }

       protected void service(HttpServletRequest req, HttpServletResponse resp){

            根据http传来的method.equals(METHOD_GET)  调用不同的方法:doget() dopost()

       }







    */


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

}

继承HttpServlet

自己写的类继承HttpServlet,最常用的一种

public class servletShow3 extends HttpServlet {

    /*  int num1=1;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        num1++;//servlet是单例,多个人对num1进行++,线程会出现问题。
        //所以不要用全局变量,使用局部变量。
        int num2=1;
        System.out.println(num1);
    }*/

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("老子要办事!");
        System.out.println("办不了,我找人来");
        ServletContext application=this.getServletContext();  //servletContext域对象,代表整个app
        RequestDispatcher rd=application.getRequestDispatcher("/servletShow4");
        rd.forward(request, response);//把请求向下传递
        System.out.println("事情办完了");
    }



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

}
//--------------------------------------------------
public class servletShow4 extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我是show4,我来帮show3办事");
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

}

ServletContext对象

什么是ServletContext对象

ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象 内部封装是该web应用的信息,ServletContext对象一个web应用只有一个

问题:一个web应用有几个servlet对象?
多个

ServletContext对象的生命周期?
创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状 态))
销毁:web应用被卸载(服务器关闭,移除该web应用)

怎样获得ServletContext对象
1)ServletContext servletContext = config.getServletContext();
2)ServletContext servletContext = this.getServletContext();

ServletContext的作用

(1)获得web应用全局的初始化参数

web.xml中配置初始化参数
这里写图片描述

通过context对象获得参数
这里写图片描述

获得web应用中任何资源的绝对路径(重要 重要 重要)

方法:String path = context.getRealPath(相对于该web应用的相对地址);
String path=this.getServletContext().getRealPath(“/WEB-INF/classes/柳梦璃.jpg”);

ServletContext是一个域对象(重要 重要 重要)

什么是域对象?什么是域?
存储数据的区域就是域对象

ServletContext域对象的作用范围:整个web应(所有的web资源都可以随意向 servletcontext域中存取数据,数据可以共享)

域对象的通用的方法:
setAtrribute(String name,Object obj);
getAttribute(String name);
removeAttribute(String name);

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值