Master Service
カスタマイズの 3 つのレベル つのレベル つのレベル つのレベル : 静的、半動的および動的 静的、半動的および動的 静的、半動的および動的 静的、半動的および動的
1 エンド・ユーザー設定項目の取得 エンド・ユーザー設定項目の取得 エンド・ユーザー設定項目の取得 エンド・ユーザー設定項目の取得
XMLを使用したコンテンツのカスタマイズ: Dynamic Newsアプリケーション 5-11
コンテンツのパーソナライズ コンテンツのパーソナライズ コンテンツのパーソナライズ コンテンツのパーソナライズ
Oracle9iにより、Dynamic Newsに柔軟性が加わります。ニュース項目はデータベースに格 納されるため、Dynamic Newsにより必要に応じてコンテンツをカスタマイズできます。こ の項のコード例は、エンド・ユーザーが指定したカテゴリのニュース項目をアプリケーショ ンが取り出し、ページをパーソナライズする方法を示しています。主要な作業は次のとおり です。
1. エンド・ユーザー設定項目を取得します。
2. データベースからニュース項目を取り出します。
3. ニュース項目を結合してドキュメントを構築します。
4. パーソナライズされたコンテンツに編集した後に、アプリケーションは後述のようにエ ンド・ユーザーのブラウザに合せてページをフォーマットし、表示をカスタマイズしま す。
1 エンド・ユーザー設定項目の取得 エンド・ユーザー設定項目の取得 エンド・ユーザー設定項目の取得 エンド・ユーザー設定項目の取得
設定項目を処理するロジックはアプリケーション全体に分散されており、データはデータ ベースとクライアント側Cookieの両方に格納されます。アプリケーションは、パフォーマ ンスを改善できる場合にCookieから設定項目データを読み込みます。 Cookieからデータを 取得できない場合(エンド・ユーザーがサイトに初めてアクセスした場合や、そのブラウザ
がCookieを受け入れない場合など)、アプリケーションは設定項目データをデータベースか
ら読み込みます。
クライアント側 クライアント側 クライアント側
クライアント側 Cookie から から から から
次の2つの方法は、アプリケーションがCookieに格納された設定項目データを処理する方 法を示しています。どちらの方法もxmlnews.common.UserPreferenceからの抜粋で、サン プルCookieは次のとおりです。
DynamicServlet=3$0$0#4$2$1***242
このCookieは、ドル記号を使用して設定項目の値を区切り、シャープ記号を使用してカテ
ゴリを区切り、3個のアスタリスクを個々のユーザーIDおよび設定項目データへのトーク ンとして使用しています。前述のサンプルCookieは、ユーザー242がカテゴリ3および4 の項目を要求していることを示します。カテゴリ3で、ユーザーは全サブカテゴリの全タイ プの項目を要求しています(値0の場合は、すべての項目が選択されます)。カテゴリ4で は、ユーザーはサブカテゴリ2の項目のうち、タイプ1の項目のみを要求しています。
サンプル・アプリケーションでは、このようなCookieは次の2段階で処理されます。
1. 最初に、getNewsCookieがHTTP要求を発行したブラウザから「DynamicServlet」 Cookieを取得します。
2. 次に、それをloadPreferenceFromCookieが解析し、ユーザーのIDと設定項目を 含む文字列を取得します。
1 エンド・ユーザー設定項目の取得
public Cookie getNewsCookie(HttpServletRequest request) throws Exception {
Cookie c[] = request.getCookies();
Cookie l_returnCookie = null;
for (int i = 0; (c!= null) && (i < c.length); i++) { if (c[i].getName().equals("DynamicServlet")) { l_returnCookie = c[i];
} }
return l_returnCookie;
}
public Vector loadPreferenceFromCookie(Cookie p_cookie) throws Exception { Vector l_prefId = new Vector(2);
String l_Preferences = p_cookie.getValue();
StringTokenizer l_stToken = new StringTokenizer(l_Preferences, "***");
String l_userId = "";
while (l_stToken.hasMoreTokens()) { // First Token is User Preference.
l_Preferences = l_stToken.nextToken();
// Second Token is User ID.
l_userId = l_stToken.nextToken();
}
l_prefId.addElement(l_Preferences);
l_prefId.addElement(l_userId);
return l_prefId;
}
データベースの問合せ データベースの問合せ データベースの問合せ データベースの問合せ
Cookieから設定項目を読み込めない場合、アプリケーションはデータベースに問合せしま
す。クラスxmlnews.common.GenUtilityでは、データベースに接続してニュースのカテ ゴリ、サブカテゴリおよびタイプをフェッチするメソッドが実装されます。
半動的サーブレットと動的サーブレットは、これらのメソッドとメソッド
loadInitalPreferenceおよびconstructUserPreferenceをコールします。どちら も、xmlnews/common/UserPreference.javaに実装されます。
メソッドloadInitalPreferenceはgetSubCategoriesをコールし、結果セット全体 をループし、カテゴリ値を区切り文字と結合して設定項目文字列を構築します。
public String loadInitialPreference(Vector p_category, Vector p_subcategory, Vector p_types, Connection p_con)
throws Exception {
GenUtility m_general = new GenUtility();
...
1 エンド・ユーザー設定項目の取得
XMLを使用したコンテンツのカスタマイズ: Dynamic Newsアプリケーション 5-13 Vector l_subcategory = m_general.getSubCategories(p_con,l_cat[0]);
for(int l_j = 0, l_k = 0; l_j < l_subcategory.size(); l_j++, l_k++) {
...
// Append the next preferences to the constructed string
l_userPref = l_userPref+"#"+l_category+"$"+l_subCat+"$"+l_typeStr;
} } ...
return l_userPref;
}
public static Vector getSubCategories(Connection p_conn, String p_categoryId) throws Exception {
Vector l_subCats = new Vector();
PreparedStatement l_pstmt = p_conn.prepareStatement(
"Select id, name from sub_categories where category_id = ? ");
l_pstmt.setString(1, p_categoryId);
ResultSet l_rset = l_pstmt.executeQuery();
while (l_rset.next()) {
String[] l_subCat = new String[2];
l_subCat[0] = new String(l_rset.getString(1));
l_subCat[1] = new String(l_rset.getString(2));
l_subCats.addElement(l_subCat);
}
l_pstmt.close();
return l_subCats;
}
たとえば、次のコードはxmlnews.dynamic.DynamicServlet.serviceから抜粋したも のです。
このコードは、前述のメソッドをコールしてデータベースからエンド・ユーザー設定項目を 読み込み、それを使用してHTMLページを構築します。
public void service(HttpServletRequest p_request, HttpServletResponse p_ response)
throws ServletException {
// The following are declared elsewhere as class variables // and initialized in the servlet's init method.
// GenUtility m_general = null;
// m_general = new GenUtility();
// UserPreference m_userPreference = null;