Create an international JSP application

xiaoxiao2021-03-05  27

Sing li author, Wrox Press, April 14, 2005

Designing Java Server Pages (JSP) applications for international customers is more art, not as science, but what you involve can not only meet eyeballs. The key to success is to understand the unique server-side issues related to internationalization. Java Developers Sing Li will elaborate this important issue and give two solutions that have been active and effective.

The world economy is increasingly global promoting people's demand for web-based software, as users in many countries can access Web software. The language, display, data entry, representation, and cultural needs of these users may differ. Internationalization (abbreviations I18N) is an art of an application that is created as a scattered user group.

Perhaps a little surprised, J2SE's international built-in support will exhibit some shortcomings when doing any custom modifications are used in server-side. In general, the internationalization of the server is still an art, not a science, often involving some dedicated or user-developed solutions.

This article uses the internationalization needs of the server-based JSP application to the international demand of the J2SE application. This article will introduce a variety of client / server technologies that lead to different server-side requirements. The actual code will then be viewed, which shows two solutions used to solve basic problems.

Beyond the J2SE J2SE concept (see NOTATION for A Locale) for internationalization. On a machine, the region represents the user-selected display language (eg, English or Spanish), and the formatting agreement in the date, time, currency and other aspects. Typically, the regional option settings are managed by the underlying operating system and passed it to J2SE when running.

If the running server is on the local machine, or on the LAN, then the concept of a machine-specific area is very easy. All clients and servers included are in the same area, so they use the same display language, date agreed. These scenarios will not bring trouble. But if you want to provide services to users in multiple international locations with the same server, then the situation is very complicated.

Server-side internationalization issues When deploying a server application in order to conduct international access, the application must support different areas. Figure 1 shows a possible scenario. Each machine-server and client access to the server at any time may have its own regional settings, and these areas are different.

The concept area of ​​the region is specified in the form of a national code followed by the language code. (See Resources to get more information about these codes.) The standard format is XX_YY, where the language code representing two lowercase letters, YY is the country code represented by two uppercase letters. It is also possible to see the use of a function between language and country code, or write country code into a small-writen form. It is necessary to divide the language from the state because there may be more than one country to use the same language, but the date, time, currency format, etc. may have different expressions.

Figure 1. Servers providing services for users in different regions

In Figure 1, the server machine is located in San Francisco, and the machine-specific area is en_us (US English). Users from New York and Dallas use EN_US regions, so there is no additional international demand. However, users from Seoul expect to see apps that use Korean expressed apps, their area is KO_KR. At the same time, users from Shanghai want to see the application text expressed in Chinese, and their area is zh_cn. User from Tokyo expects to display applications in Japanese, their area is JA_JP. All of these users must be met by running JSP applications on the San Francisco server. At the server side, you have all control over your own area, but you can't change the area of ​​the client or forcibly convert it into a particular area. Instead, the application must identify the user's area and ensure that the JSP page appears in the correct localization form (see detecting client locale).

Detecting the client area by checking the HTTP header passed from the user browser, you can automatically detect the user's area. However, the user or operating system may not set this information correctly. Coupled with this fact, that is, some browser processing areas exists, there is a bug, you are not difficult to understand, why make the app explicitly ask the user preferred area is a method of determining regional values.

More regional judgment complexity is that you can safely determine the client area safely and present the JSP accordingly, the new problem has appeared. Consider these very realistic scenes.

A visit personnel from Tokyo are using machines located in Shanghai, China, and the area of ​​the machine is zh_cn. Because not familiar with written Chinese, she wants to access the web application in Japanese. For this case, see Figure 2 (a).

Figure 2. Different parts of the user expectations from the client machine

In Figure 2 (a), the area of ​​the San Francisco server is en_us. The client machine is in Shanghai, and the region is zh_cn. But JSP applications need to be displayed in JA_JP, which is useful to users.

Look at another scene of weird but not impossible. Developers with international JSP applications are debugging. On a region of en_us, three browser instances running a JSP application are opened on a client system for EN_US. The server machine is also en_us on the local area network. However, it is now necessary to test the application of the application for Chinese and Japanese users. So on an EN_US client machine, a browser instance is in English (En_us), one is Japanese (JA_JP), and one is Chinese (zh_cn). Figure 2 (b) demonstrates this situation.

So far, basic internationalization should be very clear: When dealing with any special instances required to display an international application, there are some power from the server area and server. Only users can say which area to display the page.

But Xie Tian, ​​usually, certainly assume that the user does not change the display language during the use of the application. So you usually associate the regions with the session.

Solve the problem-specific language display problem For JSP applications, there are at least two general acceptance methods for displaying different languages ​​to display:

Store multiple sets of JSPs, each set of JSPs are encoded in different languages, and then switch between these JSPs according to the user's region. Separate all strings used, in which a region-specific string is obtained from resource binding. (This approach uses J2SE-specific resource binding processing mode.) This article provides two versions of the sample code, respectively, corresponding to both methods. The sample application is a login screen called developerWorks Email's fictional email service. First, use the language to select the screen prompt the user of the email system to determine the current session area required. The choices that can be made include English, Korean, Japanese and Chinese, as shown in Figure 3. If you want to try the code, use http: // /dwi18n/multdir/index.jsp This URL accesses this page.

Initial language to use pictures The usually no reliable method can pre-know what kind of font supports the introduced client. In order to save storage and memory, most modern operating systems do not pre-install support for all fonts of all Unicode characters in advance. Some browsers will try to download and install these fonts when the first visit to the web page containing the necessary font. Use pictures to display foreign characters or flags in a viable way to represent language selection screens.

Figure 3. Language selection screen for explicitly selection

In Figure 3, the language selection screen represents 4 regions in four images (see Using images for Initial Language Selection). The login screen uses the user to display the language selected here. Figure 4 shows the Japanese login screen.

Figure 4. Japanese login screen

The first version of the multi-setup-specific JSP set example is located in the WebAPPS / DWI18N / MULTDIR / directory of the sample code release package, which uses multiple sets of JSP pages. Figure 5 shows the directory structure of this application.

Figure 5. Directory structure of DWI18N / MULTDIR shows a region-specific directory

In Figure 5, there are corresponding subdirects in each region. The JSP encoded in English is in the EN sub-directory. The JSP encoded with the KO_KR region is in the KO subdirectory. For JA_JP regions, Japanese encoded JSP is in the JA subdirectory. The ZH_CN area is expressed in the ZH subdirectory. Each subdirectory contains both the JSP (Login.jsp) of the login screen, also contains data confirmation JSP (Confirm.jsp).

Data confirmation JSP is only displayed in this simple example. For example, if information is entered in the Chinese login screen, then click the button, the data confirms that the JSP will display the input data, as shown in Figure 6.

Figure 6. Chinese confirmation page

Writing a JSP code area Select JSP called index.jsp, which directly links to a language-specific JSP. This version of INDEX.JSP code is in Listing 1:

Listing 1. Region Selection JSP Direct Link to language-specific pages

<% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%> select a language </ title> </ head> <body> <Table> <TR> <TD colspan = 4 BGColor = "black"> <center> <font face = "arial" size = 2 color = "white"> <b> <i> Developer </ i> Works Email </ b> </ font> </ center> </ td> <c: url value = "en / login.jsp" VAR = "Englishurl" /> <a href=" ${ iong src = "english.gif" /> </a> </ td> <td> <c: url value = "ja / login .jsp "VAR =" japaneseurl "/> <a href=" $ jAPAneseurl }"> <img src =" japanese.gif "/> </a> </ td> <td> <c: url value =" KO / login.jsp "var =" koreanurl "/> <a href=" - {koreanurl }"> <img src =" korean.gif "/> </a> </ td> <td> <c: URL Value = "zh / login.jsp" var = "chineseurl" /> <a href="-{ "}"> <img src =" chinese.gif "/> </a> </ td> </ tr> </ table> </ body> </ html> Note that the <C: URL> tab from the JSP Standard Tag Library (JSTL) is created in Listing 1 to create a link URL. This ensures that session management is properly processed. (See Resources for more information on JSTL and <C: URL> tags.)</p> <p>Each group login.jsp and confirm.jsp use the area-specific language. Listing 2 shows login.jsp in the JA_JP region (corresponding to Figure 4):</p> <p>Listing 2. Login page (login.jsp) in the JA_JP area (login.jsp)</p> <p><% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%> <html> <head> <meta http-equiv = "content-type" content = " TEXT / HTML; Charset = UTF-8> <Title> DeveloperWorks Electronic メ メ ル </ title> </ head> <body> <c: url value = "confirm.jsp" var = "actionURL" /> <form action = "$ {ActionURL}" method = "post"> <table> <tr> <td colspan = 2 bgcolor = "black"> <center> <font face = "arial" size = 2 color = " White "> <b> <i> Developer </ i> Works Electronics メ メ </ b> </ font> </ center> </ td> </ tr> <tr> <td> ユ ID < / TD> <TD> <input type = "text" name = "userid" size = "40" /> <td> パ パ ワ ワ ド </ td> <td> <input type = "Password" name = "pass" size = "40" /> <td> </ tr> <TD colspan = "2" align = "center"> <input type = "Submit" value = "グ グ グ ン "/> </ td> </ tr> </ table> </ form> </ body> </ html> is similar to this, for the zh_cn area, confirm.jsp (corresponding to Figure 6) is Chinese code , As shown in Listing 3:</p> <p>Listing 3. Data confirmation page in the zh_cn area (Confirm.jsp)</p> <p><html> <head> <meta http-equiv = "content-type" content = "text / html; charSet = UTF-8"> <title> developerWorks Email </ title> </ head> <body> <Table Border = "1"> <Tr> <TD colspan = 2 bgcolor = "black"> <center> <font face = "arial" size = 2 color = "white> <b> <i> developer </ i> Works Email </ b> </ font> </ center> </ td> </ tr> <tr> <td> User account </ td> <td> $ {param.userid } </ td> </ tr> <tr> <td> Password </ td> <td> $ {param.pass} </ td> </ tr> </ table> </ body> </ html> front The multiple-redundant-set method describes the following applications is a feasible solution:</p> <p>Mainly in a language, occasionally access from other regions. The JSP change of the layer representing the layer is not very frequent.</p> <p>The maximum deficiencies of the solution in the previous section of the J2SE resource binding the previous section of the Using Multiple Redundant Sets of Language-Specific JSPS is that all redundant encoded JSP colors must be updated at the same time when updating the language-specific JSP set. For a medium-scale project, this will cause lengthy and easy error update.</p> <p>The look and effects of the solutions to be introduced now are similar to the previous, but in this case, there is only one set of login.jsp and confirm.jsp. This solution uses J2SE to support regional support in resource bindings, only uses regional text strings only when needed (see Resources for more information on J2SE resource binding). The sample code of this solution is located in the WebApps / DWI18N / Javares directory. If the sample code is deployed, use http: // <server address> /dwi18n/javares/index.jsp this URL.</p> <p>Figure 7 shows the four browser sessions running on the same client machine, each session request is different.</p> <p>Figure 7. 4 different regions on the same machine</p> <p>In Figure 7, it is clear that the international JSP application can handle multiple regions simultaneously.</p> <p>Writing JSP code In this case, INDEX.JSP is slightly different because it is now linked to a separate login.jsp. Listing 4 shows this version of INDEX.JSP code:</p> <p>Listing 4. Link to regional selection page of Login.jsp</p> <p><% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%> <html> <head> <title> select language </ title> </ head> < Body> <table> <tr> <TD colspan = 4 BGColor = "black"> <center> <font face = "arial" size = 2 color = "white"> <b> <i> developer </ i> Works email </ b> </ font> </ center> </ td> </ tr> <tr> <td> <c: url value = "login.jsp" var = " EnglishURL> <c: param name = "locale" value = "en_us" /> </ c: url> <a href=" - iv src = "english.gif" /> </ a > </ td> <td> <c: url value = "login.jsp" var = "japaneseseurl"> <c: param name = "locale" value = "ja_jp" /> </ c: url> <a href = "$ {japaneseseurl}"> <img src = "japanese.gif" /> </a </ td> <td> <c: url value = "login.jsp" var = "koreanurl"> <C: Param name = "locale" value = "ko_kr" /> </ c: URL> <a href=" #koreanurl }"> <img src = "korean.gif" /> </a> </ td> < TD> <c: url value = "login.jsp" var = "chineseURL"> <c: param name = "locale" value = "zh_cn" /> </ c: URL> <a href = "$ {ChineseURL} "> <img src =" chinese.gif "/> </a> </ td> </ tr> </ table> </ body></p> <p></ html> Note that in Listing 4, a URL request parameter called LOCALE is set using JSTL's <C: Param> tag. This parameter is passed to login.jsp when the user clicks the language selection. Listing 5 shows the code of login.jsp:</p> <p>Listing 5. Use the J2SE resource binding login page (login.jsp)</p> <p><% @ Page PageEncoding = "UTF-8"%> <% @ taglib prefix = "c" URI = "http://java.sun.com/jsp/jstl/core"%> <% @ taglib prefix = " FMT "URI =" http://java.sun.com/jsp/jstl/fmt "%> <HTML> <c: set var =" loc "value =" en_us "/> <c: if test =" $ {! (empty param.locale)}> <c: set var = "LOC" value = "$ {param.locale}" /> </ c: if> <fmt: setlocale value = "$ {loc}" /> <fmt: bundle base = "app"> <head> <title> developerWorks <fmt: message key = "email" /> </ title> </ head> <body> <c: url value = "confirm. JSP "var =" formactionurl "/> <form action =" $ {formactionurl} "method =" post "> <table> <tr> <td colspan = 2 bgcolor =" black "> <center> < FONT face = "arial" size = 2 color = "white> <b> <i> developer </ i> Works <fmt: message key =" email "/> </ b> </ font> </ center > </ td> <fmt: message key = "userid" /> </ td> <td> <input type = "hidden" name = "locale" value = "$ {loc}" /> <input type = "text" name = "userid" size = "40" /> </ td> <fmt: message key = "Password "/> </ td> <TD> <input type =" text "name =" pass "size =" 40 "/> </ td> </ Tr> <TR> <TD COLSPAN =</p> <p>"2" align = "center"> <input type = "Submit" value = "<fmt: message key = 'login' /> /> </ td> </ tr> </ table> </ form> < / body> </ fmt: bundle> </ html> Listing 5 Using JSTL Internationalized Auxiliary Tag Library (see Resources). If the incoming param.locale is empty, the area is set to En_US by default. When using resource binding, you can set the area with the <FMT: SetLocale> tag.</p> <p>After completing the regional settings, <fmt: message> extracts text from the properties file in a binding state, which corresponds to the specified keyword. Use the <fmt: bundle> JSTL tab to set the bound basic name to App. If you look at the DWI18N / Web-INF / CLASSES directory, you can see all the files in the resource binding. Table 1 describes these files. See Resources to get more information that uses J2SE resource bindings.</p> <p>Table 1. Files in resource binding</p> <p>File Name Description App.Properties The properties file used by default. Corresponding to the EN_US area. App_zh.Propertieszh_cn's properties file. Contains a string encoded with Chinese. App_ko.propertiesko_kr region attribute file. Contains strings encoded with Korean language. App_ja.Properties attribute files in the PropertiesJa_JP area. Contains a string encoded with Japanese. * .ucd creates the Unicode source file for the properties file. Convacii.bat converts UCD files into batch files for attribute files.</p> <p>As an example, Listing 6 shows the contents of the app_ko.properties file:</p> <p>Listing 6. APP_KO.PROPERTIES file in resource binding</p> <p>Email = / uc774 / uba54 / uc77c userid = / uc544 / uc774 / ub514 password = / ube44 / ubc00 / ubc88 / ud638 login = / ub85c / uadf8 / uc778</p> <p>Note that in Listing 6, all Unicode characters are essential. Must do this, because Java's resource binding mechanism only accepts properties files encoded with ASCII. To create this file, you can use the string resource editor in the IDE, you can also create a Unicode file using the Unicode editor, and then convert it with JDK's NativeToascii tool. In this example, the Convascii.bat file is responsible for conversion.</p> <p>Conclusion When setting an international JSP application, you need to understand its unique needs. Applications must be prepared to support multiple concurrent access to users with different regional needs. This article describes two solutions for displaying international apps with regional language text. But I am just the face of charming art of creating an international server-side application. Other important issues include handling different dates and currency formats, managing the GUI layout and using a specialized input method editor (IME, entering foreign characters). Please refer to the reference for information that helps further understand internationalization.</p> <p>Reference</p> <p>You can see this article in our website on our world. Click Code.zip discussed in this article on the top or bottom of this article (or see Downloads). The language code for the two characters used in the region is ISO-639 code. Country code for two characters used in the region is ISO-3166 code. Consultate. To learn all the JSTL tags, see the A JSTL Getting Started Series on DeveloperWorks. The representation is that everything is closely related to these tags, which introduces some international labels. By reading JavaDoCEBundle classes, found more about J2SE resource bindings, how to create them and where they are placed. You can also read Javadoc's online article here. Tutorial Java Internationalization Basics (United States, April 2002) is a great place to learn all available J2SE international support mechanisms, allowing you to create them in server-side applications. Unicode Input Method Editor is a tool that can help discover globalization in the development cycle as soon as possible, which provides a simple mechanism to easily reproduce globalization. International Components for Unicode is a mature, widely used library for UNICODE support, software internationalization, globalization. ICU is an open source development project sponsored by IBM sponsorship, support and use. Refer to Sing Li's book Beginning JavaServer Pages (John Wiley & Sons, 2005) to further study internationalization and localization. Join the developerWorks community by participating in developerWorks Blogs. An article on all aspects of Java programming can be found at the Java technology area of ​​DeveloperWorks. See Developer Bookstore to get a complete list of technical books, including hundreds of Java-related topics. download</p> <p>description</p> <p>Name</p> <p>Size</p> <p>Download Method</p> <p>Sample Code for Article Tested On Tomcat 5.5.7</p> <p>Code.zip</p> <p>25 KB</p> <p>FTP</p> <p>Information about downloading methods</p> <p>About the author Sing Li is a consultant and freelance. His work includes Beginning JavaServer Pages, Professional Apache Tomcat 5, Pro JSP - Third Edition, Early Adopter Jxta, Professional Jini</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-35518.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="35518" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.050</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'ylF1O_2BY1XJBzV2jUuKEKlh0NYmQjXC8JeksTPHFRfEoQVfwd2snHO7U5XSTYcaGOCDdSbxdPm1Iq3pTv0FdZ1w_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>