Servlet复习

Servlet复习

一、ServletConfig

ServletConfig对象可以用来封装Servlet的初始化参数。在Web容器创建Servlet对象时,会将Servlet的初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。

1.1在web.xml中< init-param >标签下设置Servlet初始化参数:



  
  	servletConfigDemo1
  	servletReviewDemo1.servletConfigDemo1
  	
  	
        name
        clam
    
     
        password
        123
    
    
        charset
        UTF-8
    
  
  
  	servletConfigDemo1
  	/demo1
  

1.2在Servlet中接收ServletConfig对象:

package servletReviewDemo1;

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

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 当servlet配置了初始化参数后,web容器在创建servlet实例对象时
 * 会自动将这些初始化参数封装到ServletConfig对象中
 * 并在调用servlet的init方法时,将ServletConfig对象传递给servlet
 * 进而,我们通过ServletConfig对象就可以得到当前servlet的初始化参数信息
 *@author clam
 *
 */
public class servletConfigDemo1 extends HttpServlet {
	/**
	 * 定义ServletConfig对象
	 * 用来接收初始化参数
	 */
	private ServletConfig config;
	/**
	 * 接收初始化参数
	 */
	public void init(ServletConfig config){
		this.config = config;
	}
	/**
	 * 检验初始化参数的接收
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param1 = this.config.getInitParameter("name");
		/*
		 * 输出name的value值
		 */
		PrintWriter out = response.getWriter();
		
		out.println(param1);
		
		Enumeration params = this.config.getInitParameterNames();
		/*
		 * 输出全部参数的name和value值
		 */
		while(params.hasMoreElements()){
			String name = params.nextElement();
			out.println("name = "+ name);
			out.println("value = " + this.config.getInitParameter(name));
		}
	}
}

1.3网页查看输出结果:

Servlet复习_第1张图片

二、ServletContext

WEB容器在启动时,会为每个WEB应用程序都创建一个ServletContext对象,它代表当前的WEB程序,称context域。
一个WEB应用程序中的Servlet共用一个context域,所以可以用context域在不同的Servlet中共享数据。
ServletConfig中维护了ServletContext对象的引用,可以通过ServletConfig的getServletContext()方法获得context对象的引用,也可以直接使用Servlet的方法getServletContext()获得引用。
Servlet复习_第2张图片

2.1使用context域共享数据

在demo1中将数据存入context域中,在demo2中进行访问:
demo1中的代码:

package servletReviewDemo1;

import java.io.IOException;
import java.io.PrintWriter;

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;

/**
 * 在Demo1中创建数据
 * 使用ServletContext在Demo2中访问该数据
 *@author clam
 *
 */
public class servletContextDemo1 extends HttpServlet {
	/*
	 * data是在Demo2中将要访问的数据
	 */
	private String data = "This is clam";
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 获得ServletContext对象
		 * 使用ServletConfig中的getServletContext()方法
		 */
		ServletContext context = this.getServletConfig().getServletContext();
		/*
		 * 将data放入context域中
		 */
		context.setAttribute("data", data);
	}
}

demo2中的代码:

package servletReviewDemo1;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * 在Demo2中访问data数据
 *@author clam
 *
 */
public class servletContextDemo2 extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 获取ServletContext对象
		 * 使用Servlet中的getServletContext()方法
		 */
		ServletContext context = this.getServletContext();
		/*
		 * 获取data元素
		 */
		String data = (String) context.getAttribute("data");
		
		response.getWriter().println(data);
	}

}

2.2获取WEB应用的初始化参数

使用< context-param >标签配置WEB的初始化参数(< context-param >和< init-param >的区别):


    url
    jdbc:mysql://localhost:3306/test
  

在demo3中获取初始化参数:

package servletReviewDemo1;

import java.io.IOException;

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;

/**
 * 使用ServletContext获取项目初始化参数
 *@author clam
 *
 */
public class servletContextDemo3 extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 获取context对象
		 */
		ServletContext context = this.getServletContext();
		/*
		 * 获取初始化参数
		 */
		String value = (String) context.getInitParameter("url");
		response.getWriter().println("name = url");
		response.getWriter().println("value = " + value);
	}
}

2.3使用ServletContext实现请求转发:

使用ServletContext将demo4的请求转发至demo5
demo4中代码:

package servletReviewDemo1;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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;

/**
 * 使用ServletContext实现请求转发
 *@author clam
 *
 */
public class servletContextDemo4 extends HttpServlet {
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().println("This is demo4");
		
		ServletContext context = this.getServletContext();
		
		RequestDispatcher dispatcher = context.getRequestDispatcher("/contextdemo5");//获取请求转发对象
		
		dispatcher.forward(request, response);//请求转发
	}
}

demo5中代码:

package servletReviewDemo1;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class servletContextDemo5 extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().println("This is demo5");
	}
}

使用浏览器访问demo4时的效果:
Servlet复习_第3张图片

2.4使用ServletContext访问文件资源

文件目录信息:
Servlet复习_第4张图片
文件中的内容:
Servlet复习_第5张图片
使用demo6访问文件内容:

package servletReviewDemo1;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;

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;

/**
 * 使用ServletContext读取文件资源
 *@author clam
 *
 */
public class servletContextDemo6 extends HttpServlet {
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = this.getServletContext().getRealPath("/WEB-INF/classes/c3p0.properties");
		
		InputStream in = new FileInputStream(path);
		
		Properties prop = new Properties();
		prop.load(in);
		
		String driver = prop.getProperty("c3p0.driverClass");
		String url = prop.getProperty("c3p0.jdbcUrl");
		String user = prop.getProperty("c3p0.user");
		String password = prop.getProperty("c3p0.password");

		response.getWriter().println(
                MessageFormat.format(
                        "driver={0},url={1},user={2},password={3}", 
                        driver,url, user, password));
	}

}

浏览器访问demo6的效果:
Servlet复习_第6张图片
如有不足欢迎指出!

你可能感兴趣的:(Java学习,servlet,ServletConfig,ServletContext)