• Servlet简介和ServletContext


    0x01: 什么是Servlet?

    • 是sun公司开发动态web的技术
    • 实现了servlet接口的Java程序

    0x02: Servlet的实现类有哪些?

    Servlet接口默认有两个实现类

    • HttpServlet
    • GenericServlet
      HttpServlet 继承自 GenericServlet, 一般我们自己写类只需要继承HttpServlet,重写方法就可以了
    public class HelloServlet extends HttpServlet {
        
        //由于get或者post只是请求实现的不同的方式,可以相互调用,业务逻辑都一样;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //ServletOutputStream outputStream = resp.getOutputStream();
            PrintWriter writer = resp.getWriter(); //响应流
            writer.print("Hello,Serlvet");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    
    

    0x03:Servlet的原理

    浏览器发送http请求给web容器,web容器产生Request和Response对象,这两个对象调用servlet接口的Service方法,Service方法再将返回的Responce信息返回给Responce对象,web容器从Responce对象读取将其响应给客户端。(注意,如果web容器是首次被访问,需要先把我们的java类变成class字节码文件)

    什么是ServletContext

    Web容器在启动时,为每个web程序创建一个对应的ServletContext,它代表当前web应用

    ServletContext的一些用法和特性

    在同一个web应用中,不同的servlet可以共享数据

    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            
            //this.getInitParameter()   初始化参数
            //this.getServletConfig()   Servlet配置
            //this.getServletContext()  Servlet上下文
            ServletContext context = this.getServletContext();
    
            String username = "黎星澄"; //数据
            context.setAttribute("username",username); //将一个数据保存在了ServletContext中,名字为:username 。值 username
    
        }
    
    }
    
    public class GetServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String username = (String) context.getAttribute("username");
    
            resp.setContentType("text/html");
            resp.setCharacterEncoding("utf-8");
            resp.getWriter().print("名字"+username);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    

    获取初始化参数

        <!--在web.xml配置一些web应用初始化参数-->
        <context-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
        </context-param>
    
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String url = context.getInitParameter("url"); //得到name为url的参数
        resp.getWriter().print(url);
    }
    
    

    请求转发

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        System.out.println("进入了ServletDemo04");
        //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/xxx");           //转发的请求路径
        //requestDispatcher.forward(req,resp);      //调用forward实现请求转发;
        context.getRequestDispatcher("/gp").forward(req,resp);
    }
    //请求转发和重定向的区别?
    请求转发:像当于在服务器内部将请求转给请求的资源,然后由服务器相应给客户端
    重定向:服务器将请求资源作为响应信息响应给客户端,客户端根据这个信息对服务器发起请求
    

    读取资源文件
    在java目录下新建properties
    在resources目录下新建properties
    都被打包到同一路径:classes

    username=admin
    password=123456
    
    public class Servlet_Properties extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/hu/servlet/test.properties"); //输入流
    
            Properties prop = new Properties();                                                                               //创建一个Properties对象
            prop.load(is);                                                                                                    //将读取到的加载到prop里
            String user = prop.getProperty("username");
            String pwd = prop.getProperty("password");
    
            resp.getWriter().print(user+":"+pwd);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
  • 相关阅读:
    [Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
    [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid
    [Swift实际操作]八、实用进阶-(5)通过间接代理进行对象间的消息传递
    [Swift]LeetCode927. 三等分 | Three Equal Parts
    [Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II
    我是大牛,我自豪
    程序员拓展人脉的重要性
    程序员拓展人脉的重要性
    2013年总结(3)-活动篇
    2013年总结(3)-活动篇
  • 原文地址:https://www.cnblogs.com/0x10-lexsblog/p/14219975.html
Copyright © 2020-2023  润新知