プロが教えるわが家の防犯対策術!

Contextとは何でしょうか?

HttpServletを継承するクラス(例えば、DoSomethingServlet)を定義する中で、しばしば getServletContext() の呼び出しにより、ServletContextインターフェイス型オブジェクト(以下、オブジェクトSCと略)への参照を得ますが、このオブジェクトSCとはなんなのか?、そもそもServletの世界においてContextとは一体なんなのか?、どうもピンと来ません。

http://java.sun.com/javase/6/docs/api/

javax.servlet.http
Class HttpServlet
Methods inherited from class javax.servlet.GenericServlet
getServletContext


javax.servlet
Class GenericServlet
getServletContext()
Returns a reference to the ServletContext in which this servlet is running.
(オブジェクトSCは、その中で、DoSomethingServletクラス型オブジェクトが実行中の、ServletContextインターフェイス型オブジェクトのことである。)


javax.servlet
Interface ServletContext
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
(オブジェクトSCは、DoSomethingServletクラス型オブジェクトが、そのコンテナ(Tomcatなど)にメッセージを送るためのメソッドをまとめたオブジェクトである。)


javax.naming
Interface Context
This interface represents a naming context, which consists of a set of name-to-object bindings. It contains methods for examining and updating these bindings.
(一般に、Context(インターフェイス)型オブジェクトとは、binding(名前とオブジェクトの関連)をまとめたオブジェクトである。)


JAVA ENTERPRIZE IN A NUTSHELL, Chap-6
http://docstore.mik.ua/orelly/java-ent/jenut/ch0 …
A naming service associates names with objects. An association between a name and an object is called a binding, and a set of such bindings is called a context. A name in a context can be bound to another context that uses the same naming conventions; the bound context is called a subcontext. For example, in a filesystem, a directory (such as /temp) is a context that contains bindings between filenames and objects that the system can use to manipulate the files (often called file handles). If a directory contains a binding for another directory (e.g., /temp/javax), the subdirectory is a subcontext.
(contextは、サブcontextを持つことがある。サブcontextは、contextである。
 → 任意のcontextに注目したとき、そのcontextは、親や子を持つことがある。)


以上、よろしくお願いします。

A 回答 (3件)

レイア間のやり取りをするための空間をコンテキストと呼んでいるだけ。


ServletContextはそれをJavaで表現したものよ。
    • good
    • 6

難しいわね。


そもそもの定義があいまいだから。

これにしか、こういうときにしか使っちゃいけない
という定義がないから。

日本語で無理やりに訳すなら
層間の情報群とかそんな言葉になるんじゃないかしら。

正直こういうものだっていう概念で理解するしかないわね。

この回答への補足

Javaの package の日本語訳は パッケージ ですが、その定義は、Wikipedia に書かれているとおりです。
Servletの Context の日本語訳は コンテキスト のままでよいと思いますが、その定義はどのようになるのでしょうか?

補足日時:2010/05/11 20:53
    • good
    • 0

Contextを単純に説明すると


「情報の塊」
てところね。
Javaではレイヤ間でデータをやり取りする際の
入れ物をContextとして名づけることが多いわ。

ServletContextはServletがServletコンテナとやり取りする際に利用する入れ物よ。
ServletコンテナていうのはServletの実行環境のことだと思ってくれていいわ。
ServletContextが保持している情報にはweb.xmlの値があったり
パスの通っているところに置かれているファイルを参照するための情報をもっていたりするわね。

この回答への補足

ご回答ありがとうございます。

「context」を、曖昧さが残らないように正確に表現すると、どのようになるのでしょうか?

補足日時:2010/05/10 19:38
    • good
    • 4

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!