Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Chinese and related problems in J2EE Web components?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

J2EE Web components in Chinese and related issues, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Chinese and related problems in J2EE Web components

1. Internationalized XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >

In the form.JSP in the fifth section above, perhaps Taiwan customers prefer the prompt message "enter your name", while American customers like "Please input your name", we can meet their needs through internationalization.

We first create three region-related resource files in webappWEB-INFclasses. Simplified Chinese version:

# LocalStrings_zh_CN.properties

Page.title= internationalization

Form.label=, please enter your name.

Form.button= submission

Traditional Chinese version:

# LocalStrings_zh_TW.properties

Nationalization of page.title=

Form.label= enter your name

Form.button= submission

English version:

# LocalStrings_tw.properties

Page.title=internationalization

Form.label=Please enter your name

Form.button=submit

The three files, LocalStrings_zh_CN.properties, LocalStrings_zh_TW.properties, and LocalStrings_tw.properties, are saved in the system's default encoding. Change form.jsp to:

The source file for beans.LocalStrings is as follows:

Package beans

Import Java.util.ResourceBundle

Import java.io.UnsupportedEncodingException

Import java.util.Locale

Public class LocalStrings

{

Protected ResourceBundle rb

Protected boolean encoded

Public LocalStrings (String baseName, Locale locale)

{

This.rb = ResourceBundle.getBundle (baseName, locale)

If (locale.getCountry () .equals ("CN") | | locale.getCountry () .equals ("TW"))

Encoded = true

}

Public String getString (String key)

{

String value = rb.getString (key)

If (encoded)

{

Try

{

Byte bs [] = value.getBytes ("ISO-8859-1")

Return new String (bs, "GBK")

}

Catch (UnsupportedEncodingException uee)

{

Return value

}

}

Return value

}

}

To access form.jsp from the browser, we should see the correct output, and then set the browser language to "Chinese (Taiwan) [zh-tw]" by browsing the Inte.NET option, and then visit form.jsp, we can see the prompt message of traditional characters; then set the browser language to "English (United States) [en-us]", and then visit form.jsp. Why use LocalStrings to re-encode these strings? if you look at the source program of ResourceBundle, you can find that when it reads the file, it does not encode the string, so we have to encode the string related to these areas with the correct encoding in LocalStrings.

2. Some suggestions

The enterprise applications we develop with J2EE (Java 2 Enterprise Edition/ Java 2 Platform, Enterprise Edition,Java 2 Enterprise Edition) may be migrated between servers that support the J2EE specification, and the platforms of these servers may be different. Our customers' operating systems or browsers may also be different, so we should try to follow some suggestions:

Determine the user's operating system, browser and language from the request header, and use the appropriate method to set the charset in the response header.

L try not to use constant strings with non-English characters in servlet.

For JSP files, as long as there is a constant string containing non-English characters in the page that is not ignored by the server, the pageEncoding attribute in the page directive should be set accordingly.

L use the filter to set through the

ServletRequest.setCharaterEncoding (String encoding)

To set the encoding of the requesting entity, rather than in each JSP or Servlet. Abandon the habit of setting content-Type in Servlet and let the filter pass according to different resources and user objects

ServletResponse.setContentyType (String type)

To set up.

Try to use UTF-8 for coding instead of GBK or GB2312.

L fully filter out the encoding used in the underlying database, which may cause trouble in the migration of the application.

It is a great regret for programmers that the user's browser cannot simply and effectively judge the encoding used to encode the submitted data. If W3C extends the existing standard and modifies the enctype of the form element, so that this attribute is also fully compatible with the development of MIME's charset,Web application, it will save some trouble.

Appendix A. For all the experiments, the environment is configured as follows:

L operating system MS windows 2000 Server

L locale Chinese (China)

L system language Chinese (simplified) default

L browser Microsoft Internet Explorer 6.0b

L Java virtual machine J2SDK 1.4.0-b92

L Java Server apache tomcat 4.0.4

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report