JavaWeb的ServletContext!

一、ServletContext(数据共享):

解释:Web容器启动时,他会为每个web容器创建一个,它代表当前的Web应用;是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放。

白话解释:现在有一个,全局的数据,张三可以拿到,李四也可以拿到,王麻子也可以拿到;ServletContext起的作用就是把共给数据传递给每一个想要数据的Servlet的中间键;

二、代码解释:我的项目目录结构都是一样的不会有任何变化

①:在servlet包里面创建一个Helloservlet,附:源码!源码图!

package com.laugh.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class Helloservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //第一步获取ServletContext对象
        ServletContext Context = this.getServletContext();

        //设置公共数据
        String Name = "Laugh";
        String Sex = "男";
        int Age = 24;

        //把上面数据设置成键值对的形式 (key,value)
        Context.setAttribute("Name",Name);
        Context.setAttribute("Sex",Sex);
        Context.setAttribute("Age",Age);

        //写一个反馈在页面的东西,证明来过
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();
        writer.print("<h1>我进入了“Helloservlet”里面了!</h1>");

        //在后台输入一下,验证进入与否
        System.out.println("进入了“Helloservlet”里面了");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

②:再创建一个Getservlet,附:源码!源码图!

package com.laugh.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置一下编码格式
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");

        //想要获取Helloservlet刚刚赋的值,咱得现有ServletContext这个对象  this:因为咱操作操作的都是它这个对象,所以用this
        ServletContext Context = this.getServletContext();

        //咱们在Helloservlet里面的是Set设置了值,所以咱们在getServlet里面获取值就用get
        //开始生成下面这个的时候类型是Object,这里咱们用String来接,咱们知道存的是字符串嘛,然后会报错,因为程序不知道是字符串所以强行转换一个类型就好,下面以此类推
        String Name = (String) Context.getAttribute("Name");
        String Sex = (String) Context.getAttribute("Sex");
        int Age = (int) Context.getAttribute("Age");

        //把拿到的值,输出到页面上
        resp.getWriter().println(Name);
        resp.getWriter().println(Sex);
        resp.getWriter().println(Age);

        //在后台输入一下,验证进入与否
        System.out.println("进入了“Getservlet”里面了");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

③:配置web.xml,一个配一对,上面有两个,所以咱得配两对;附:源码!源码图!

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--我这个头是新的-->
    <!--我是提供 Helloservlet 使用的哦;顺便让Helloservlet给ServletContext赋一下值-->
    <servlet>
        <servlet-name>laugh</servlet-name>
        <servlet-class>com.laugh.servlet.Helloservlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>laugh</servlet-name>
        <url-pattern>/laugh</url-pattern>
    </servlet-mapping>

<!--我是提供GetServlet使用的哦,在运行完 Helloservlet 的时候,运行我就会显示值啦--> <servlet> <servlet-name>get</servlet-name> <servlet-class>com.laugh.servlet.GetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>get</servlet-name> <url-pattern>/get</url-pattern> </servlet-mapping> </web-app>

④:运行结果(正常运行顺序)

⑤:输出结果(非正常运行顺序)

生在内卷,你卷我也卷。 愿与诸君共勉!
原文地址:https://www.cnblogs.com/superyonng/p/15593966.html