ServletContextの役割

ServletContext

Webコンテナが開始されると、現在のWebアプリケーションを表す各Webアプリケーションに対応するServletContextオブジェクトが作成されます。

ここに画像の説明を挿入

ServletContextは、サーブレットの上にあるオブジェクトです。

1.共有データを実現します。

まず、S1を介して共有データをServletContextに保存します。

public class Servlet1 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        ServletContext context = getServletContext();
        context.setAttribute("message","Hello Servlet");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().println("添加到context成功");
    }
}

次に、S2で、共有データをServletContextから取り出すことができます

public class Servlet2 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        ServletContext context = getServletContext();
        String val = (String)context.getAttribute("message");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().println(val);
    }
}

それらの構成は次のとおりです。

      <servlet>
        <servlet-name>s1</servlet-name>
        <servlet-class>Servlet1</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>s2</servlet-name>
        <servlet-class>Servlet2</servlet-class>
      </servlet>
  <servlet-mapping>
    <servlet-name>s1</servlet-name>
    <url-pattern>/s1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>s2</servlet-name>
    <url-pattern>/s2</url-pattern>
  </servlet-mapping>

最後に、/ s1、次に/ s2にアクセスして、寄託されたメッセージを取得します。

2.初期化パラメータを取得します

まず、でweb.xmlいくつかの初期化パラメーターを構成してから、ServletContextのgetInitParameter()メソッドを使用して初期化パラメーターの値を取得できます。

<context-param>
        <param-name>url</param-name>
        <param-value>http://www.baidu.com</param-value>
</context-param>
public class Servlet3 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        ServletContext context = getServletContext();
        String s = context.getInitParameter("url");
        System.out.println(s);
    }
}

3.リクエスト転送を実現します

リクエスト転送とリクエストリダイレクトの違いについてお話ししましょう。

写真を見てください:

ここに画像の説明を挿入

フォワードAがBを要求すると、BはAがCのリソースを必要としていることを検出したため、Bは要求をCに転送し、Cは処理後にBに戻り、Bは再びAに戻ります。このプロセスでは、AとBがそれぞれリクエストを送信します。

リダイレクトAがBを要求し、BがAのリソースがCにあることを検出したので、Cにリソースが必要であることをAに伝えてから、AがCに要求を送信します。このプロセスAは2つの要求を送信しました。(したがって、AのリクエストURLアドレスは変更されます)

ServletContextを介したリクエスト転送を実現します。

public class Servlet3 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        ServletContext context = getServletContext();
        context.getRequestDispatcher("/s2").forward(req,resp);
    }
}

getRequestDispatcher()リクエストの転送パスを取得することによりforward()メソッド呼び出しを介して転送されます。

4.リソースファイルを読み取ります

プロパティ

  • Javaディレクトリに新しいプロパティを作成します
  • リソースディレクトリに新しいプロパティを作成します

見つかった:、都被打包到了同一个路径下:classes私たちは一般的にこのパスを呼び出しますclasspath

まず、ファイルストリームが必要です。

username=root
password=510781

次に、ServletContextを介してリソースファイルを読み取ります。

public class Servlet4 extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        ServletContext context = getServletContext();
        InputStream in = context.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(in);
        String user = prop.getProperty("username");
        String pwd = prop.getProperty("password");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.getWriter().println(user+":"+pwd);
    }
}

上記のコードでServletContextは、getResourceAsStream()メソッドを使用してリソースファイルをロードすることに注意してください。

パスに注意してください。WEBサーバー(Tomcat)の場合、パスの先頭にある「/」は現在のプロジェクトの基礎を表します。

[外部リンク画像の転送に失敗しました。ソースサイトにヒル防止リンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-I4dvu9jq-1616852250176)(ServletContext / image-20210327213238051.png)]

ServletContext`のgetResourceAsStream()メソッドは、リソースファイルをロードできます。

パスに注意してください。WEBサーバー(Tomcat)の場合、パスの先頭にある「/」は現在のプロジェクトの基礎を表します。

ここに画像の説明を挿入

それがS1-1.0-.....その中の道です。次に、以下のパスはすべて、クラシックに従ってプロジェクトに従って計算されます。など、ソースコード内のリソースファイルの場所ではなく、展開およびパッケージ化後に、リソースファイルがプロジェクトの対応するパスに含まれていることを確認してください

おすすめ

転載: blog.csdn.net/qq_40596572/article/details/115272339