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 solutions to common Chinese problems in JAVA?

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

Share

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

What are the solutions to common Chinese problems in JAVA? many beginners are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Solutions to Common Chinese problems in JAVA

The following solutions are encountered by the author in daily life, and I hope they can help you solve the JAVA Chinese problem.

1. Add to the head of the jsp page

Use httpServlerResponse.setContentTpye ("text/html; charset=GB2312") in servlet; some Chinese problems can be avoided

2. When using JDBC to connect to a mysql database, the connection string is written in the following form to avoid some Chinese problems:

Jdbc://mysql://hostname:port/DBname?user=username&password=pwd&

UseUnicode=true&characterEncoding= iso-8859-1

If you are connecting to the database as a data source, use it in the configuration file:

Url

Jdbc:mysql://hostname:port/DBname? & useUnicode=true&characterEncoding=iso-8859-1

Be careful to use the & replace & symbol, otherwise the XML file will make an error when parsing.

3. The data read from the database may be garbled. If you encounter this problem, you can solve it in the following ways:

String desc = rs.getString ("desc")

Desc = new String (desc.getBytes ("ISO-8859-1"), "GB2312")

4. When a page submits Chinese content to Servlet,Servlet, it is necessary to transcode the submitted content in order to receive data correctly.

Usually we add the following code to servlet to solve the problem.

HttpServlerRequest.setCharacterEncoding ("GB2312")

5. In struts, transcode the resource file and use the transcoding tool of the JDK belt:

> native2ascii-encoding BG2312 Myresource.properties Myresource_zh.properties

6. Extend the org.apache.struts.action.RequestProcessor class in struts and override the processPreprocess () method:

Package com.mypro.action

Public class MyProRequestProcessor extends RequestProcessor

{

Protected boolean processPreprocess (HttpServletRequest request

HttpServletResponse response)

{

Try

{

Request.setCharacterEncoding ("GB2312")

/ / other code

}

Catch (Exception e) {}

Return true

}

}

After writing the above code, don't forget to modify struts-config.xml:

7. Implement with filter (recommended)

Package com.kefeiannan

Import java.io.IOException

Import javax.servlet.*

Public class SetCharacterEncodingFilter implements Filter

{

Protected String encoding = null

Protected FilterConfig filterConfig = null

Protected boolean ignore = true

Public void destroy () {

This.encoding = null

This.filterConfig = null

}

Public void doFilter (ServletRequest request, ServletResponse response

FilterChain chain)

Throws IOException, ServletException {

If (ignore | | (request.getCharacterEncoding () = = null)) {

String encoding = selectEncoding (request)

If (encoding! = null)

Request.setCharacterEncoding (encoding)

}

Chain.doFilter (request, response)

}

Public void init (FilterConfig filterConfig) throws ServletException {

This.filterConfig = filterConfig

This.encoding = filterConfig.getInitParameter ("encoding")

String value = filterConfig.getInitParameter ("ignore")

If (value = = null)

This.ignore = true

Else if (value.equalsIgnoreCase ("true"))

This.ignore = true

Else if (value.equalsIgnoreCase ("yes"))

This.ignore = true

Else

This.ignore = false

}

Protected String selectEncoding (ServletRequest request) {

Return (this.encoding)

}

}

Configure the web.xml under your site and add at the end

Set Character Encoding

Com.kefeiannan.SetCharacterEncodingFilter

Encoding

UTF-8

Set Character Encoding

/ *

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