java——servletConfig的作用

一、在xml配置中添加参数,具体代码
package com.itheima.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;

public class HelloServletConfig extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//ServletConfig 可以获取servlet在配置的一些信息
		
		//1、得到servlet配置对象
		ServletConfig config = getServletConfig();
		
		//获取到的是配置servlet里面servlet-name的文本内容
		String servletName = config.getServletName();
		System.out.println("servletName: "+servletName);
		
		//2.可以获取具体的某一个参数
		String address = config.getInitParameter("address");
		System.out.println("address"+address);
		
		System.out.println("----------------------");
		//3.获取所有的参数名称
		Enumeration<String> names = config.getInitParameterNames();
		//遍历所有的参数名称
		while (names.hasMoreElements()) {
			String key = (String) names.nextElement();
			String value = config.getInitParameter(key);
			System.out.println("key="+key+"  value="+value);
		}
	}
	
	//来了post请求,就让它去执行doGet方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

对应的xml配置


<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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>HelloWebdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>

  <servlet>
  	<servlet-name>HelloServletConfigservlet-name>
  	<servlet-class>com.itheima.servlet.HelloServletConfigservlet-class>
  	
  	
  	<init-param>
  		<param-name>addressparam-name>
  		<param-value>beijing...param-value>
  	init-param>
  	<init-param>
  		<param-name>nameparam-name>
  		<param-value>zhangsan...param-value>
  	init-param>
  	
  servlet>
  
  <servlet-mapping>
  	<servlet-name>HelloServletConfigservlet-name>
  	<url-pattern>/HelloServletConfigurl-pattern>
  servlet-mapping>
  
  
  
web-app>

在浏览器输入路径,如http://localhost:8080/HelloWeb/HelloServletConfig,效果图
java——servletConfig的作用_第1张图片

二、在xml配置中不添加参数
package com.itheima.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;

public class HelloServletConfig extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//1.获取该servlet配置的参数
		ServletConfig config = getServletConfig();
		String number = config.getInitParameter("number");
		if (number == null) {
			throw new IllegalArgumentException("servlet在配置的时候一定要添加number这个参数");
		}else {
			System.out.println("number...");
		}
	}
	
	//来了post请求,就让它去执行doGet方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

xml配置


<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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>HelloWebdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>

  <servlet>
  	<servlet-name>HelloServletConfigservlet-name>
  	<servlet-class>com.itheima.servlet.HelloServletConfigservlet-class>
  	
  	
  	<init-param>
  		<param-name>addressparam-name>
  		<param-value>beijing...param-value>
  	init-param>
  	<init-param>
  		<param-name>nameparam-name>
  		<param-value>zhangsan...param-value>
  	init-param>
  	
  servlet>
  
  <servlet-mapping>
  	<servlet-name>HelloServletConfigservlet-name>
  	<url-pattern>/HelloServletConfigurl-pattern>
  servlet-mapping>
  
  
  
web-app>

在浏览器输入路径是会打印异常信息
java——servletConfig的作用_第2张图片
控制台输出异常信息
java——servletConfig的作用_第3张图片
当你在xml配置中添加number的时候就不会抛出异常了

<init-param>
  		<param-name>numberparam-name>
  		<param-value>18param-value>
  	init-param>

正在尝试写博客,把会的分享给你们,如有写的不好的地方,希望指点一下,喜欢的朋友们请点个赞,谢谢!

你可能感兴趣的:(#,JavaWeb)