JSP page written in Chinese to MySQL garbled (go)

Source: Internet
Author: User

Today, I use JSP to write Chinese to MySQL when garbled, read from the database when it is also displayed as "?? ", it feels like there should be a loss of character information during the encoding conversion process. Then execute the command directly in MySQL and find that Chinese is normal and that there is a problem when writing Chinese in the database.

On the internet for a while, found a lot of solutions, and turned an article over, this just can solve their own problems. Thank you for that.

Today encountered in the JSP page written in Chinese to MySQL garbled, has been a long process, the problem finally solved. First say your own development environment: JSP+ECLIPSE+MYSQL+TOMCAT, now generally used in DW (Dreadweaver) to write JSP code, is through a javabean to insert data into the MySQL database. After the test found English success, but Chinese showed: "??? "Such garbled, the first reaction is to execute the insertion in MySQL directly in the Chinese statement, MySQL succeeded, no garbled." So the internet to find a lot of information:

In fact, no matter what code: gb2312 or Utf-8, we should follow the principle of unified coding. That is, if you are using Utf-8 on the page, then you build the MySQL table, and here are the places where you need to unify:

  1. Add Contenttype= "text/html to the <%@ page location on the JSP pages; Charset=utf-8″%>,<meta's position is also the same: content= "text/html;charset=utf-8″/>
  2. Build a MySQL database, the character set uses Utf-8, and the table is built.
  3. In the URL of getconnection add:? Useunicode=true&characterencoding=utf-8″, such as: url = "jdbc:mysql://localhost:3306/( The database name here)? Useunicode=true&characterencoding=utf-8″, which is after (database name), where the question mark cannot be lost
  4. If you're not working directly with the form in a JSP page, and I'm using a servlet to process the form like I do, then add a filter, mainly because the Tomcat Web Publisher is used, and if you use resin, there's no such problem. That's why I spent a lot of time behind me. Filter Setcharacterencodingfilter.java Code: Package Peom;import java.io.IOException; Import Javax.servlet.Filter; Import Javax.servlet.FilterChain; Import Javax.servlet.FilterConfig; Import javax.servlet.ServletException; Import Javax.servlet.ServletRequest; Import Javax.servlet.ServletResponse; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletRequestWrapper;

    public class Setcharacterencodingfilter implements Filter {class Request extends Httpservletrequestwrapper {         Public Request (HttpServletRequest request) {super (request); }

            public string Tochi (String input) {             try {                 byte[] bytes = input.getbytes ("Iso8859-1″");                 return New String (Bytes, "Utf-8″");             } catch (Exception ex) {             }              return null;         }

            private httpservletrequest gethttpservletrequest ()          {             return (HttpServletRequest) super.getrequest ();         }

            public string GetParameter (string name)          {            return              tochi (Gethttpservletrequest (). GetParameter ( name));         }         public String[ ] Getparametervalues (String name)         {             string values[] =gethttpservletrequest (). getParameterValues (name);             if (values = null) {                 for (int i = 0; i < values.length; i+ +) {                    values[i] = Tochi (Values[i]);                 }              }              return values;         }     }

    public void Destroy () {} public void DoFilter (ServletRequest request, Servletresponse Response,filterchain CH Ain) throws IOException, servletexception {httpservletrequest httpreq = (httpservletrequest) request;

    if (Httpreq.getmethod (). Equals ("POST")) {

    Request.setcharacterencoding ("Utf-8″");

    } else {request = new request (HTTPREQ); }

    Chain.dofilter (request, response); }

    public void init (Filterconfig filterconfig) throws servletexception {}}

    Then in the Web-inf directory of the Web. xml file, configure the filter,

    <filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>peom. setcharacterencodingfilter</filter-class> </filter> <filter-mapping> <filter-name> Setcharacterencodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

    Note here: The path, code as far as possible in front, preferably behind the <web-app

  5. Finish encountering type problem to deal with, feel very simple, in the problem still want to more point reference source address: http://blog.sina.com.cn/s/blog_6c5ad3d10100ligv.html

JSP page written in Chinese to MySQL garbled (go)

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.