溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

什么是Servlet JSP的ServletConfig對象

發(fā)布時(shí)間:2020-07-16 10:41:43 來源:億速云 閱讀:184 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)什么是Servlet JSP的ServletConfig對象,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

                                                           ServletConfig對象有四個(gè)方法。

getInitParameter、 getInitParameterNames、 getServletName

(1)getInitParameter、 getInitParameterNames用于獲取Web.xml中的參數(shù)名、參數(shù)值。

(2)getServletName 獲取 Web.xml中的 Servlet-name。

實(shí)例

下面是Web.xml的文件內(nèi)容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
<servlet>
        <servlet-name>TestServletConfig</servlet-name>
        <servlet-class>com.djun.serveleMapping.TestServletConfig</servlet-class>
 
        <!--配置Servlet的初始化參數(shù)-->
        <!-- 如何獲取初始化的參數(shù)?
            1、getInitParameter(String name)
            Returns a String containing the value of the named initialization parameter,
            or null if the parameter does not exist.
            2、 getInitParameterNames()
            Returns the names of the servlet's initialization parameters as an Enumeration of String objects,
            or an empty Enumeration if the servlet has no initialization parameters.
        -->
        <init-param>
            <param-name>username</param-name>
            <param-value>admin</param-value>
        </init-param>
 
        <init-param>
            <param-name>passworld</param-name>
            <param-value>admin</param-value>
        </init-param>
        <!--
         指定Servlet JSP被創(chuàng)建的時(shí)機(jī)
         若數(shù)值 a<0,則僅在第一次的時(shí)候被創(chuàng)建。
         若 a>=0 , 則在當(dāng)前應(yīng)用被Servlet容器加載時(shí)創(chuàng)建實(shí)例
         數(shù)值越小越早被創(chuàng)建
      -->
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>TestServletConfig</servlet-name>
        <!--只要后綴為html的文件都由該類處理-->
        <url-pattern>/servletConfig</url-pattern>
    </servlet-mapping>
</web-app>
import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;
 
public class TestServletConfig implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("Init TestServletConfig...");
        System.out.println("-----------執(zhí)行g(shù)etInitParameter--------");
        String username = servletConfig.getInitParameter("username");
        String passworld = servletConfig.getInitParameter("passworld");
        System.out.println("username: " + username+"\n"+"password : "+passworld);
 
        System.out.println("----------執(zhí)行g(shù)etInitParameterNames------");
        Enumeration<String> names = servletConfig.getInitParameterNames();
 
        while(names.hasMoreElements()){
            String name = names.nextElement();
            String value = servletConfig.getInitParameter(name);
            System.out.println("username: " + name+"\n"+"password : "+value);
        }
        String servletName = servletConfig.getServletName();
        System.out.println(servletName);
    }
 
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
 
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("TestServletConfig....");
    }
 
    @Override
    public String getServletInfo() {
        return null;
    }
 
    @Override
    public void destroy() {
 
    }
}

getServletContext

(1)Servlet為每個(gè)Web應(yīng)用程序都創(chuàng)建了一個(gè)對應(yīng)的ServletContext對象,ServletContext對象被包含在ServletConfig對象中,通過調(diào)用 ServletContext.getServletContext()方法可以返回ServletContext對象的引用。

(2) 由于一個(gè)Web應(yīng)用程序中的所有Servlet都共享同一個(gè)ServletContext對象,所以,ServletContext對象被稱為application對象(也就是web應(yīng)用程序?qū)ο螅?/p>

(1) getRealPath()

獲取某一個(gè)文件在服務(wù)器上的絕對路徑,注意:并非是部署前的路徑。

注意我的下面文件存放的目錄

什么是Servlet JSP的ServletConfig對象(2) getContextPath()

獲取當(dāng)前Web應(yīng)用的某一個(gè)文件對應(yīng)的輸入流。

System.out.println("getContextPath() -----------");
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        String fileName = "application.properties";
        try {
            File file = new File(realPath+ "/" + fileName);
            ClassLoader classLoader = getClass().getClassLoader();
 
            InputStream is = classLoader.getResourceAsStream(realPath + "/" + fileName);
            System.out.println(realPath+ "/" + fileName);
 
            System.out.println("1. "+ is);
 
        } catch (Exception e) {
            e.printStackTrace();
        }

以上就是什么是Servlet JSP的ServletConfig對象,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI