JSP (1) -- Chinese character conversion

Source: Internet
Author: User

If you are a. Net programmer, you should be glad that you will never encounter the following problems.

If you are a JSP programmer, you should be glad you have encountered the same problem as me.

That is, the conversion and reading of Chinese Character paths on the JSP page.

I wrote this article to vent my feelings, and I used to remind people who read the article not to fall into such a dilemma.


Venting is angry. Why is JSP so inflexible? I have been suffering from a Chinese character for N days. The second venting is that there are so many network resources. Why can't I find relevant information.

After the vent is completed, the following parts are the problem and solution.


I. Business Requirements

1. Upload a video file to a specific path, such as http: // 8080/Java programming/video tutorial/Chinese character. Avi

2. You can preview the video file in the Page Management Section.


Ii. Preliminary plan

1. Use the smartupload component to upload files: the previous article provides specific solutions.


2. Use the JSP video player to play the video. The Code is as follows:


<Object ID = "layer" classid = "CLSID: 6bf52a52-394a-11d3-b153-00c04f79faa6 "width =" 320 "Height =" 217 "> <br/> <Param name =" url "value =" relative or absolute playback path "/> <br /> <embed autostart = "true" <br/> src = "<% = http: // 8080/Java programming/video tutorial/Chinese characters. avi %> "<br/> type =" Video/X-MS-WMV "<br/> width =" 320 "<br/> Height =" 217 "<br/> controls = "imagewindow" <br/> console = "cons"> <br/> </embed> <br/> </Object>


Iii. Effect

1. Most people will think that the preview is successful and the result is handled (such a person is a rookie like me)

2. Actual results: the video cannot be played normally.

3. Reason: The Fault Caused by Chinese Character paths


Iv. Solutions

1. Modify the ctor node in Tomcat 6.0/CONF/server. xml.

<Connector Port = "8080" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Redirectport = "8443"
Uriencoding = "GBK" // newly added content

2. Make sure that the standard JSP page ID exists in the first line of the video preview page:

<% @ Page Language = "Java" contenttype = "text/html; charset = GBK" iselignored = "false" pageencoding = "GBK" %>


3. The function that completes character conversion: utftogbk (string Str). The specific implementation is as follows:


1. Import java.net .*;

2. The slash '/' must not be converted.

// Database character conversion function <br/> Public String utftogbk (string SRC) <br/>{< br/> string des = ""; <br/> for (INT I = 0; I <= SRC. length (); I ++) <br/>{< br/> If (SRC. charat (I) = '/') <br/>{< br/> des + = "/"; // conversion is not required when encountering/Symbols <br/>}< br/> else <br/>{< br/> // when encountering Chinese or English characters <br/> des + = urlencoder to be converted. encode (string. valueof (SRC. charat (I), "GBK"); </P> <p >}< br/> return des; <br/>}


4. Final Implementation Code

<Object ID = "layer" classid = "CLSID: 6bf52a52-394a-11d3-b153-00c04f79faa6 "width =" 320 "Height =" 217 "> <br/> <Param name =" url "value =" relative or absolute playback path "/> <br /> <embed autostart = "true" <br/> src = "<% = utftogbk (http: // 8080/Java programming/video tutorial/Chinese characters. avi) %> "<br/> type =" Video/X-MS-WMV "<br/> width =" 320 "<br/> Height =" 217 "<br/> controls = "imagewindow" <br/> console = "cons"> <br/> </embed> <br/> </Object>

5. Playback successful. Solve Chinese Character Problems


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.