四、web.xml文件配置 及 Servlet类方法中使用

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" metadata-complete="true">
	
	<!-- 为整个web应用配置初始化参数 -->
	<context-param>
		<param-name>abc</param-name>
		<param-value>123123</param-value>
	</context-param>
	<servlet>
		<!-- 方法别名 -->
		<servlet-name>MyFirstServlet</servlet-name>
		<!-- 指向含包名的全路径方法 -->
		<servlet-class>cn.ly.mywebproject.MyFirstServlet</servlet-class>
		<!-- 配置本servlet的初始化参数:键值对的形式 可以在serlvet方法中获取到 -->
		<init-param>
			<param-name>user</param-name>
			<param-value>admin</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<!-- 路由指向的方法别名 -->
		<servlet-name>MyFirstServlet</servlet-name>
		<!-- 路由 -->
		<url-pattern>/MyFirstServlet</url-pattern>
	</servlet-mapping>
</web-app>

Servlet类 init() 中获取 web.xml中的参数

javax.servlet.ServletConfig 提供四个常用的方法:

  1. config.getServletName() // 获取当前Servlet的名称
  2. config.getInitParameter() // 获取当前Servlet中配置的初始化参数
  3. config.getInitParameterNames() // 获取全部初始化参数名,以枚举的格式
  4. config.getServletContext() // 返回对象 代表当前web应用(重要
@Override
public void init(ServletConfig config) throws ServletException {
	// 1、获取 web.xml中 init-param 标签中的参数值
	String userString = config.getInitParameter("user");	// admin
	
	// 2、获取全部初始化参数名,以枚举的格式
	Enumeration<String> enumeration = config.getInitParameterNames();
	// 循环输出
	while (enumeration.hasMoreElements()) {	// 判断是否有更多元素
		// 获取下一个元素名
		String name = (String) enumeration.nextElement();
		// 获取元素名获取其值
		String value = config.getInitParameter(name);
		System.out.println(value);
	}
	
}

ServletContext

1、servletContext 代表当前web应用。
2、web容器在启动时,会为每个web应用程序都创建一个对应的ServletContext对象,它就代表当前web应用。
3、ServletConfig对象中维护了ServletContext对象的引用,可通过 ServletConfig.getServletContext方法获得ServletContext对象
4、一个web应用中所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象实现通讯
5、ServletContext对象通常被称为context域对象

ServletContext应用:

域对象:在一个可以被看见的范围内共享数据用到的对象
作用范围:整个web应用
生命周期

  1. 当服务器启动web应用加载后创建出ServletContext对象,域产生。
  2. 当web应用被移除或服务器关闭,随着web应用销毁,域销毁。

例1:不同Servlet中操作同一个变量

1、One中设置域对象 变量

package cn.ly.auth;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class One implements Servlet {
	// 类变量 用来存放ServletContext对象
	private ServletContext servletContext;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		// 将servletContext对象赋值给一个类变量
		this.servletContext = config.getServletContext();
	}

	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
		// 在ServletContext域对象的属性空间中 定义一个变量,名为name,值为xiaoming
		this.servletContext.setAttribute("name", "xiaoming");
	}
	
	@Override
	public void destroy() {}

	@Override
	public ServletConfig getServletConfig() {}

	@Override
	public String getServletInfo() {}
}

2、Two中获取变量值

package cn.ly.auth;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class Two implements Servlet {
	// 类变量 用来存放ServletContext对象
	private ServletContext servletContext;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		// 将servletContext对象赋值给一个类变量
		this.servletContext = config.getServletContext();
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		// 获取ServletContext对象 属性空间中的变量
		String value = (String) this.servletContext.getAttribute("name");
		// 显示在页面中
		res.getWriter().println(value);	// xiaoming
	}

	@Override
	public void destroy() {}

	@Override
	public ServletConfig getServletConfig() {}

	@Override
	public String getServletInfo() {}
}

例2:获取web应用的初始化参数

<init-param> 标签中设置的参数,只能在当前Servlet中访问使用
<context-param> 标签可以为整个web应用配置属性参数,定义时与<Servlet>标签同级

<!-- 为整个web应用配置初始化参数 -->
<context-param>
	<param-name>username</param-name>
	<param-value>guanyy</param-value>
</context-param>

获取<context-param>标签中的属性参数

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
	// 获取属性参数
	String nameString = this.servletContext.getInitParameter("username");
	res.getWriter().println(nameString);	// guanyy
}

例3:加载并获取资源文件 properties

资源文件的存放目录为:

  • 根目录 / src / main / webapp / config.properties (与jsp文件同级)

资源文件配置格式如下:

username=admin
psd=123456

获取配置参数

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
	// 实例化Properties对象
	Properties properties = new Properties();	// java.util.Properties
	// 获取属性配置文件的绝对路径
	String filePealPath = this.servletContext.getRealPath("config.properties");
	// 实例化配置文件的文件流
	FileReader fr = new FileReader(filePealPath);	// java.io.FileReader
	// 读取文件流
	properties.load(fr);
	// 获取配置文件中的参数值
	String usernameString = properties.getProperty("username");
	// 在页面中显示
	res.getWriter().println(usernameString);	// admin
}

例4:获取当前web应用的名称

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
	// getContextPath() 获取当前web应用的名称
	String contextPathString = this.servletContext.getContextPath();
	res.getWriter().println(contextPathString);	// /demo01
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值