Servlet类详解

Servlet核心类

关联:

从我可以拿到你

想要拿到servletConfig对象只要通过Servlet的getServletConfig()就可以拿到了

在ServletConfig中提供了getServeltContext()方法,返回的是一个ServeltContext对象,也是通过方法拿到了ServeltContext对象,所以ServletConfig和ServeltContext也是关联的关系

依赖:依赖的那个为被依赖的参数

 

ServletConfig

1、作用:

就是拿取servlet的相关配置.也就是拿取web.xml里面的配置信息(这个配置信息都是一样的,所以无论哪个方法得到都是一样)

 

创建的时机:当在创建Servlet对象的时候,服务器已经帮我们创建了这个对象,并作为参数传递进来了

生命:作为init方法的形参,所以离开了该方法,就死亡了

延长它的生命:把它作为一个全局变量

2、获取ServeltConfig对象的方式

(1). 采用带参的init方法
(2). 采用servlet实例拿取(不要写带参的init方法)(推荐)

 


import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//演示如何获取servletConfig对象
/**
 * 获取的方式有两种:
 * 			1. 采用带参的init方法
 * 			2. 采用servlet实例拿取(不要写带参的init方法)  
 * @author Administrator
 *
 */
public class ServletConfig1 extends HttpServlet {
	
	ServletConfig config ;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config) ;
		this.config = config ;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletConfig sc = this.getServletConfig() ;
		//System.out.println(config == sc);
		System.out.println(sc);
		System.out.println(config == sc);
	}
	

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

}

 

在没有写带参的init方法的时候,默认调用父类的带参的init(),而在父类的init()中,已经帮我门把config给实例化了

 

如果我们自己去写带参的init方法的话,就不会去调用父类的带参的init方法了,就不会实例化,就会是null

 

3、拿取servlet的相关配置信息

在xml中使用<init-param>配置信息,通过servletConfig对象获取这些配置信息

xml:(注意:一个<init-param>只能配置一个键值对)

 

<servlet>
    <servlet-name>ServletConfig2</servlet-name>
    <servlet-class>com.example.servletconfig.ServletConfig2</servlet-class>
    <init-param>
    	 <param-name>name</param-name>
    	 <param-value>张无忌</param-value>
    </init-param>
     <init-param>
    	 <param-name>age</param-name>
    	 <param-value>20</param-value>
    </init-param>
  </servlet>

 

 

 

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//演示servletConfig对象的应用
public class ServletConfig2 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//拿到servletConfi对象
		ServletConfig sc = this.getServletConfig() ;

		//拿取配置的单个信息
//		String name = sc.getInitParameter("name") ;
//		System.out.println(name);
		
		//拿取配置的多个信息
		Enumeration<String> enu = sc.getInitParameterNames() ;
		while(enu.hasMoreElements()){
			String name = enu.nextElement() ;
			System.out.println(name + ":" + sc.getInitParameter(name));
		}
		
	}

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

}

servletContext

servlet上下文,一个全局对象,即工程对象,代表了一个应用

(就是建的项目,一个工程就代表一个servletContext,每个工程的servletContext对象是不一样的)

(1). 生命周期很长:

产生:当服务器启动,加载这个应用的时候,就创建了servletContext这个对象;

灭亡:当服务器关闭时或停止应用时

(2).每个web应用都有一个唯一的servletContext对象.

(3).在每个应用加载的时候,服务器就会创建servletContext对象。

(4).ServletContext对象是一个域对象(领域)

 

1、获取sevletContext对象的方式:
1. 采用servletConfig对象获取
2. 采用servlet实例对象获取(推荐)
3. 采用request对象获取

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//演示获取servletContext对象
/**
 * 有三种方式获取servletContext对象
 * 			1. 采用servletConfig对象获取
 * 			2. 采用servlet实例对象获取
 * 			3. 采用request对象获取
 * @author Administrator
 *
 */
public class ServletContext1 extends HttpServlet {
	
	ServletContext sc ; 
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config) ;
		sc = config.getServletContext() ;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//第二种方式
		ServletContext sc1 = this.getServletContext() ;
		System.out.println(sc);
		System.out.println(sc1 == sc);
		//第三种方式
		ServletContext sc2 = request.getSession().getServletContext() ;
		System.out.println(sc2 == sc);
	}

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

}

2.常用方法

存储一个键值对;set:存进去;get:取出来

 

 

获得在该对象里面所能够存储的所有的键:

 

根据路径,来获得路径所对应的servletContext对象:

 

拿到工程的路径:

 

获得初始化的参数:

 
获取真实路径,即在服务器上的路径
 
 

获得一个请求分发器:

 

获得资源文件:

 

从对象中删除属性:

 

3.应用

1.作为域对象来使用:传递数据

域对象:在底层有一个map集合

存:

 


import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//演示servletContext对象作为域对象使用
public class ServletContext2 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//获取全局对象
		ServletContext sc = this.getServletContext() ;
		
		//存储数据
		sc.setAttribute("name", "张三丰") ;
		System.out.println("数据存储完毕");
	}

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

}
取:

 

 


import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//从servletContext对象中拿取数据
public class ServletContext3 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//拿取全局对象
		ServletContext sc = this.getServletContext() ;
		
		//从sc中拿取数据
		String name = (String)sc.getAttribute("name") ;
		
		System.out.println(name);
		
	}

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

}
 

 

2.获取全局配置参数

配置全局参数:在<display-name></display-name>外配置,位置任意

使用<context-param>标签

 

<context-param>
  	 	<param-name>name</param-name>
  	 	<param-value>东西方不败</param-value>
  </context-param>
   <context-param>
  	 	<param-name>sex</param-name>
  	 	<param-value>人妖</param-value>
  	</context-param>

 

 


import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//获取全局配置参数
public class ServletContext4 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//拿到全局对象
		ServletContext sc = this.getServletContext() ;
		//获取单个配置参数(获取姓名)
//		String name = sc.getInitParameter("name") ;
//		System.out.println(name);
		
		//拿取多个配置参数的值
		Enumeration<String> enu = sc.getInitParameterNames() ;
		while(enu.hasMoreElements()){
			String name = enu.nextElement() ;
			System.out.println(name + ":" + sc.getInitParameter(name));
		}
		
	}

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

}
 

3.请求转发

 



import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//演示全局对象的请求转发
public class ServletContext5 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//拿到全局对象
		ServletContext sc = this.getServletContext() ;
		
		request.setAttribute("name", "乔峰") ;
 		
		//拿到请求转发器
		RequestDispatcher rd = sc.getRequestDispatcher("/servlet/ServletContext6") ;
		//转发过去
		rd.forward(request, response) ;
		
	}

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

}
转发到的页面:

 

 



import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContext6 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		System.out.println("你终于过来了");
		String name = (String)request.getAttribute("name") ;
		System.out.println("转发过来的数据: " + name);
	}

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

}
 

 

4.获取资源文件

获取资源文件有三种方式: 
1.采用 ServletContext对象获取 

优点: 任意文件,任意路径

缺点: 必须有web环境

2.采用ResourceBundle类来获取 

优点:简单方便

缺点: 

1.只能拿取properties文件 

2. 只能拿取非web环境下的资源
3.采用类加载器获取

优点: 任意文件,任意路径

缺点: 编写稍显麻烦
 

ResourceBundle类:

 

该类(抽象类)专门用来加载资源,还可以处理一些国际化的东西

 

 

类加载器:
一个java文件,编写好之后是源码,后缀名是.java,要将这个源码首先采用编译命令javac把其编译为.class文件,该.class文件位于硬盘上,在运行时,需要把.class文件加载到虚拟机里运行,就用类加载器来加载,类加载器的主要目的就是将字节码文件加载到内存里,然后运行字节码文件

 

获取类加载器的方式

1. 通过类名 :

ServletContext7.class.getClassLoader()

2. 通过对象:

this.getClass().getClassLoader()

3. Class.forName()

Class.forName("ServletContext7").getClassLoader()

 

 

package com.hcx.four;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//演示获取资源文件
/**
 * 获取资源文件有三种方式: 
 *              1.采用 ServletContext对象获取 
 * 				2.采用ResourceBundle类来获取 
 *  			3.采用类加载器获取
 *           
 *             第一种方式:优点: 任意文件,任意路径
 *             		       缺点: 必须有web环境
 *             第二种方式: 优点:简单方便
 *             		        缺点: 1.只能拿取properties文件 2. 只能拿取非web环境下的资源
 *             第三种方式: 优点: 任意文件,任意路径
 *             		       缺点: 编写稍显麻烦
 *             		  
 * 
 * @author Administrator
 * 
 */
public class ServletContext7 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//test11() ;
		// test12() ;
		// test13();
		// test22();
		// test31();
//		test32();
//		test33();
	//	test34();

	}

	// 采用 ServletContext对象获取p1资源文件的内容
	public void test11() {
		// 拿到全局对象
		ServletContext sc = this.getServletContext();

		// 获取p1.properties文件的路径
		String path = sc.getRealPath("/WEB-INF/classes/p1.properties");
		System.out.println(path);
		// 创建一个Properties对象
		Properties pro = new Properties();
		// 加载文件
		try {
			pro.load(new FileReader(path));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 读取k的值
		System.out.println(pro.get("k"));
	}

	// 采用 ServletContext对象获取p2资源文件的内容
	public void test12() {
		// 拿到全局对象
		ServletContext sc = this.getServletContext();

		// 获取p1.properties文件的路径
		String path = sc
				.getRealPath("/WEB-INF/classes/com/hcx/four/p2.properties");
		System.out.println(path);
		// 创建一个Properties对象
		Properties pro = new Properties();
		// 加载文件
		try {
			pro.load(new FileReader(path));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 读取k的值
		System.out.println(pro.get("k"));
	}

	// 采用 ServletContext对象获取p3资源文件的内容
	public void test13() {
		// 拿到全局对象
		ServletContext sc = this.getServletContext();

		// 获取p1.properties文件的路径
		String path = sc.getRealPath("/p3.properties");
		System.out.println(path);
		// 创建一个Properties对象
		Properties pro = new Properties();
		// 加载文件
		try {
			pro.load(new FileReader(path));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 读取k的值
		System.out.println(pro.get("k"));
	}

	// 采用resourceBunble拿取资源文件:获取p1资源文件的内容 默认路径是src,对用到web环境就是classes目录
	public void test21() {
		// 拿取ResourceBundle对象(专门用来获取properties文件的信息,所以不用加后缀名)
		ResourceBundle rb = ResourceBundle.getBundle("p1");//p1:路径
		// 拿取文件中的内容太
		System.out.println(rb.getString("k"));
	}

	// 采用resourceBunble拿取资源文件:获取p2资源文件的内容
	public void test22() {
		// 拿取ResourceBundle对象(专门用来获取properties文件的信息)
		ResourceBundle rb = ResourceBundle.getBundle("com.hcx.four.p2");
		// 拿取文件中的内容太
		System.out.println(rb.getString("k"));
	}

	// 采用类加载器拿取资源文件:获取p1资源文件的内容 : 默认路径是src,对用到web环境就是classes目录
	public void test31() {
		// 获取类加载器的方式
		/*
		 * 1. 通过类名 ServletContext7.class.getClassLoader() 
		 * 2. 通过对象
		 * this.getClass().getClassLoader() 
		 * 3. Class.forName()
		 * 获取Class.forName("ServletContext7").getClassLoader()
		 */
		InputStream in = this.getClass().getClassLoader()
				.getResourceAsStream("p1.properties");

		// 创建Properties对象
		Properties pro = new Properties();
		try {
			pro.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 拿取文件的数据
		System.out.println(pro.getProperty("k"));

	}

	// 采用类加载器拿取资源文件:获取p2资源文件的内容 : 默认路径是src,对用到web环境就是classes目录
	public void test32() {
		
		InputStream in = this.getClass().getClassLoader()
				.getResourceAsStream("com/hcx/four/p2.properties");

		// 创建Properties对象
		Properties pro = new Properties();
		try {
			pro.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 拿取文件的数据
		System.out.println(pro.getProperty("k"));

	}

	// 采用类加载器拿取资源文件:获取p3资源文件的内容 : 默认路径是src,对用到web环境就是classes目录
	public void test33() {
		
		InputStream in = this.getClass().getClassLoader()
				.getResourceAsStream("../../p3.properties");

		// 创建Properties对象
		Properties pro = new Properties();
		try {
			pro.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 拿取文件的数据
		System.out.println(pro.getProperty("k"));

	}
	
	// 采用类加载器拿取资源文件:获取p3资源文件的内容 : 默认路径是src,对用到web环境就是classes目录
	public void test34() {
		// 获取类加载器的方式
		/*
		 * 1. 通过类名 ServletContext7.class.getClassLoader() 2. 通过对象
		 * this.getClass().getClassLoader() 3. Class.forName()
		 * 获取Class.forName("ServletContext7").getClassLoader()
		 */
		URL url  = this.getClass().getClassLoader().getResource("p1.properties") ;
		
		String path = url.getPath() ;
				

		// 创建Properties对象
		Properties pro = new Properties();
		try {
			pro.load(new FileReader(path));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 拿取文件的数据
		System.out.println(pro.getProperty("k"));

	}
}
 

 

 
  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值