[Java] context-param、ServletContext.getInitParameter()

這邊簡單介紹「全域初始化變數」、「取用方式」、「全域、區域、取用差別」

全域初始化變數:

web.xml
<context-param>
    <param-name>allowed file type</param-name>
    <param-value>.gif,.jpg,.bmp</param-value>
</context-param>


區域初始化變數:

web.xml
<servlet>
    <init-param>
        <param-name>admin</param-name>
        <param-value>admin</param-value>
    </init-param>
</servlet>


取用方式
全域初始化變數:

xxx.java
//取得所有參數的名稱、值(型別為 ServletContext):
ServletContext servletContext = getServletConfig().getServletContext(); 
//取得所有參數的名稱(型別為 Enumeration):
Enumeration<String> params = servletContext.getInitParameterNames();
//取參數值:
String allowedFileType = servletContext.getInitParameter("allowed file type");


區域初始化變數:

xxx.java
//取所有參數的名稱、值(型別為 Enumeration):
Enumeration<String> params = getInitParameterNames();
//取參數名:
String usernameParam = params.nextElement();
//取參數值:
String passwordParam = getInitParameter(usernameParam);
or String passwordParam = getInitParameter("admin");



一、程式結果
二、程式碼




一、程式結果



二、程式碼

Project 類型: Dynamic Web Project
Project 名稱: servlet
Package 名稱: idv.ycc.servlet
Class 類型: Servlet
Class 名稱: ContextParamServlet.java

ContextParamServlet.java
package idv.ycc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class ContextParamServlet
 */
@WebServlet("/ContextParamServlet")
public class ContextParamServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContextParamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  //設定request編碼
        response.setCharacterEncoding("UTF-8"); //設定response編碼
        response.setContentType("text/html");   //設定response內文類型
        
        PrintWriter out = response.getWriter(); //取得response傳送字元串給client的對象
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("<HEAD><TITLE>讀取全域初始化變數</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<div align=center><br/>");
        out.println("<fieldest style='width:90%'><legend>所有的全域初始化變數</legend><br/>");
        
        ServletContext servletContext = getServletConfig().getServletContext();         //取得所有全域初始化變數
        String uploadFolder = servletContext.getInitParameter("upload folder");         //取得全域初始化變數的值
        String allowedFileType = servletContext.getInitParameter("allowed file type");  //取得全域初始化變數的值
        Enumeration<String> params = servletContext.getInitParameterNames();
        
        out.println("<div>");
        out.println("<div align='left'>上傳文件夾</div>");
        out.println("<div align='left''>" + uploadFolder + "</div>");
        out.println("</div>");
        out.println("<div>");
        out.println("<div align='left'>實際磁碟路徑</div>");
        out.println("<div align='left'>" + servletContext.getRealPath(uploadFolder) + "</div>");
        out.println("</div>");
        out.println("<div>");
        out.println("<div align='left'>允許上傳的格式</div>");
        out.println("<div align='left'>" + allowedFileType + "</div>");
        out.println("</div>");
        out.println("</fieldset></div>");
        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();    //釋放緩衝區,待滿出未釋放會出現ERROR
        out.close();    //關閉PrintWriter,釋放所有系統資源
    }
}


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
         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_3_1.xsd">
<!-- 全域初始化變數 -->
<context-param>
        <param-name>upload folder</param-name>
        <param-value>attachment</param-value>
    </context-param>
    <context-param>
        <param-name>allowed file type</param-name>
        <param-value>.gif,.jpg,.bmp</param-value>
    </context-param>
    <servlet>
        <servlet-name>ContextParamServlet</servlet-name>
        <servlet-class>idv.ycc.servlet.ContextParamServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ContextParamServlet</servlet-name>
        <url-pattern>/contextparamservlet</url-pattern>
    </servlet-mapping>
</web-app>

留言

這個網誌中的熱門文章

[面試] 日月光 設備工程師

[日文] Google日文輸入法 簡單安裝說明

[Windows] 還我 win7 相片檢視器!!