Servlet web hosting - JavaSercer Pages All radio button elements have the

December 31, 2006 on 8:19 pm | In Java | No Comments

JavaSercer Pages All radio button elements have the name language, which means that they form a group where only one of them can be selected. When the user clicks on the Submit button, the same page is requested with the value of the selected radio button included as a request parameter named language. As described above, this triggers the action to switch to the selected language. Next comes another form with radio buttons representing the three possible answers to the poll question. As you can see, both the question and the answers are displayed in the selected language. When the user selects an answer and clicks on the button to submit a vote, the calculate.jsp page shown in Example 11.2 is invoked. Example 11.2. Validation and Calculation of Votes (calculate.jsp) <% if (answer.isValid( )) { %> <% } else { %> <% } %> As with all pure logic pages, this page contains only actions and a few simple scriptlets; no response text is generated. A PollBean in the application scope is used to keep track of the answers from all visitors, and an AnswerBean in the page scope captures and validates a single answer. The AnswerBean has one property named answer, which is set to the value of the corresponding request parameter using the action. It also has an isValid( ) method, used in a scriptlet to test if the answer is valid or not. In this example, it returns true if the answer ID is valid (1, 2, or 3). However, in a real application you may want to include other validation rules. For instance, if the poll information was stored in a database, you could use cookies or a username to make sure each user answers only once. If the answer is valid, a action is used to set the answer property of the PollBean to the valid answer, and the request is forwarded to the result.jsp page to display the poll statistics. Figure 11.3 shows a sample of the result page with the Swedish locale. Figure 11.3. The result page using the Swedish locale page 156

Hint: If you are looking for very good and affordable webspace to host and run your j2ee hosting application check Sandzak.com j2ee web hosting services

JavaSercer Pages The next (Mambo hosting) action in Example 11.1

December 31, 2006 on 1:05 pm | In Java | No Comments

JavaSercer Pages The next action in Example 11.1 is . This is described in Table 11.5. Table 11.5, Attributes for Attribute Name Java Type Request-Time Value Accepted Description name String No Mandatory. The name of the LocaleBean instance. key String Yes Mandatory. The name of a property in the text resource properties files. The action is used to get the page title and header. The name attribute specifies the name of the LocaleBean created by the action, and the key attribute specifies one of the properties in the files with localized strings. These files are named exactly like the files used by the ResourceBundle described in the previous section. In other words, you need one file with the same name as the base name (specified as the bundleName for the action) for the default locale, and one file with a name that combines the base name and a language code for all other locales. In this example, then, you need the files poll.properties, poll_de.properties, and poll_sv.properties. If you want to add support for another language, say Italian, just create a poll_it.properties file and add it (the language code for Italian) to the list of supported languages for the action. All properties files must be placed in the WEB-INF/classes directory for the web application so that the ResourceBundle can find them. Here’s what the poll.properties file looks like: poll.title=Industry Trends poll.select_language=Select your preferred language poll.new_language=New Language poll.english=English poll.swedish=Swedish poll.german=German poll.question=What’s the longest development time you dare to plan with? poll.answer1=One year poll.answer2=Six months poll.answer3=Less than six months poll.submit=Vote poll.number_of_votes=Total number of votes poll.result=Poll result The value of the poll.title key, used by the first two actions, is set to “Industry Trends”; that’s what will appear as the title and header of the page when the default locale is selected. If a Swedish locale was selected instead, the text “Industri Trender” would be used, which is how it is listed in the poll_sv.properties file. The action is used with different keys for all text content in the page. Internally, it uses one of the bean’s regular methods: public String getText(String propertyName) This method returns the specified property (the action element uses the key attribute value) from the properties file that most closely matches the selected locale. The bean provides similar methods for date and numeric values, as you can see in Appendix C. To let the user pick another language than the one selected based on the Accept-Language header, the page contains a form with a set of radio buttons and a Submit button. Every time the page is displayed, the radio button group must reflect the currently selected language. This is done by calling the bean’s language property access method and comparing the return value with the language code represented by each radio button: … > <%= locale.getText("poll.english") %>
… You probably recognize the type of JSP expression used to set the checked attribute for the radio button from previous chapters. The getLanguage( ) method returns the language code for the selected locale as a String. The equals( ) method compares the return value to its argument and returns true if they are the same. If they are, the first string after the question mark is returned as the value of the expression. If not, the second string is used. You also may have noticed that you can use the bean’s getText( ) method directly, as an alternative to the action. Which alternative to use is largely a matter of preference. I used the method here because it’s more compact and less intrusive when the text is used as part of an HTML element. page 155

Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services

JavaSercer Pages At the top of the page,

December 31, 2006 on 6:09 am | In Java | No Comments

JavaSercer Pages At the top of the page, the taglib directive is used to identify the library containing all custom actions, as in previous chapters. Then follows the first custom action for localization: . It’s described in Table 11.3. Table 11.3, Attributes for Attribute Name Java Type Request-Time Value Accepted Description id String No Mandatory. The name used to reference the LocaleBean instance. bundleName String Yes Mandatory. The base name for text resource properties files. supportedLangs String Yes Mandatory. A comma-separated list of language/country codes. The first code is used as the default language. This innocent-looking action does a number of things. First, it looks for a LocaleBean with the name specified by the id attribute in the session scope, and creates one if it doesn’t exist. The LocaleBean, described in Table 11.4, handles all localization tasks. It can be used as-is, as you will soon see, but in most cases it’s used indirectly by other custom actions in the set of localization actions. The action then asks the implicit request object for the list of locales specified by the Accept-Language header, and uses that list to set the bean’s requestLocales property. It also looks for a request parameter named language, and if it’s present, it uses the value to set the corresponding language property in the bean. Finally, it sets the bean’s supportedLangs property to the value of the action attribute with the same name. Both the language property and the supportedLangs property take a value that’s either just a language code, or a language code plus a country code separated by a dash (e.g., Es-MX). As shown in Example 11.1, you can specify a number of supported languages as a comma-separated list of codes. The final attribute is called bundleName; this attribute is the base name for a set of ResourceBundle properties files, as described in the first section of this chapter. Table 11.4, Properties for com.ora.jsp.bean.locale.LocaleBean Property Name Java Type Access Description bundleName String write The base name for the properties files charset String write The charset used to decode parameters language String read/write The language code for the selected locale locale java.util.Locale read The locale, selected based on other properties requestLocales java.util.Locale[] write The locales received with the request supportedLangs String write A comma-separated list of language codes With all these properties set, the bean can decide which locale to use for the user requesting the page. The first time the page is requested, the language property is not set, so it compares the language specified by each locale in the requestLocales property to the set of languages in the supportedLangs property, and selects the first locale that is supported. Since the request locales are ordered by preference, the locale with the highest ranking that represents a supported language is selected. As you will soon see, the user can also request this page with a specific language specified by the language parameter. In this case, the action sets the corresponding bean property and the bean uses this value to select the locale, assuming it’s one of the supported languages. If neither the request locales nor the explicitly specified language is supported, the bean selects a locale that represents the first language listed in the supportedLanguages property. page 154
Hint: If you are looking for high quality webhost to host and run your jsp application check Vision web hosting jsp services

JavaSercer Pages The poll.jsp page also includes a (Mysql webhost)

December 30, 2006 on 10:13 pm | In Java | No Comments

JavaSercer Pages The poll.jsp page also includes a question, linked to a page with background information for the question, and a group of radio buttons representing the different answers, as well as a Submit button. Clicking on the Submit button invokes the calculate.jsp page, where the vote is validated. If it’s valid, it’s added to the global poll result. The request is then forwarded to the result.jsp page, which displays the poll statistics with all numbers formatted according to the selected locale. If it’s not valid, the request is forwarded back to the poll.jsp page. Both the poll.jsp page and the result.jsp page are designed to show text, numbers, and dates according to the selected locale using custom actions based on the Java classes described in the previous section. This approach is perfect when the amount of text is small; only one page has to be maintained. But if a page needs to contain a great deal of text, typing it into a properties file and escaping all line breaks may not be the best approach. Some pages also need to use different layouts, colors, images, and general appearances based on the locale. In this case, it’s easier to use a separate page per locale. This approach is illustrated by the pages providing more detailed information about the question in this example. The link on the poll.jsp page leads to different JSP pages depending on the selected language, named according to the same naming convention as ResourceBundle properties files: details.jsp, details_de.jsp, and details_sv.jsp for English (the default), German, and Swedish pages, respectively. Let’s look at the one-page and the multipage approaches separately. 11.2.1 Using One Page for Multiple Locales Example 11.1 shows the poll.jsp page. That’s where the magic of locale selection happens, and the selection is then used to produce text in the corresponding language throughout the page. Example 11.1. Language Selection and Vote Page (poll.jsp) <%@ page language="java" contentType="text/html" %> <%@ taglib uri="/orataglib" prefix="ora" %>

:

> <%= locale.getText("poll.english") %>
> <%= locale.getText("poll.swedish") %>
> <%= locale.getText("poll.german") %>

“>

“>



“>

page 153

Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services

JavaSercer Pages 11.2 Generating Localized Output Now that (Php5 web hosting)

December 30, 2006 on 2:22 pm | In Java | No Comments

JavaSercer Pages 11.2 Generating Localized Output Now that you have an understanding of the type of internationalization support Java provides, let’s look at a concrete example. But instead of using the internationalization classes directly in the pages, let’s use a set of custom actions based on these classes. Using custom actions minimizes the need for Java code in the JSP pages, making it easier for page authors to develop an internationalized site. The example application, briefly described in the introduction to this chapter, lets visitors voice their opinions by selecting one of the answers to a question, as well as seeing how others have answered. The text, numbers, and dates are available in three different languages. Figure 11.1 shows all pages used in this application and how they are related. Figure 11.1. Localized poll application pages The first page the user sees is the poll.jsp page, shown in Figure 11.2. The language used to display the contents the first time this page is displayed is based on the Accept-Language header value in the request. The top part of the page contains radio buttons for the three supported languages and a Submit button. If the user wants the application to be presented in another language, he or she selects the corresponding radio button and clicks Submit, causing the page to be requested again, this time with a language parameter included in the request. The value of the language parameter is then used to display the page in the selected language. Information about the selected language is saved as session data, so it’s available to all the other application pages. Figure 11.2. The language selection and question page page 152
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services

Web hosting plans - JavaSercer Pages 11.1.3 Using Localized Text Automatic translation

December 30, 2006 on 7:27 am | In Java | No Comments

JavaSercer Pages 11.1.3 Using Localized Text Automatic translation of numbers and dates into the local language is a great help. But until automatic translation software is a lot smarter than it is today, you have to translate all the text used in the application yourself. A set of Java classes then helps you pick the right version for a specific locale. The main class for dealing with localized resources (such as text, images, and sounds) is named java.util.ResourceBundle. This class is actually the abstract superclass for the two subclasses that do the real work, ListResourceBundle and PropertyResourceBundle, but it provides methods that let you get an appropriate subclass instance, hiding the details about which subclass actually provides the resources. Details about the difference between these two subclasses are beyond the scope of this java blog. It suffices to say, however, that the ListResourceBundle is overkill for our needs when developing web applications, so we will be using an instance of the PropertyResourceBundle. To learn more about these classes, I suggest glancing at the Java API documentation. A PropertyResourceBundle instance is associated with a named set of localized text resources, where each resource is identified by a key. The keys and their corresponding text strings are stored in a regular text file as key-value pairs: site_name=The Big Corporation Inc. company_logo=/images/logo_en.gif welcome_msg=Hello! Here, three keys, site_name, company_logo, and welcome_msg, have been assigned string values. The key is a string, without spaces or other special characters, and the value is any text. If the value spans more than one line, the line break must be escaped with a backslash character (): multi_line_msg=This text value continues on the next line. The file must use the extension .properties, for instance sitetext.properties, and be located in the class path used by the Java Virtual Machine. In the case of web applications, you should store the file in the application’s WEB-INF/classes directory, since this directory is always included in the class path. When you have created a properties file, you can obtain the text corresponding to a key like this: java.util.Locale locale = request.getLocale( ); java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle(”sitetext”, locale); String msg = bundle.getString(”welcome_msg”); Note that the getBundle( ) method takes two arguments: a Locale argument, the same as the methods for getting number and date formatters; and a bundle name. These arguments are used like this: the method gets the language and country codes from the Locale object and starts looking for a file with a name composed of both the bundle name and the language and country codes. If you pass it a locale for Mexican Spanish, for example, it first looks for a file named sitetext_es_MX.properties, where es is the language code for Spanish and MX is the country code for Mexico. If it can’t find a file with this name, it looks for sitetext_es.properties, ignoring the country code. If there’s still no such file, it uses the file with just the bundle name, sitetext.properties. As you can see, this makes it possible for you to create multiple properties files, each with the text values translated into a specific language for a specific country. In other words, you can create one file for each supported locale. The ResourceBundle ensures that when you ask for a bundle, you get the one that most closely matches the specified locale, or the default bundle if there is no match. We’ll look at an example in detail in the next section. Besides the ResourceBundle class, there’s a class named java.text.MessageFormat that you can use for messages composed of fixed text plus variable values, such as, “An earthquake measuring 6.7 on the Richter scale hit Northridge, CA, on January 17, 1994.” Here, each underlined word represents a variable value. Another class related to localization is the java.text.Collator class, used for localized string comparison and sorting. These classes are less commonly used, so they are not covered in detail here. You can read more about them in the Java API documentation. page 151

Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services

JavaSercer Pages (Hosting tomcat) The languages are listed in order

December 30, 2006 on 1:28 am | In Java | No Comments

JavaSercer Pages The languages are listed in order of preference, with each language represented either by just the language code or by the language code and country code separated by a dash (-). This example header specifies the first choice as U.S. English, followed by any type of English, and finally Swedish. The HTTP specification allows an alternative to listing the codes in order of preference, namely adding a so-called q-value to each code. The q-value is a value between 0.0 and 1.0 indicating the relative preference between the codes. Very few browsers, if any, use this alternative today, however. The Accept-Language header helps you localize your application. You could write code that reads this header and creates the corresponding Locale instances. The good news is you don’t have to do this yourself; the servlet container takes care of it for you and makes the locale information available through two methods on the implicit request object: java.util.Locale preferredLocale = request.getLocale( ); java.util.Enumeration allLocales = request.getLocales( ); The getLocale( ) method returns the Locale with the highest preference ranking, and the getLocales( ) method returns an Enumeration of all locales in order of preference. All you have to do is match the preferred locales to the ones that your web application supports. The easiest way to do this is to loop through the preferred locales and stop when you find a match. As you will see later, the custom actions developed for this book relieve you of all of this, but now you know how it’s done. 11.1.2 Formatting Numbers and Dates Let’s look at how a locale can be used. One thing that we who live on this planet have a hard time agreeing upon is how to write dates and numbers. The order of the month, the day, and the year; if the numeric value or the name should be used for the month; what character to use to separate the fractional part of a number: all of these details differ between countries, even between countries that speak the same language. And even though these details may seem picky, using the wrong format can cause a great deal of confusion. For instance, if you ask for something to be done by 5/2, an American thinks you mean May 2 while a Swede believes that it’s due by February 5. Java provides two main classes to deal with formatting of numbers and dates for a specific locale, appropriately named java.text.NumberFormat and java.text.DateFormat, respectively. The NumberFormat class was used in Chapter 9, to format the price information for items in a shopping cart according to the customs of the country where the server is located. By default, the NumberFormat class uses the locale of the underlying operating system. If used on a server configured to use a U.S. English locale, it formats numbers according to American customs; on a server configured with an Italian locale, it formats them according to Italian customs, and so forth. But you can also explicitly specify the locale to format numbers according to the rules for locales other than the one used by the operating system: java.util.Locale locale = request.getLocale( ); java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance(locale); String localNumber = nf.format(10000.00); This piece of code creates a String with the number 10000.00 formatted according to the locale that corresponds to the preferred language specified by the Accept-Language header in a request. Besides the getNumberInstance( ) method, you can use the getPercentInstance( ) and the getCurrency-Instance( ) to format a decimal number as a percentage string or any number as a currency string. The DateFormat class works basically the same way, but how dates are written differs a lot more between locales than numbers do, since the day and month names are sometimes spelled out in the local language. Besides the locale, a formatting style is also specified as one of DEFAULT, SHORT, MEDIUM, LONG, or FULL: java.util.Locale locale = request.getLocale( ); java.text.DateFormat df = java.text.DateFormat.getDateInstance(df.SHORT, locale); String localDate = df.format(new java.util.Date( )); If the current date is May 2, 2000, this code formats the date as 5/2/00 with an American locale and as 2000-05-02 with a Swedish locale. If you use the FULL formatting style, the results are Tuesday, May 2, 2000 and den 2 maj 2000 instead. As with the NumberFormat class, there are other specialized date formatters besides the one used here. You can use the getDateTimeInstance( ) and getTimeInstance( ) methods to produce strings including both the date and time or just the time. page 150
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

JavaSercer Pages Here, a Locale for U.S. English (Verizon web hosting)

December 29, 2006 on 8:08 pm | In Java | No Comments

JavaSercer Pages Here, a Locale for U.S. English is created. George Bernard Shaw (a famous Irish playwright) once observed, “England and America are two countries divided by a common language,” so it’s no surprise that both a language code and a country code are needed to describe some locales completely. The language code, a lowercase two-letter combination, is defined by the ISO 639 standard, available at http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt. The country code, an uppercase two-letter combination, is defined by the ISO 3166 standard, available at http://www.chemie.fuberlin. de/diverse/doc/ISO_3166.html. Table 11.1 and Table 11.2 show some of these codes. Table 11.1, ISO-639 Language Codes Language Code Language af Afrikaans da Danish de German el Greek en English es Spanish fr French ja Japanese pl Polish ru Russian sv Swedish zh Chinese Table 11.2, ISO-3166 Country Codes Country Code Country DK Denmark DE Germany GR Greece MX Mexico NZ New Zealand ZA South Africa GB United Kingdom US United States As luck would have it, these two standards are also used to define language and country codes in HTTP. As you may remember from Chapter 2, a browser can send an Accept-Language header with a request for a web resource such as a JSP page. The value of this header contains one or more codes for languages that the user prefers, based on how the browser is configured. If you use a Netscape 4 browser, you can specify your preferred languages in the Edit->Preferences dialog, under the Languages tab. In Internet Explorer 4, you find the same thing in View->Internet Options when you click the Language button under the General tab. If you specify more than one language, they are included in the header as a comma-separated list: Accept-Language: en-US, en, sv page 149
Quick Hint: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting plans

JavaSercer Pages Chapter 11. Internationalization Taking the term

December 29, 2006 on 2:17 pm | In Java | No Comments

JavaSercer Pages Chapter 11. Internationalization Taking the term World Wide Web literally means that your web site needs to respect the languages and customs of all visitors, no matter where they come from. More and more, large web sites provide content in several different languages. Just look at a site like Yahoo!, which provides directory services in the local languages of more than 20 countries in Europe, Asia, and North America. Other good examples are CNN, with local news for 8 different countries, and Vitaminic (http://www.vitaminic.com), a site with MP3 music and artist information customized for different countries. If the site contains only static content, it’s fairly easy to support multiple languages: just make a static version of the site for each language. But this approach is not practical for a site with dynamic content. If you develop a separate site for each language, you will have to duplicate the code that generates the dynamic content as well, leading to maintenance problems when errors are discovered or when it’s time to add new features. Luckily, Java and JSP provide a number of tools to make it easier to develop one version of a site that can handle multiple languages. The process of developing an application that caters to the needs of users from different parts of the world includes two phases: internationalization and localization. Internationalization means preparing the application by identifying everything that will be different for different geographical regions, and providing means to use different versions of all these items instead of hardcoded values. Examples of this are labels and messages, online help texts, graphics, format of dates, times, and numbers, currencies, measurements, and sometimes even the page layouts and colors. You should note that instead of spelling out the word internationalization, the abbreviation I18N is often used. It stands for “an I followed by 18 characters and an N.” When an application has been internationalized, it can also be localized for different regions. This means providing the messages, the help texts, the graphics and so forth, as well as the rules for formatting dates, times, and numbers, for one or more regions that the internationalized application can use. Localization is sometimes abbreviated L10N, following the same logic as the I18N abbreviation. The set of localized information for one region is called a locale. Support for new locales can be added without changing the application itself. In this chapter, we first look at the basic Java classes used for internationalization. If you’re not a programmer, you can skim through this section without worrying about the details. (However, you should understand the terminology, and knowing a bit about the inner workings of these classes also makes it easier to understand the rest of the chapter.) We then develop a web application in which visitors can answer a poll question and see statistics over how other visitors have answered, using a set of custom actions that hide the Java classes to make internationalization a lot easier. You can reuse these custom actions in your own application to handle most internationalization needs. The poll site is localized for three languages. The initial language is based on the user’s browser configuration. The user can also explicitly select one of the supported languages. 11.1 How Java Supports Internationalization and Localization Java was designed with internationalization in mind and includes a number of classes to make the effort as painless as possible. The primary class used for internationalization represents a specific geographical region. Instances of this class are used by other classes to format dates and numbers, as well as including localized strings and other objects in an application. There are also classes for dealing with different character encodings, which we will see later in the chapter. 11.1.1 The Locale Class All Java classes that provide localization support use a class named java.util.Locale. An instance of this class represents a particular geographical, political, or cultural region, as specified by a combination of a language code and a country code. Java classes that perform tasks that differ depending on a user’s language and local customs, called locale-sensitive operations, use a Locale instance to decide how to operate. Examples of locale-sensitive operations are the interpretation of date strings and formatting numeric values. You create a Locale instance using a constructor that takes the country code and language code as arguments: java.util.Locale usLocale = new Locale(”en”, “US”); page 148
Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services

JavaSercer Pages Even if only a server certificate (Comcast webspace)

December 29, 2006 on 9:07 am | In Java | No Comments

JavaSercer Pages Even if only a server certificate is used, however, the communication between the client and the server is still encrypted. This means that the issue of sending passwords as clear text for basic authentication and form- based authentication, as well as the application-controlled authentication we developed in this chapter, is nullfied. Most web servers today support server certificates and SSL. When you use HTTP over SSL (HTTPS), the URLs start with https instead of http. Not all applications need the kind of tight security offered by HTTPS, but you should be aware of all security threats, and carefully evaluate if the risks of not using it are acceptable for your application. page 147
Quick Hint: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting plans

Next Page »