Handle garbled output when Jsp/Servlet receives FORM table parameters

Source: Internet
Author: User
Tags sybase database

1. garbled characters about the FORM parameters received by jsp (preferred for SUN Enterprise Applications:

<%@ page contentType="text/html;charset=GB2312" %>

<! --/* For tomcat (a very useful JSP running platform) 4. Use jsp (preferred for SUN Enterprise applications) to generate Chinese garbled characters. Define a conversion function getStr ()! -->
<%!
Public String getStr (String str ){
Try {String temp_p = str;
Byte [] temp_t = temp_p.getBytes ("ISO8859-1 ");
String temp = new String (temp_t );
Return temp;
}
Catch (Exception e ){}
Return "NULL ";
}
%>
<! --------------------- The end -------------------- */-->

2. garbled characters about the Form parameters received by Servlet.

File Name: ThreeParams. java:
Package moreservlets; import java. io. *; import javax. servlet. *; import javax. servlet. http. *; public class ThreeParams extends HttpServlet {// defines a function getStr () to solve the problem of garbled form parameters. public String getStr (String str) {try {String temp_p = str; byte [] temp_t = temp_p.getBytes ("ISO8859-1"); String temp = new String (temp_t); return temp;} catch (Exception e) {} return "NULL ";} // -- receives and displays the three parameters sent from form. Note that the third parameter is not converted and can be used for comparison. public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {response. setContentType ("text/html; charset = GB2312"); PrintWriter out = response. getWriter (); String docType = "<! Doctype html public "-// W3C // dtd html 4.0" + "Transitional // EN"> "; out. println (docType + "<HTML>" + "<BODY>" + getStr (request. getParameter ("param1") + "<BR>" + getStr (request. getParameter ("param2") + "<BR>" + request. getParameter ("param3") + "<BR>" + "<center> 

3. How to Deal with Chinese garbled characters when connecting to the Sybase Database Using jConnect:

Make sure that the database uses a Chinese character set, such as eucgb, utf8, and cp936. At the same time, set the necessary connection attributes during jconnect programming, including charset (which must be consistent with the database) and jconnect_version.

For example:

Jconnect 4.2:

Jdbc: sybase: Tds: hostname: port/dbname? Charset = eucgb & jconnect_version = 4

Jconnect 5.2:

Jdbc: sybase: Tds: hostname: port/dbname? Charset = eucgb & jconnect_version = 0

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.