获取servlet配置信息

导读:本篇文章讲解 获取servlet配置信息,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

servletconfig.java代码:

package com.itcast.servletConfig;

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;

public class ServletConfigDemo1 extends HttpServlet {

	
	/*private ServletConfig config;

	public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}*/

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        //第一种方式获取配置信息
		/*String encoding = config.getInitParameter("encoding");//获得配置文件中的信息的
		System.out.println(encoding);*/
		//第二种方式或者配置信息
//		String encoding = this.getServletConfig().getInitParameter("encoding");
//		System.out.println(encoding);
		//第三种方式获取配置信息
		String encoding = super.getInitParameter("encoding");
		System.out.println(encoding);
	}

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

}

配置文件代码:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <servlet>
    <servlet-name>servletconfig</servlet-name>
    <servlet-class>com.haidi8.servletconfig.servletconfig</servlet-class>
    <init-param>
    	<param-name>encoding</param-name>
    	<param-value>GBk</param-value>
    </init-param>
  </servlet>


 <!--  创建一个客户端可以访问的映射 -->
  <servlet-mapping>
    <servlet-name>servletconfig</servlet-name>
    <url-pattern>/servletconfigdemo1</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

运行结果:

获取servlet配置信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94189.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!