ServletContextとは:
- ServletContextはインターフェースであり、サーブレットコンテキストオブジェクトを表します。
- Webプロジェクトには、ServletContextオブジェクトインスタンスが1つだけあります
- サーブレットオブジェクトはドメインオブジェクトです
- ServletContextは、Webプロジェクトのデプロイメントの開始時に作成され、Webプロジェクトの停止時に破棄されます
ドメインオブジェクト:ドメインオブジェクトと呼ばれる、マップなどのデータにアクセスできるオブジェクト
ここでのドメインとは、データにアクセスするための操作範囲(Webプロジェクト全体)を指します。
データを保存する | データを取得する | データを削除する | |
---|---|---|---|
地図 | put() | 取得する() | 削除する() |
ドメインオブジェクト | setAttribute() | getAttribute() | removeAttribute() |
ServletContextクラスの4つの関数:
- web.xmlで設定されたコンテキストパラメータcontext-paramを取得します
- 現在の作業パスを取得します。形式:/ working path
- プロジェクトが展開された後、サーバーのハードディスク上の絶対パスを取得します
- 地図などのデータにアクセスする
web.xmlで構成されたコンテキストパラメーターcontext-paramを取得します。
最初にweb.xmlを構成し、それに次のコンテンツを追加します。
<!-- Context-param是上下文参数(它属于整个web工程,只要是在这个web工程中的,像Servlet 程序、Filter 过滤器、Listener 监听器,都可以得到这些参数) -->
<!-- context-param根据需要,可以配置多组 -->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>
ContextServlet.javaのdoGetメソッド:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 获取web.xml中配置的上下文参数context-param
ServletContext context = getServletConfig().getServletContext(); //首先,通过ServletConfig获取到ServletContext对象
String username = context.getInitParameter("username");
System.out.println("context-param参数的username的值是:"+username);
String password = context.getInitParameter("password");
System.out.println("context-param参数的password的值是:"+password);
}
次に、次のサイトにアクセスしてください。
http://localhost:8080/06_servlet1_war_exploded/contextServlet
演算結果:
注:
このメソッドを使用すると、init-paramのコンテンツを取得できません。
init-paramは、ServletConfigによってのみ取得できます。
context-paramは、ServletContextオブジェクトによってのみ取得できます。
現在の作業パスを取得します。形式:/作業パス:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 2. 获取当前的工作路径,格式:/工作路径
System.out.println("当前工程路径:" + context.getContextPath());
}
演算結果:
結果は次のとおりです。
プロジェクトが展開された後、サーバーのハードディスク上の絶対パスを取得します。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 3. 获取工程部署后在服务器硬盘上的绝对路径
/*
/ 斜杠被服务器解析地址为:http://ip:port/工程名/
*/
System.out.println("工程部署的路径是:" + context.getRealPath("/"));
}
演算結果:
このパスにアクセスします:(これは、モジュール内のWebディレクトリに対応します。これについては後で説明します)
再起動しましょう:
パスを取得します。
C:\Users\Point\.IntelliJIdea2019.3\system\tomcat\Tomcat_9_0_37_Javaweb
これは、IDEAがTomcatを統合した後のTomcatのコピーコンテンツです。
このパスにアクセスします。
06_servlet1_war_exploded.xml構成ファイルは次のとおりです。
前の06_servlet1_war_explodedは、作成したプロジェクトのプロジェクトパスであり、作成したモジュールのWebディレクトリに対応します(上記を参照:プロジェクトの展開後にサーバーのハードディスクの絶対パスを取得します)。
Webはプロジェクトとしてコピーされ、過去にその名前が06_servlet1_war_explodedに変更されました。
WEB-INFのclassesフォルダーのsrcにコンパイルされたバイトコードがいくつかあります。
結論として:
/ 斜杠被服务器解析地址为:http://ip:port/工程名/
このプロジェクト名は、ディスク上のC:\ Users \ Point \ IdeaProjects \ Javaweb \ out \ artifacts \ 06_servlet1_war_exploded \に対応します。
ディスク上のこの場所は、IDEAのWebディレクトリに対応しています。
つまり、IDEAコードのWEBディレクトリにマップされます
これから、Webディレクトリ内のファイルの絶対パスを取得することもできます。
ファイルと画像を展開します。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("工程部署的路径是:" + context.getRealPath("/"));
System.out.println("工程下css目录的绝对路径是:" + context.getRealPath("/css"));
System.out.println("工程下imgs目录的1.jpg绝对路径是:" + context.getRealPath("/css/1.jpg"));
}
マップなどのデータにアクセスします。
ServletContextオブジェクトを呼び出す簡単な方法は次のとおりです。
getServletContextメソッドを直接呼び出します。
ServletContext context = getServletContext();
getServletContextメソッドはGenericServletで作成されます:
最初にServletConfigオブジェクトを呼び出し、次にGenericServletを呼び出すServletContext
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取ServletContext对象方法一:getServletConfig().getServletContext();
//获取ServletContext对象方法二:直接调用getServletContext()
ServletContext context = getServletContext();
System.out.println("保存之前:ServletContext1 中获取数据域key1的值是:" + context.getAttribute("key1"));
context.setAttribute("key1","value1");
System.out.println("ServletContext1 中获取数据域key1的值是:" + context.getAttribute("key1"));
}
ServletContext2を作成します。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("ServletContext2 中获取数据域key1的值是:" + context.getAttribute("key1"));
}
- 最初にcontextServlet1にアクセスし、次にcontextServlet1に1回アクセスします。
-
再デプロイ(再デプロイ)して、contextServlet1に再度アクセスします。
-
サーバーを再起動し(サーバーの再起動)、contextServlet1に再度アクセスします。
- contextServlet2にアクセスします:
目に見える:
-
ServletContextは、Webプロジェクトの展開が開始されると作成され、Webプロジェクトが停止すると破棄されます。
-
データにアクセスするための操作範囲(Webプロジェクト全体)
展開(再展開):
最初に元のプロジェクトを停止してから、プロジェクト全体を開始します。つまり、サーバーを再起動する必要はなく、プロジェクトを再起動します。
サーバーを再起動する場合(サーバーの再起動):
元のServletContextデータはもう存在しないためServletContext context = getServletContext()
、前の保存之前:ServletContext1 中获取数据域key1的值
データを取得する前に、最初に取得する必要があります。
Webプロジェクトには、ServletContextオブジェクトインスタンステストが1つだけあります。
contextServlet1とcontextServlet2にアクセスする場合、取得されるコンテキストオブジェクトのアドレスは同じです。