zoukankan      html  css  js  c++  java
  • ServletContext

    web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;

    作用:

      1)共享数据,我在这个servlet中保存的数据,可以在另外一个servlet中拿到

     HelloServlet

    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String username = "老尹";  // 数据
            context.setAttribute("username",username); // 将一个数据保存在ServletContext中
        }
    }

    getc

    public class GetServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
    // 获得ServletContext中放置的uaername数据,强转为String String username
    = (String)context.getAttribute("username"); resp.setContentType("text/html"); // 设置文本格式 resp.setCharacterEncoding("utf-8"); // 设置字符格式 resp.getWriter().print("name" + username); // 输出信息到网页 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

    2) 获取初始化参数

    <context-param>
      <param-name>url</param-name>
      <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>
    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String url = context.getInitParameter("url");
            resp.getWriter().print(url);
    
        }

    3)请求转发

     gp的代码(gp是ServletDemo03的缩写)

    package com.kuang.servlet;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class ServletDemo03 extends HelloServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String url = context.getInitParameter("url");
            resp.getWriter().print(url);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }

    图解重定向和转发:

    重定向

    转发

    4:读取资源文件

    Properties类

  • 相关阅读:
    【数据相关】如何进行数据标注(1)
    【机器视觉硬件】机器视觉硬件学习笔记3——镜头
    【机器视觉硬件】工业相机的分类
    【机器视觉硬件】工业相机的主要参数
    【机器视觉硬件】工业相机的主要接口类型
    【机器视觉硬件】机器视觉硬件学习笔记2——工业相机
    【知识相关】机器学习之独热编码(One-Hot)详解(代码解释)
    【机器视觉硬件】硬件学习笔记1——光源
    学习笔记分享之汇编---3. 堆栈&标志寄存器
    学习笔记分享之汇编---1. 通用寄存器
  • 原文地址:https://www.cnblogs.com/YXBLOGXYY/p/14603432.html
Copyright © 2011-2022 走看看