request可以直接获取servletcontext对象吗_ServletContext对象的使用

本文详细介绍了Servlet上下文ServletContext的使用,包括通过HttpServlet获取ServletContext对象,利用它来获取MIME类型、设置和获取域对象数据,以及获取项目中文件的服务器真实路径。示例代码展示了如何在不同Servlet中实现数据共享和文件路径的获取。
摘要由CSDN通过智能技术生成

ServletContext对象:代表了整个web应用(也就是整个项目工程),可以和程序的容器(服务器来进行通信)

功能:

1.获取MIME类型

2.域对象:共享数据

3.获取文件的真实(服务器)路径(javaweb运行的项目其实是保存在tomcat里的,而不是运行的工作空间里面的项目(Eclipse里面写的项目))

获取:

1.通过request对象获取

request.getServletContext();

2.通过HttpServlet获取

this.getServletContext();

javaweb里面的配置文件一般放在什么位置

package cn.itcast.web.servletcontext;

import java.io.IOException;

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;

/**
 * Servlet implementation class ContextDemo1
 */
@WebServlet("/ContextDemo2")
public class ContextDemo2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContextDemo2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//2.通过HttpServlet获取
		ServletContext context = this.getServletContext();
		//3.定义一个文件名称
		String filename="a.jpg";  //image/jpeg
		//4.获取MIME类型
		String mimeType = context.getMimeType(filename);
		System.out.println(mimeType);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
package cn.itcast.web.servletcontext;

import java.io.IOException;

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;

/**
 * Servlet implementation class ContextDemo1
 */
@WebServlet("/ContextDemo3")
public class ContextDemo3 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContextDemo3() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//2.通过HttpServlet获取
		ServletContext context = this.getServletContext();
		//设置数据
		context.setAttribute("msg", "haha");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
package cn.itcast.web.servletcontext;

import java.io.IOException;

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;

/**
 * Servlet implementation class ContextDemo1
 */
@WebServlet("/ContextDemo4")
public class ContextDemo4 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContextDemo4() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//2.通过HttpServlet获取
		ServletContext context = this.getServletContext();
		//获取数据
		Object msg = context.getAttribute("msg");
		System.out.println(msg);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
package cn.itcast.web.servletcontext;

import java.io.File;
import java.io.IOException;

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;

/**
 * Servlet implementation class ContextDemo1
 */
@WebServlet("/ContextDemo5")
public class ContextDemo5 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContextDemo5() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//2.通过HttpServlet获取
		ServletContext context = this.getServletContext();
		//获取文件的服务器路径
		String realPath = context.getRealPath("/b.txt");//访问web目录下的资源路径
		System.out.println(realPath);
		File file=new File(realPath);
		String c = context.getRealPath("/WEB-INF/c.txt");//访问WEB-INF目录下的资源路径
		System.out.println(c);
		String a = context.getRealPath("/WEB-INF/classes/a.txt");//访问src目录下的资源路径
		System.out.println(a);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
e585e7b3f5e18b7b26797b02f82550ee.png
https://www.zhihu.com/video/1242481616892735488
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值