亲宝软件园·资讯

展开

Java ServletContext 深入了解Java ServletContext

一只胡说八道的猴子 人气:0
想了解深入了解Java ServletContext的相关内容吗,一只胡说八道的猴子在本文为您仔细讲解Java ServletContext的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,ServletContext,下面大家一起来学习吧。

什么是ServletContext?

根据字面意思即Servlet上下文

服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象,这个对象是
全局唯一的,工程内部的所有servlet都共享这个对象,所有应用程序共享对象

下面用一段简单的代码来证明一下

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("/ServletContext2")
public class ServletContext2 extends HttpServlet{
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext servletContext = this.getServletContext();
    System.out.println("ServletContext2的地址:");
    System.out.println(servletContext);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doGet(req, resp);
  }
}
import javax.servlet.RequestDispatcher;
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("/ServletContext")
public class ServletContext extends HttpServlet{
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    javax.servlet.ServletContext servletContext = this.getServletContext();
    System.out.println("ServletContext2的地址:");
    System.out.println(servletContext);
    RequestDispatcher servletContext2 = req.getRequestDispatcher("ServletContext2");
    servletContext2.forward(req,resp);
  }
}

可以看到二者的地址一模一样

ServletContext对象的生命周期

诞生:web部署到服务器上,启动服务器,此时上下文环境对象创建,只要tomcat服务器不关闭,上下文环境对象就一直
存在

销毁:关闭服务器或者是从web服务器上卸载该程序的时候,该对象销毁

怎么创建ServletContext对象?

方法一:

javax.servlet.ServletContext servletContext = this.getServletContext();

方法二:

javax.servlet.ServletContext servletContext1 = req.getServletContext();

方法三:

javax.servlet.ServletContext servletContext3 = this.getServletConfig().getServletContext();

ServletContext对象的作用与相关方法

1.是一个域对象*

什么是域:域其实就是一个map结构,及存入一个键值对,来存储获取数据
作用范围:本工程内部
域对象可以被本工程内所有的servlet对象共享,因为一个工程只有一个servletcontext对象

方法介绍:
存储数据: void setAttribute(String var1, Object var2);

移除数据: void removeAttribute(String var1);

获取数据: Object getAttribute(String var1);
1.由于取出的是一个Object类型,需要强转
2.如果指定的属性名不存在,则返回null

2.可以获取文件的真实路径

真实路径:即部署在服务器是的路径
String getRealPath() 获取文件的真实路径

不同目录下访问资源的方式不同

//src下的资源访问
    String realPath = servletContext.getRealPath("/WEB-INF/classes/a.text");
    System.out.println(realPath);
//WEB-INF下的资源访问
    String realPath1 = servletContext.getRealPath("/WEB-INF/b.text");
    System.out.println(realPath1);
 //web目录下的资源访问
    String realPath2 = servletContext.getRealPath("/c.text");
    System.out.println(realPath2);

代码

import javax.servlet.RequestDispatcher;
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("/ServletContext")
public class ServletContext extends HttpServlet{
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    javax.servlet.ServletContext servletContext = this.getServletContext();
    //src下的资源访问
    String realPath = servletContext.getRealPath("/WEB-INF/classes/a.text");
    System.out.println(realPath);
    //WEB-INF下的资源访问
    String realPath1 = servletContext.getRealPath("/WEB-INF/b.text");
    System.out.println(realPath1);
    //web目录下的资源访问
    String realPath2 = servletContext.getRealPath("/c.text");
    System.out.println(realPath2);
  }
}

结果

3.获得MIME类型

s什么是MIME类型,是在互联网通信过程中定义等等一种文件数据类型
格式:大类型/小类型 image/jpeg
获取: String getMimeType(String file)

代码如下

import javax.servlet.RequestDispatcher;
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("/ServletContext")
public class ServletContext extends HttpServlet{
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    javax.servlet.ServletContext servletContext = this.getServletContext();
    String filename="a.jpg";
    String mimeType = servletContext.getMimeType(filename);
    System.out.println(mimeType);
  }
}

结果

加载全部内容

相关教程
猜你喜欢
用户评论