<%--
Copyright (c) 2002 by Phil Hanna
All rights reserved.
You may study, use, modify, and distribute this
software for any purpose provided that this
copyright notice appears in all copies.
This software is provided without warranty
either expressed or implied.
--%>
<%@ page session="false"
import="java.io.*,
java.net.*,
java.util.*" %>
<html>
<head>
<title>Using Cookies to Store Preferences</title>
<style><%= STYLESHEET %></style>
</head>
<body>
<table border="0" cellspacing="3" width="500">
<%-- Language preference bar --%>
<tr><td class="LB">
<%= getLanguageBar(request) %>
</td></tr>
</table>
<%-- Localized greeting --%>
<h1><%= getGreeting(request) %></h1>
<%-- Store language preference in a persistent cookie --%>
<% storeLanguagePreferenceCookie(request, response); %>
</body>
</html>
<%!
// ===========================================
// Helper methods included here for
// clarity. A better choice would be
// to put them in beans or a servlet.
// ===========================================
/**
* The CSS stylesheet
*/
private static final String STYLESHEET =
"h1 { font-size: 130%; }\n"
+ ".LB {\n"
+ " background-color: #005A9C;\n"
+ " color: #FFFFFF;\n"
+ " font-size: 90%;\n"
+ " font-weight: bold;\n"
+ " padding: 0.5em;\n"
+ " text-align: right;\n"
+ " word-spacing: 1em;\n"
+ "}\n"
+ ".LB a:link, .LB a:active, .LB a:visited {\n"
+ " text-decoration: none;\n"
+ " color: #FFFFFF;\n"
+ "}\n";
/**
* Creates the language preference bar
*/
private String getLanguageBar
(HttpServletRequest request)
throws IOException
{
String thisURL = request.getRequestURL().toString();
StringBuffer sb = new StringBuffer();
appendLink(sb, thisURL, Locale.ENGLISH);
appendLink(sb, thisURL, Locale.GERMAN);
appendLink(sb, thisURL, Locale.FRENCH);
appendLink(sb, thisURL, Locale.ITALIAN);
String languageBar = sb.toString();
return languageBar;
}
/**
* Helper method to create hyperlinks
*/
private void appendLink
(StringBuffer sb, String thisURL, Locale locale)
throws UnsupportedEncodingException
{
if (sb.length() > 0)
sb.append(" ");
String language = locale.getLanguage();
sb.append("<a href=\"");
sb.append(thisURL);
sb.append("?language=");
sb.append(URLEncoder.encode(language, "UTF-8"));
sb.append("\">");
sb.append(locale.getDisplayName(locale));
sb.append("</a>\n");
}
/**
* Gets the greeting message appropriate for
* this locale
*/
private String getGreeting
(HttpServletRequest request)
{
Locale locale = getLocaleFromCookie(request);
ResourceBundle RB = ResourceBundle.getBundle
("welcome", locale);
String greeting = RB.getString("greeting");
return greeting;
}
/**
* Determines the locale to use, in the following
* order of preference:
*
* 1. Language parameter passed with request
* 2. Language cookie previously stored
* 3. Default locale for client
* 4. Default locale for server
*/
private Locale getLocaleFromCookie
(HttpServletRequest request)
{
Locale locale = null;
String language = request.getParameter("language");
if (language != null)
locale = new Locale(language);
else {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
String name = cookie.getName();
if (name.equals("language")) {
language = cookie.getValue();
locale = new Locale(language);
break;
}
}
}
if (locale == null)
locale = request.getLocale();
}
return locale;
}
/**
* Stores the language preference
* in a persistent cookie
*/
private void storeLanguagePreferenceCookie
(HttpServletRequest request, HttpServletResponse response)
{
Locale locale = getLocaleFromCookie(request);
String name = "language";
String value = locale.getLanguage();
Cookie cookie = new Cookie(name, value);
final int ONE_YEAR = 60 * 60 * 24 * 365;
cookie.setMaxAge(ONE_YEAR);
response.addCookie(cookie);
}
%>
|