ServletConfig和ServletContext的应用

1、ServletConfig的对象可以得到当前的servlet文件的配置信息,这些配置信息都写在web.xml文件当中,通过this.getServletConfig();方法就可以得到当前文件的ServletConfig对象,通过getInitParameterNames()方法可以得到当前

servlet文件中的所有配置信息的name,然后通过getInitParameter(name)方法就可以读出其中的value。

ServletConfig config = this.getServletConfig();
		//得到当前存在于xml文件中的Servlet1的所有配置信息的名称
		Enumeration names = config.getInitParameterNames();
		//输出所有配置信息的名称以及value值
		while(names.hasMoreElements()) {
			String name = names.nextElement();
			String val = config.getInitParameter(name);
			System.out.println(name);
			System.out.println(val);
		}

web.xml



  firstWEB
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    
    Servlet1
    Servlet1
    cn.horizonhui.Servlet1
    
      Servlet1
      hello1
    
    
      Servlet1.1
      hello1.1
    
  
  
    Servlet1
    /Servlet1
  
  
    
    Servlet2
    Servlet2
    cn.horizonhui.Servlet2
  
  
    Servlet2
    /Servlet2
  

2、ServletConfig的对象还维护了一个ServletContext对象,此对象中包含了当前网站的配置信息,ServletContext的对象是域对象,其中的信息是以键值对的形式存在的,通过setAttribute(key,value)与getAttribute(key)方法就可以设置与读取该对象的信息,由于ServletContext对象保存的是当前网站的信息,所以可以利用该对象实现信息的共享,两个或多个servlet文件都可以访问该对象,也就是实现了信息共享。

3、利用ServletContext对象读取*.properties文件的内容,通过getRealPath(String path)方法可以得到文件的真实路径,其中传入的参数path是文件相对于该网站的相对路径。

a.properties

key=horizonhui
value=https://mp.csdn.net/postedit/83578181

servlet1.java

package cn.horizonhui;

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

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;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//创建一个ServletConfig对象
		ServletConfig config = this.getServletConfig();
		//得到当前存在于xml文件中的Servlet1的所有配置信息的名称
		Enumeration names = config.getInitParameterNames();
		//输出所有配置信息的名称以及value值
		while(names.hasMoreElements()) {
			String name = names.nextElement();
			String val = config.getInitParameter(name);
			System.out.println(name);
			System.out.println(val);
		}
		
		System.out.println("多个文件共享信息---------------------");
		//创建一个ServletContext对象
		ServletContext context = config.getServletContext();
		//向当前网站的配置信息中写入键值对
		context.setAttribute("name1", "123456");
				
		
	}

	/**
	 * @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);
	}

}

Servlet2.java

package cn.horizonhui;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

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

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//创建一个ServletContext对象
		ServletContext context1 = this.getServletConfig().getServletContext();
		//读取共享的内容
		System.out.println("servlet2------------");
		String val = (String)context1.getAttribute("name1");
		System.out.println("name1   "+val);
		
		//读取properties文件
		String realPath = context1.getRealPath("/WEB-INF/a.properties");
		Properties pros = new Properties();
		pros.load(new FileInputStream(realPath));
		System.out.println("key="+pros.getProperty("key"));
		System.out.println("value="+pros.getProperty("value"));
	}

	/**
	 * @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);
	}

}

 

输出现象

ServletConfig和ServletContext的应用_第1张图片

你可能感兴趣的:(Java,web)