1. 程式人生 > >有關Servlet初始化引數的獲取方法

有關Servlet初始化引數的獲取方法

昨天做了一道有關servlet的面試題,題目如下:

  1. Servlet

  2. ServletContext

  3. ServletConfig(正確答案)

  4. GenericServlet

我當時選的是第二個ServletContext,後面查閱資料大致弄明白了,這裡記錄一下,希望對大家有所幫助。

先說說兩個引數

  1. context-param:元素含有一對引數名和引數值,用作應用的ServletContext上下文初始化引數。引數名在整個Web應用中必須是惟一的。

  2. init-param:相應的某個Serlet的初始化引數

然後講講ServletContext和ServletConfig物件,下面是我個人的看法

  1. ServletConfig物件是當前Servlet的物件,就是在當前Servlet執行init初始化方法之後產生的,是屬於當前servlet的物件。

  2. ServletContext物件是web應用上下文物件,是web伺服器啟動之後就產生了的,因此在每個servlet裡面都可以通過getServletConfig().getServletContext()拿到ServletContext物件

經過以上分析,相信大家應該大致清楚了吧,這裡總結一下

1. init-param引數可以通過ServletConfig物件的getInitParameter()方法獲取到

2. context-param引數可以通過ServletContext物件的getInitParameter()方法獲取到

對此我還寫了個測試程式,程式碼如下:

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <context-param>
    <param-name>context_param</param-name>
    <param-value>context_param_value</param-value>
  </context-param>
  <servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.servlet.Test</servlet-class>
<init-param>
    <param-name>init_param</param-name>
    <param-value>init_param_value</param-value>
</init-param>
  </servlet>

  <servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

Test.java

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author phn
 * @date 2015-4-9
 * @TODO
 */
public class Test extends HttpServlet {

    public Test() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String context_param = getServletConfig().getInitParameter(
                "context_param");
        String context_param1 = getServletConfig().getServletContext()
                .getInitParameter("context_param");
        String context_param2 = getServletContext()
                .getInitParameter("context_param");

        String init_param = getServletConfig().getInitParameter("init_param");
        String init_param1 = getServletConfig().getServletContext().getInitParameter("init_param");
        String init_param2 = getServletContext().getInitParameter("init_param");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("getServletConfig().getInitParameter(context_param) : " + context_param);
        out.println("<br/>");
        out.println("<br/>");
        out.println("getServletConfig().getServletContext().getInitParameter(context_param) : " + context_param1);
        out.println("<br/>");
        out.println("<br/>");
        out.println("getServletContext().getInitParameter(context_param) : " + context_param2);
        out.println("<br/>");
        out.println("<br/>");
        out.println("getServletConfig().getInitParameter(init_param) : " + init_param);
        out.println("<br/>");
        out.println("<br/>");
        out.println("getServletConfig().getServletContext().getInitParameter(init_param) : " + init_param1);
        out.println("<br/>");
        out.println("<br/>");
        out.println("getServletContext().getInitParameter(init_param) : " + init_param2);
        out.flush();
        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        doGet(request, response);
    }

    public void init() throws ServletException {
    }

}

之後這裡是在瀏覽器顯示的結果: