ServletConfig物件

SecondDream_1017發表於2018-08-15

原出處:https://www.cnblogs.com/HigginCui/p/5961082.html

【ServletConfig物件簡述】

在Servlet的配置檔案中,可以使用一個或多個<init-param>標籤為Servlet配置一些初始化引數。

當Servlet配置了初始化引數後,web容器在建立Servlet例項物件時,會自動將這些初始化引數封裝到ServletConfig物件中,並在呼叫Servlet的init方法的時候,將ServletConfig物件傳遞給Servlet,進而,我們同通過ServletConfig物件就可以得到當前Servlet的初始化引數的資訊。

 

【ServletConfig的API】

String getInitParameter(String name)       得到引數

Enumeration getInitParameterNames()      得到所有引數

ServletContext getServletContext()           得到上下文物件

String getServletName()                          得到Servlet名稱

 

【簡單案例----從init方法中獲取init-param中的引數值】

[ web.xml ]

<?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>HelloServlet</display-name>
 
  <servlet>
    <servlet-name>FirstServlet</servlet-name>
    <servlet-class>com.Higgin.servlet.FirstServlet</servlet-class>
    <init-param>
        <param-name>name</param-name>
        <param-value>Higgin</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>FirstServlet</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>

</web-app>

 

[ FirstServlet ]

package com.Higgin.servlet;

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 FirstServlet extends HttpServlet {
     
    private ServletConfig config; 
    
    public void init(ServletConfig config) throws ServletException {
        this.config=config;
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=config.getInitParameter("name"); //使用ServletConfig物件的getParameter(param-name)來獲取對應的value值
        System.out.println("name=="+name);
        System.out.println("doGet方法執行---");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
        System.out.println("doPost方法執行---");
    }

}

 

[ 執行結果 ]

 

【簡單案例2----獲取init-param中的 指定的引數值 和  所有的引數值】

[ web.xml ]

<?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>HelloServlet</display-name>
 
  <servlet>
    <servlet-name>FirstServlet</servlet-name>
    <servlet-class>com.Higgin.servlet.FirstServlet</servlet-class>
    <init-param>
        <param-name>name</param-name>
        <param-value>Higgin</param-value>
    </init-param>
    
    <init-param>
        <param-name>age</param-name>
        <param-value>18</param-value>
    </init-param>
    
    <init-param>
        <param-name>eamil</param-name>
        <param-value>12345@qq.com</param-value>
    </init-param>
    
    <init-param>
        <param-name>sex</param-name>
        <param-value>男</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>FirstServlet</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>

</web-app>

 

[ FirstServlet.java ]

 

package com.Higgin.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 FirstServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //得到指定的值
        String value =this.getServletConfig().getInitParameter("name");
        System.out.println("指定name----------"+value);
        
        //得到所有的
        Enumeration e=this.getServletConfig().getInitParameterNames();
        while(e.hasMoreElements()){
            String name=(String) e.nextElement();
            String value1=this.getServletConfig().getInitParameter(name);
            System.out.println("遍歷"+name+"-------"+value1);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
        System.out.println("doPost方法執行---");
    }

}

 

[ 執行結果 ]

 [ 提示 ]

實際開發中,有一些不適合在Servlet程式中寫死的模組,這些資料可以通過配置方式給Servlet,例如Servlet採用那種編碼、Servlet連線哪個庫、Servlet哪個配置檔案。

相關文章