To create a locale-aware set of web pages, the choices are
Accept-language
HTTP header
hello.var
URI: hello
URI: en/hello.html
Content-type: text/html; charset=iso-8859-1
Content-language: en
URI: fr/hello.html
Content-type: text/html;charset=iso-8859-1
Content-language: fr
en
and fr
..../hello.var
..../en/hello.html
..../fr/hello.html
hello.html
is
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Draft//EN">
<html> <head>
<title>Hello</title>
</head>
<body>
<h1>Hello in English</h1>
</body>
</html>
hello.html
is
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Draft//EN">
<html> <head>
<title>Bonjour</title>
</head>
<body>
<h1>Bonjour en Francais</h1>
</body>
</html>
<Directory "/home/httpd/html/i18n/webchoices/server-multiviews">
Options MultiViews
</Directory>
or by having a file .htaccess
in the directory
with contents
options MultiViews
.htaccess
hello.en.html
hello.fr.html
.html
extension!
Accept-Language
header and
return a preferred locale or a list of all locales
hello
and fr_FR
it could create a name of
fr_FR/hello.jsp
package jan;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
public class ServletForwardNoCheck extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Locale preferredLocale = request.getLocale();
RequestDispatcher dispatcher = request.
getRequestDispatcher("/jsp/" + preferredLocale.toString() +
"/hello.jsp");
PrintWriter out = response.getWriter();
if (dispatcher != null) {
out.println("Dispatcher not null " + dispatcher.toString());
try {
dispatcher.forward(request, response);
} catch (Exception e) {
out.println("JSP Not found" + e.toString());
}
} else {
out.println("Dispatcher is null");
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
ServletContext.getNamedDispatcher()
which uses the
name of a JSP
web.xml
file in WEB-INF
can map names
to JSP compiled classes
<servlet>
<servlet-name>hello_fr_jsp</servlet-name>
<servlet-class>org.apache.jsp.jsp.hello_fr_jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fr_hello_jsp</servlet-name>
<url-pattern>/jsp/fr/hello.jsp</url-pattern>
</servlet-mapping>
package jan;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.util.Enumeration;
public class ServletForward extends HttpServlet {
private ServletContext context;
private HttpServletRequest request;
private HttpServletResponse response;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
boolean found = false;
this.request = request;
this.response = response;
Enumeration locales = request.getLocales();
context = request.getSession().getServletContext();
while (locales.hasMoreElements()) {
if (tryDispatchForLocale((Locale) locales.nextElement())) {
found = true;
break;
}
}
if (!found) {
// error
}
}
private boolean tryDispatchForLocale(Locale locale)
throws IOException {
String jspName = locale.toString() + "_hello.jsp";
RequestDispatcher dispatcher = context.
getNamedDispatcher(jspName);
PrintWriter out = response.getWriter();
if (dispatcher != null) {
out.println("Dispatcher not null " + dispatcher.toString());
try {
dispatcher.forward(request, response);
return true;
} catch (Exception e) {
out.println("JSP Not found" + e.toString());
}
} else {
out.println("Dispatcher is null");
}
return false;
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Draft//EN">
<html> <head>
<title>First Page</title>
</head>
<body>
<h1>First page</h1>
<p>
Choose the <a href="en/hello.html">English pages</a>
<br>
Choisir les pages <a href="fr/hello.html">Francais</a>
</p>
<!--#include virtual="../copyright" --></body>
</html>
en
and fr
directories - it could call servlets or JSP pages, and there
could be different directory structures
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Draft//EN">
<html> <head>
<title>First Page</title>
</head>
<body>
<h1>First page</h1>
<p>
Choose the <a href="hello.jsp?locale=en">English pages</a>
<br>
Choisir les pages <a href="hello.jsp?locale=fr">Francais</a>
</p>
<!--#include virtual="../copyright" --></body>
</html>
locale
parameter and then could set its own locale
for e.g. date formatting