• 0821Servlet基础


    什么是servlet
        jsp页面的前身是servlet, 但是servlet和jsp是两个不同概念
        servlet是运行在服务器端的一段程序, 是可以直接运行一段java后台代码
            servlet特点:
                1, 功能强大
                2, 可移植性
                3, 速度快, 性能高
                4, 安全性高
                5, 可扩展(面向对象)
            与jsp的区别
                1, 角色不同(视图层和控制层)
                2, 编程方法不同
                3, 是否需要重新编译
                4, 运行速度不同

    servlet的代码结构
        public class 类名 extends HttpServlet{
            protected void doGet(HttpServletRequest request, HttpServletResponse response) {
                ...执行代码块
            }

            protected void doPost(HttpServletRequest request, HttpServletResponse response) {
                ...执行代码块
            }
        }

    在web.xml中配置servlet的访问路径
        <servlet>
            <servlet-name>定义一个名字</servlet-name>
            <servlet-class>servlet类的全路径名(选中类名右键点复制限定名, 贴在这里)</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>与上面定义的名字一模一样<servlet-name>
            <url-pattern>/请求的路径(与页面上的一模一样)</url-pattern>
        </servlet-mapping>

    servlet生命周期:
        init();----初始化一个servlet类的实例, 这个方法只会执行一次
            可以在web.xml中配置, <load-on-startup></load-on-startup>, 标签中写一个int型数字, 当这个值为0或者不配置这个标签的时候, 这个servlet将会在使用的时候才会去初始化, 其    他数字会在tomcat服务器运行的时候就加载, 数字越小, 优先级越高
        service()方法
            这个方法是响应客户请求的, 这个方法的执行来决定这个servlet是该调用doPost还是doGet
        destory();----销毁一个servlet类的实例
            停止tomcat的时候会调用此方法, 来销毁servlet实例对象, 释放资源

    servlet中的9大内置对象
        request
        response
        out--->response.getWriter(){注意, 这里的out跟response.getWriter()是有区别的}
        application--->request.getServletContext()
        session--->request.getSession()
        pageContext--->参考jsp中pageContext的获取过程
        page--->this对象
        exception--->抛出的异常对象
        config---调用父类的getServletConfig()


  • 相关阅读:
    SpringCloud系列——SSO 单点登录
    SpringBoot系列——Redis
    基于“formData批量上传的多种实现” 的多图片预览、上传的多种实现
    SpringCloud系列——Bus 消息总线
    SpringCloud系列——Config 配置中心
    SpringCloud系列——Zuul 动态路由
    SpringCloud系列——Ribbon 负载均衡
    SpringCloud系列——Feign 服务调用
    SpringCloud系列——Eureka 服务注册与发现
    ZXing 生成、读取二维码(带logo)
  • 原文地址:https://www.cnblogs.com/DONGb/p/7453464.html
Copyright © 2020-2023  润新知