ServletContextListenerを実装するWebListenerを使用できます。
展開すると、システムのプロパティを読み取って属性として設定できます。
地図とは別に、または地図内にあります。
system.properties:
はあなたがコンテンツを持つファイルsystem.properties
を考えてみましょう:
buildType=myType
systemVersion=v55
WebListener:
package testingThings.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ContextListener implements ServletContextListener {
public ContextListener() {}
public void contextDestroyed(ServletContextEvent sce) {}
public void contextInitialized(ServletContextEvent sce) {
InputStream stream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("testingThings/properties/system.properties");
Properties props = new Properties();
try {
props.load(stream);
} catch (IOException e) {
e.printStackTrace();
}
HashMap<String, String> map = new HashMap<String, String>();
for (final String name : props.stringPropertyNames()) {
map.put(name, props.getProperty(name));
}
sce.getServletContext().setAttribute("system", map);
}
}
:
WebListenerのようなものかもしれません
JSP:
JSPで使用すると、このようなsystem
属性を反復処理することができます。システムのプロパティを動的に変更する場合
あなたが直接、コンテキスト属性に、それらを更新することができ、
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head><title>access map of system properties</title></head>
<body>
<h3>access map of system properties</h3>
<table>
<c:forEach items="${system}" var="property">
<tr>
<td>${property.key}:</td>
<td>${property.value}</td>
</tr>
</c:forEach>
<tr>
</table>
<h3>directly access of map properties</h3>
<table>
<tr>
<td>buildType:</td>
<td>${system.buildType}</td>
</tr>
<tr>
<td>systemVersion:</td>
<td>${system.systemVersion}</td>
</tr>
<tr>
</table>
</body>
(system.propertiesファイルに平行)
まあ、JSP *は*サーブレットです。しかし、それは宣言的なマークアップで作られていて、それに軽微なコード文を追加するのは一般的に嫌われているので、サーブレットの形ですべてのJSPからその情報を取得するための集中型(ステートレスな)アクセスすることができます。私は個人的には、サーブレットフィルタがどのようにスコープされるのかを見ることはできません。 – Mena
もう1つの方法はPropertiesTagを作成することです。これをJSPに組み込んでサーバープロパティをレンダリングすることができます。 – donlys
@donlys - あなたは詳しく説明できますか? –