什么是ServletJSP的ServletConfig對(duì)象-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)專注于崇禮企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。崇禮網(wǎng)站建設(shè)公司,為崇禮等地區(qū)提供建站服務(wù)。全流程定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

                                                           ServletConfig對(duì)象有四個(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è)對(duì)應(yīng)的ServletContext對(duì)象,ServletContext對(duì)象被包含在ServletConfig對(duì)象中,通過調(diào)用 ServletContext.getServletContext()方法可以返回ServletContext對(duì)象的引用。

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

(1) getRealPath()

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

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

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

獲取當(dāng)前Web應(yīng)用的某一個(gè)文件對(duì)應(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對(duì)象,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁名稱:什么是ServletJSP的ServletConfig對(duì)象-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://muchs.cn/article32/dhiesc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)、商城網(wǎng)站品牌網(wǎng)站建設(shè)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名