servlet初始化参数的两种方法(1.配置web.xml文件 ,2.使用注释)

一、servlet初始化参数
使用标签初始化的参数是被应用程序中所有的servlet所共享。但是有时候我们需要为某一个特定的servlet配置参数,这个时候我们就需要使用servlet初始化参数。下边介绍两种servlet参数初始化的方法。

  • (1)配置web.xml文件
    在web.xml文件中为当前web应用添加两个名为admin-email和admin-tel的ServletContext初始化参数,在ConfigDemoServlet中获取这两个参数的参数值,使用ServletContext对象将admin-email和admin-tel的参数值写入日志文件中,并查看日志文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>homework01</display-name>
  <!-- 配置servlet -->
  <servlet>
  	<servlet-name>ConfigDemoServlet</servlet-name>
  	<servlet-class>com.configDemo.ConfigDemoServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>ConfigDemoServlet</servlet-name>
  	<url-pattern>/configDemo.do</url-pattern>
  </servlet-mapping>
  
  <!-- 配置整个web应用的初始化参数 -->
  <context-param>
  	<param-name>admin-email</param-name>
  	<param-value>123456@qq.com</param-value>
  </context-param>
  
  <context-param>
  	<param-name>admin-tel</param-name>
  	<param-value>1234567890</param-value>
  </context-param>
</web-app>
  • (2)使用注解完成servlet初始化参数
    编写ConfigDemoServlet程序,通过注释为其添加初始化参数email、telephone,在Servlet中读取该servlet的名称和初始化参数email、telephone,并将这两个参数的值在响应在页面上。
    • 下边代码所演示的是注释进行servlet的参数初始化:
@WebServlet(
		name = "ConfigDemoServlet",
		urlPatterns="/configDemo.do",
		initParams = {
				@WebInitParam(name= "email", value="telephone")	
		})

二、完整代码如下:

package com.configDemo;

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

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

/**
 * Servlet implementation class ConfigDemoServle
 * servlet的注释初始化
 */
@WebServlet(
		name = "ConfigDemoServlet",
		urlPatterns="/configDemo.do",
		initParams = {
				@WebInitParam(name= "email", value="telephone")	
		})

public class ConfigDemoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//ServletContext 的应用:作为整个web范围中的共享数据
	private ServletContext servletContext;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConfigDemoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		//获取servlet参数
		ServletConfig config = this.getServletConfig();
		Enumeration<String> enumeration = config.getInitParameterNames();
		String name = (String) enumeration.nextElement();//获取对应的值
		String value11 = config.getInitParameter(name);//利用名字获取值
		
		//将信息打印在界面上
		response.getWriter().println("servlet名称 : "+ config.getServletName());
		response.getWriter().println("

"
); response.getWriter().println("Email 地址: "+ name); response.getWriter().println("

"
); response.getWriter().println("电话号码:" + value11); response.getWriter().println("

"
); //获取web.xml文件中的初始化参数值,并且显示在页面上 this.servletContext = config.getServletContext(); String adminEmail = servletContext.getInitParameter("admin-email");//利用名字获取值 String adminTel = servletContext.getInitParameter("admin-tel");//利用名字获取值 response.getWriter().println("web.xml文件中的初始化参数值admin-email: " + adminEmail); response.getWriter().println("

"
); response.getWriter().println("web.xml文件中的初始化参数值admin-tel: " + adminTel); } /** * @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); } }

三、页面显示结果
servlet初始化参数的两种方法(1.配置web.xml文件 ,2.使用注释)_第1张图片

你可能感兴趣的:(JAVA,Web)