Chinese and related issues in J2EE Web Components (5)

zhaozj2021-02-16  59

Fourth, internationalization

In the form.jsp in the fifth section of the previous section, the customer in Taiwan is more like "please enter your name", and American customers like "please input your name", we can pass international End to meet their needs.

We first create a three area related resource files in WebApp / Web-INF / CLASSES. Chinese Simplified Edition:

# Localstrings_zh_cn.properties

Page.title = Internationalization

Form.Label = Please enter your name

Form.Button = Submit

Chinese traditional version:

# Localstrings_zh_tw.properties

Page.title = Internationalization

Form.Label = Please enter your name

Form.Button = Submit

English version:

# Localstrings_tw.properties

Page.title = INTERNATIONALIZATION

Form.Label = please enter your name

Form.Button = SUBMIT

Localstrings_zh_cn.properties, localstrings_zh_tw.properties and localstrings_tw.properties These three files are saved in system default encoding. Form.jsp is changed:

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

<% @ Page Import = "Beans.localestrings"%>

<% Localstrings lss = new localstrings ("localstrings", request.getlocale ());%>

<% = lss.getstring ("Page.title")%> </ Title></p> <p></ hEAD></p> <p><body></p> <p><form method = "post" action = "test.jsp"></p> <p><label> <% = lss.getstring ("form.label")%> </ label></p> <p><Input Type = "text" name = "name" size = "20"></p> <p><Input Type = "Submit" Value = "<% = Lss.getstring (" Form.Button ")%>">></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>The source file of Beans.localstrings is as follows:</p> <p>Package beans;</p> <p>Import java.util.resourcebundle;</p> <p>Import java.io.unsupportedEncodingexception;</p> <p>Import java.util.locale;</p> <p>Public Class Localstrings</p> <p>{</p> <p>Protected resourcebundle rb;</p> <p>Protected boilean encoded;</p> <p>Public Localstrings (String Basename, Locale Locale)</p> <p>{</p> <p>THIS.RB = ResourceBundle.getBundle (BaseName, Locale); if (locale.getcountry (). Equals ("cn") || locale.getCountry (). Equals ("tw"))</p> <p>Encoded = true;</p> <p>}</p> <p>Public String getString (String Key)</p> <p>{</p> <p>String value = rb.getstring (key);</p> <p>IF (Encode)</p> <p>{</p> <p>Try</p> <p>{</p> <p>BYTE BS [] = Value.getbytes ("ISO-8859-1");</p> <p>Return New String (BS, "GBK");</p> <p>}</p> <p>Catch (unsupportedencodingexception UEE)</p> <p>{</p> <p>Return Value;</p> <p>}</p> <p>}</p> <p>Return Value;</p> <p>}</p> <p>}</p> <p>From the browser access form.jsp, we should see the correct output, then set the browser language into "Chinese (Taiwan) [zh-tw]" by browseing the Internet option, then access form.jsp, we will You can see the prompt information of the traditional Chinese; then set the browser language into "English (US) [EN-US]", and then access form.jsp. Why use localstrings to re-encode these strings, see the resourcebundle's source program that can be found when it reads the file, and we have to encode the string, so we have to be related to the correct encoding in localstrings. String code.</p> <p>Five, some suggestions</p> <p>We have developed by J2EE (Java 2 Enterprise Edition, Java 2 Enterprise Edition), which may be transplanted between some servers that support J2EE specification, and platforms for these servers may also be different. Our client's operating system or browser used is also possible, so we should strive to follow some suggestions:</p> <p>l Depending on the user's operating system, browser, and language in the request head, and use the appropriate method to perform the setting of the CHARSET in the corresponding response head.</p> <p>l Try not to use a constant string containing non-English characters in the servlet.</p> <p>l For JSP files, as long as there is a constant string containing non-English characters that are not ignored by the server, the PageEnCoding property in the Page Ins should be set accordingly.</p> <p>l Use the filter to set</p> <p>ServletRequest.setCharateRencoding; String Encoding;</p> <p>To set the encoding of the request entity instead of setting it in each JSP or Servlet. Abandoning the habit of setting Content-Type in servlet, let the filter pass according to different resources and user objects</p> <p>ServletResponse.setContentyType (String Type);</p> <p>To set it.</p> <p>l Try to use UTF-8 as encoding instead of GBK or GB2312.</p> <p>l Follow the encoding used by the underlying database, it may cause trouble in the transplant of the application.</p> <p>l The encoding used by the data used by the user browser encoding is not easy to effectively and effectively. It is a regret that the programmer is extended, and the ENCTYPE of the FORM element is modified, so that this property is fully compatible with MIME The development of Charset, web applications has fewer troubles. Appendix A. All experiments in this article are as follows:</p> <p>l operating system MS Windows 2000 Server</p> <p>l Area Setting Chinese (China)</p> <p>l System Language Chinese (Simplified) Default</p> <p>l Browser Microsoft Internet Explorer 6.0B</p> <p>l Java virtual machine J2SDK 1.4.0-B92</p> <p>l Java server Apache Tomcat 4.0.4</p> <p>Appendix B. Reference:</p> <p>1. Java technology and its application / Wang Kehong editor; Dong Liquan. - Beijing: Higher Education Press, 1999 (2001)</p> <p>2. JSP Programming Guide (Second Edition) / (US) Brown (BROWN, S.) is waiting; Wang Jun and other translations. - Beijing: Electronic Industry Press, 2002.10 (Wrox programmer Reference Series) title Original: Professional JSP 2nd Edition</p> <p>3. JSP site Design Programming Guide / (US) Dufei (Duffey, K.) is waiting; Wang Jun and other translations. - Beijing: Electronic Industry Press, 2002.7 (Wrox programmer reference series) Original: Professional JSP Site DESIGN</p> <p>4. J2EE Server Advanced Programming Guide / (US) Allamaraju, S.) is waiting; Wenjing Study Interpretation. - Beijing: Machinery Industry Press, 2001.9 (Wrox Programmer Reference Series) : Professional Java Server Programming J2ee Edition</p> <p>5. XML Advanced Programming (Second Edition) / (English) Burbeck (Birbeck, M.) Waiting; 裴 Jianfeng and other translations. - 2 Edition. - Beijing: Machinery Industry Press, 2002.5 (Wrox programmer reference series) Sketch name original: Professional XML 2nd Edition</p> <p>6. Java programming ideology (English version, second edition) / (US) Ecr (Eckel, B.). - Beijing: Machinery Industry Press, 2002.1 (classic original library) title Original: Thinking in Java Second edition)</p> <p>7. XSL technology practice - Just XSL / (US) Senpasson (SIMPSON, J.E.); Peng Shi'an and other translations. - Beijing: Machinery Industry Press, 2002.7 (Internet New Technology Book) title Original: Just XSL</p> <p>8. Java network programming technology insider / (US) Hughes, M.); Liu Yong's first translation. - Beijing: National Defense Industry Press, 2002.3 (Programming Classic Translation) The title of the title: Java NetWork Programming9. Java TM 2 SDK, Standard Edition Documentation Version 1.4.0.</p> <p>-Sun Microsystems, Inc.</p> <p>10. HTML 4.01 Specification. -W3c, 1999.12.24.</p> <p>http://www.w3c.org/tr/html401/html401.html</p> <p>11. Hypertext Transfer Protocol - HTTP / 1.1 / Field, R) is waiting. -Ietf, 1999.6. Http://www.ietf.org/rfc/rfc2616.txt</p> <p>12. Java Chinese Processing Learning Notes - Hello Unicode / Car East. -9CBS_Documentation Center</p> <p>13. JavaScript complete manual / (US) Powell (Powell, T.) is waiting; Buffali and other translations. - Beijing: Electronic Industry Press, 2002.7 (Full Manual Book) title Original: JavaScript: The Complete Reference</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-23799.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="23799" 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.046</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 = 'x591YXpG4Fb2dVk4WhAxa07jLjZYWOfxtWCBocjgTCugYLrMj6pcU_2BBWDzutmSrhdNPS_2BfHWqwqVxwGi4jlJCw_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>