Servlet之ServletContext

ServletContext

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

  • 共享数据,可以实现servlet之间的通信。

HelloServlet 

package com.yntravelsky.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;

/**
 * @author zhh
 * @description
 * @date 2020/9/19
 */
public class PutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // this.getServletContext() servlet上下文
        ServletContext context = this.getServletContext();
        resp.setContentType("text/html");// 类型
        resp.setCharacterEncoding("utf-8");// 编码格式
        String text = "我爱你中国";// 数据
        context.setAttribute("text", text);// 将一个数据保存在了ServletContext中,key为text,value为text
        resp.getWriter().print("我放的值是:" + text);
    }

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

 GetServlet

package com.yntravelsky.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;

/**
 * @author zhh
 * @description
 * @date 2020/9/22
 */
public class GetServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        resp.setContentType("text/html");// 类型
        resp.setCharacterEncoding("utf-8");// 编码格式
        String txt = (String) context.getAttribute("text");// 获取HelloServlet类中doGet方法中放在ServletContext中的值
        resp.getWriter().print("输出的内容是:" + txt);// 输出到网页上
    }

    @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">

    <servlet>
        <servlet-name>put</servlet-name>
        <servlet-class>com.yntravelsky.servlet.PutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>put</servlet-name>
        <url-pattern>/put</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>get</servlet-name>
        <servlet-class>com.yntravelsky.servlet.GetServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>get</servlet-name>
        <url-pattern>/get</url-pattern>
    </servlet-mapping>
</web-app>

测试:

localhost:8080/put

 localhost:8080/get

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页