Servlet-ServletContext对象

概述

servlet-api 4.0.1版本

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

常用方法

public String getContextPath(); // 返回Web应用程序的上下文路径
// /xxx

public ServletContext getContext(String uripath); // 返回与服务器上指定URL对应的ServletContext对象

public int getMajorVersion(); // 返回此Servlet容器支持的Servlet API的主要版本

public int getMinorVersion(); // 返回此Servlet容器支持的Servlet API的次要版本

public int getEffectiveMajorVersion(); // 获取此ServletContext表示的应用程序所基于的Servlet规范的主要版本

public int getEffectiveMinorVersion(); // 获取此ServletContext表示的应用程序所基于的Servlet规范的次要版本

public String getMimeType(String file); // 返回指定文件的MIME类型

public Set<String> getResourcePaths(String path); // 列出了Web应用程序中资源的所有路径,这些路径的最长子路径与提供的path参数匹配

public URL getResource(String path) throws MalformedURLException; // 返回映射到给定路径的资源的URL

public InputStream getResourceAsStream(String path); // 返回位于命名路径处的资源作为InputStream对象

public RequestDispatcher getRequestDispatcher(String path); // 根据路径获取请求转发对象

public RequestDispatcher getNamedDispatcher(String name); // 根据名字获取请求转发对象

public Servlet getServlet(String name) throws ServletException; // 根据名字获取Servlet对象

public Enumeration<Servlet> getServlets(); // 获取Servlet对象组

public Enumeration<String> getServletNames();  // 获取Servlet名字组

public void log(String msg); // 写消息到log

public String getRealPath(String path); // 获取与给定虚拟路径相对应的真实路径

public String getServerInfo(); // 返回运行servlet的servlet容器的名称和版本

public String getInitParameter(String name); // 获取web.xml中<context-param>定义的参数,不存在返回null

public Enumeration<String> getInitParameterNames(); // 获取web.xml中<context-param>定义的所有参数,不存在空Enumeration

public boolean setInitParameter(String name, String value); // 在此ServletContext上使用给定的名称和值设置上下文初始化参数

public Object getAttribute(String name); // 返回具有给定名称的servlet容器属性;如果该名称没有属性,则返回null

public Enumeration<String> getAttributeNames(); // 返回一个枚举,其中包含该ServletContext中可用的属性名称

public void setAttribute(String name, Object object); // 设定指定值到ServletContext上下文环境,如果name存在则覆盖

public void removeAttribute(String name); // 从此ServletContext中删除具有给定名称的属性

public String getServletContextName(); // 返回与此ServletContext对应的Web应用程序的名称,该名称由display-name元素为此Web应用程序的部署描述符中指定

public SessionCookieConfig getSessionCookieConfig(); // 获取SessionCookieConfig对象,可以通过该对象配置代表此ServletContext创建的会话跟踪cookie的各种属性

public void addListener(String className); // 添加监听器

public JspConfigDescriptor getJspConfigDescriptor(); // 获取与<jsp-config>相关的配置,该配置是从此ServletContext表示的Web应用程序的web.xml和web-fragment.xml描述符文件中聚合的

public ClassLoader getClassLoader(); // 获取由此ServletContext表示的Web应用程序的类加载器

public String getVirtualServerName(); // 返回在其上部署了ServletContext的逻辑主机的配置名称
// Catalina/localhost

public int getSessionTimeout(); // 获取此ServletContext默认支持的会话超时(以分钟为单位)

public void setSessionTimeout(int sessionTimeout); // 设置此ServletContext的会话超时(以分钟为单位)

public String getRequestCharacterEncoding(); // 获取此ServletContext默认支持的请求字符编码

public void setRequestCharacterEncoding(String encoding); // 设置此ServletContext的请求字符编码。

public String getResponseCharacterEncoding(); // 获取此ServletContext默认支持的响应字符编码

public void setResponseCharacterEncoding(String encoding); // 设置此ServletContext的响应字符编码

共享数据

Set

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取上下文容器
    ServletContext context = this.getServletContext();
    // 将一个数据保存在了ServletContext中,键值对形式,名字为:data,值: 你好
    context.setAttribute("data", "你好");

    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    response.getWriter().print("Have set!");
}

Get

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取上下文容器
    ServletContext context = this.getServletContext();
    // 根据key获取值
    String data = (String) context.getAttribute("data");

    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    response.getWriter().print("data:" + data);
}

验证

先获取值会得到null值:

获取初始化参数

web.xml:

<!--配置一些web应用初始化参数-->
<context-param>
    <param-name>url</param-name>
    <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取上下文容器
    ServletContext context = this.getServletContext();
    // 获取web.xml中预配置的参数
    String url = context.getInitParameter("url");

    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    response.getWriter().print("data:" + url);
}

测试

请求转发

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取上下文容器
    ServletContext context = this.getServletContext();
    // 请求转发到 /hello
    context.getRequestDispatcher("/hello").forward(request, response);
}

测试

读取资源文件

新建资源文件

重新生成项目

确保配置好pom,防止导出资源失败

复制资源文件路径

代码

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取输入流
    InputStream is = this.getServletContext().getResourceAsStream("WEB-INF/classes/db.properties");
    // 加载Properties文件
    Properties prop = new Properties();
    prop.load(is);
    // 获取值
    String username = prop.getProperty("username");
    String password = prop.getProperty("password");

    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    response.getWriter().print("username:" + username);
    response.getWriter().print("<br/>");
    response.getWriter().print("password:" + password);
}

测试

posted @ 2021-01-08 20:56  神乐g  阅读(116)  评论(0)    收藏  举报