其他分享
首页 > 其他分享> > ServletContext对象_hehe.employment.over.15.6

ServletContext对象_hehe.employment.over.15.6

作者:互联网

文章目录

15.9 ServletContext对象

ServletContext对象: 代表整个web应用,可以和程序的容器(服务器)来通信。

15.9.1 获取

package com.xww.web.servletcontext;

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

@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 通过request对象获取
        ServletContext context1 = request.getServletContext();
        //2. 通过HttpServlet获取
        ServletContext context2 = this.getServletContext();

        System.out.println(context1);
        System.out.println(context2);
        System.out.println(context1 == context2);//true
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

15.9.2 功能

package com.xww.web.servletcontext;

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

@WebServlet("/servletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //2. 通过HttpServlet获取
        ServletContext context = this.getServletContext();
        //3. 定义文件名称
        String filename = "a.jpg";
        //4.获取MIME类型
        String mimeType = context.getMimeType(filename);
        System.out.println(mimeType);//image/jpeg

        //设置数据
        context.setAttribute("name","xww");

        // 获取文件的服务器路径
        String b = context.getRealPath("/b.txt");//web目录下资源访问
        System.out.println(b);
        //D:\项目_间接\idea_间接\code_employment_two\out\artifacts\day15_response_war_exploded\b.txt

        String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
        System.out.println(c);
        //D:\项目_间接\idea_间接\code_employment_two\out\artifacts\day15_response_war_exploded\WEB-INF\c.txt

        String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问,src目录中的内容会被放到/WEB-INF/classes目录中
        System.out.println(a);
        //D:\项目_间接\idea_间接\code_employment_two\out\artifacts\day15_response_war_exploded\WEB-INF\classes\a.txt

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

标签:employment,String,hehe,ServletContext,import,out,servlet,javax,over.15
来源: https://blog.csdn.net/qq_44686266/article/details/113768838