Note: Second lesson in learning javaweb development

Source: Internet
Author: User

1. Chinese back to front page will appear garbled

If the above statement is set only in the servlet, the following occurs (dynamic content is garbled, static content is normal)

Req.setcharacterencoding ("Utf-8");

So the code is also specified on the foreground page.

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

So that the front page can show normal


2. Database Query Chinese garbled

Database connection mode

Private final String Conn_url = "Jdbc:mysql://localhost:3306/weixinmybatis"; Class.forName ("Com.mysql.jdbc.Driver"); Connection conn = drivermanager.getconnection (Conn_url, "root", "root");

When using PreparedStatement's setstring (int, string) method, if the string is Chinese, it becomes. , causing the query to fail

StringBuilder sql = new StringBuilder ("Select Id,command,description,content from MESSAGE where 1=1"); Sql.append ("and Command=? "); PreparedStatement PS = conn.preparestatement (sql.tostring ());p s.setstring (1, "Chinese");

The resulting query statement becomes the

Select Id,command,description,content from MESSAGE where 1=1 and command= '?? ‘

The final solution is to set the encoding when connecting to the database, that is, adding the encoding in the Conn_url

Private final String Conn_url = "Jdbc:mysql://localhost:3306/weixinmybatis?characterencoding=utf8";


Note: Second lesson in learning javaweb development

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.