Ajax interactive servlet return data and JDBC Fuzzy query-Chinese-UTF-8 has been set and fixed garbled

Source: Internet
Author: User



1. Encoding is the conversion of characters to a certain translation into bytes stored in memory or external memory, decoding is to translate the stored bytes into characters in a certain way.



2.ASCII is a single byte, the highest bit total is 0, equivalent to only one byte of 7 bits, 2^7=128 characters, equivalent to 128 keys on the keyboard, there is case because of the letter, there is *,%¥#@! +.... Wait for these to display the characters, also have the control f1,ctr not to display ...



And Iso-8859-1 is the highest bit of ASCII code enabled, theoretically can be more than 128, in fact, not so much, as for the more those, you check the table



UTF-8 is variable-length, specific Chinese several bytes, how to transfer, how to distinguish between ASCII or what you go to view, this should be familiar, the universal code.



3.web, in the browser has a form through the url,ajax, such as the transfer of data, this time there is a place to encode, such as your form data by get way, this parameter is how to encode it, and then to the backend server, we want to receive that parameter, receive time, We're going to take that parameter out of memory, and then we'll encode it, and then to the database, how the database compares and encodes the data. When these 3 places sometimes have a difference will lead to garbled. This solution garbled, the online program is everywhere, you can also search "Java Fuzzy query Chinese" and so look.



4. When you can print those SQL statements to see if there is garbled, if not, but for example, "JDBC Fuzzy query with statement to execute the query" there will be bugs, you can not get results, Rs.next () is always false, the solution is to replace statement with Preparestatement. If you haven't come out yet, clean up your browser and project and restart Eclipse.



5.ajax Interactive servlet returns data to Ajax.



In servlet programming, it is often necessary to return some information through the response object to the browser, to our client, while we display the Chinese on the server side, but the response to the client browser is garbled, mainly due to the response object's getwriter () The PrintWriter object returned by the method uses the "Iso-8859-1" character set encoding to convert the Unicode string to a byte array by default, since the iso8859-1 character set contains no Chinese characters at all. So Java in the conversion when the invalid character encoding output to the client, so there is garbled, for this servletresponse interface defines the setcharacterencoding, setContentType methods to specify the character set encoding used by the PrintWriter object returned by the Getwriter method, so we set the values of these methods before calling the Getwriter method in the Write Servlet program. In order to prevent garbled, we often write the following two statements together:



Response.setcontenttype ("Text/html;charset=utf-8");



Response. Setcharacterencoding ("UTF-8");








// ajax
<script type = "text / javascript">
    function findsc () {
        $ (‘# Metable‘). Empty ();
        $ .ajax ({
            url: "find",
            type: "get",
            data: {
                key: $ (‘# keywords’). val ()
            },
            success: function (data) {
                alert (data);
                $ (‘# Metable‘). Append (data);
            }
        });
    }
    
</ script>
// servlet key
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType ("text / html; charset = utf-8");
        response. setCharacterEncoding ("UTF-8");
        
        response.getWriter (). append ("Served at:") .append (request.getContextPath ());
        MyServiceDao myServiceDao = new MyServiceDao ();
        List <Sc> temp;
        PrintWriter outme = response.getWriter ();
        outme.print ("<tr> <td style = 'background: # FFF68F;'> Student ID </ td> <td style = 'background: # FFF68F;'> Name </ td> <td style = 'background: # FFF68F; '> Chinese </ td> <td style =' background: # FFF68F; '> Mathematics </ td> <td style =' background: # FFF68F; '> English </ td> <tr> ");
        try {
            temp = myServiceDao.getScByName (request.getParameter ("key"));
            for (Sc sc: temp) {
                outme.print (
                        "<tr> <td style =‘ background: # CAFF70; ‘>” + sc.getId ()
                        + "</ td> <td style =‘ background: # CAFF70; ‘>” + sc.getName ()
                        + "</ td> <td style =‘ background: # CAFF70; ‘>” + sc.getYuwen ()
                        + "</ td> <td style =‘ background: # CAFF70; ‘>” + sc.getShuxue ()
                        + "</ td> <td style =‘ background: # CAFF70; ‘>” + sc.getYingyu ()
                        + "</ td> <tr>"
                        );
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
        outme.close ();
    } 





Ajax interactive servlet return data and JDBC Fuzzy query-Chinese-UTF-8 has been set and fixed garbled


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.