(Reprint)UTF-8 for multilingual JSP pages

Reprint from: http://hermanland.com/a/1/2008/04/01/1207062060000.html

UTF-8 encoding can be used to present multilingual web pages. Java handles all string internally as Unicode 16 and expects all input or output data encoded in ASCII. Therefore, we need to explicitly tell Java that our data are encoded in UTF-8. Here are the steps for Tomcat + JSP + MySQL.

  1. Tells the JSP compiler that the JSP page is encoded in utf-8. The JSP page directive to do this is:

     

    <%@page pageEncoding="UTF-8"%>

     

  2. Tells the JSP compiler to sent data to the browser in utf-8 encoding. The JSP page directive to do this is:

     

    <%@ page contentType="text/html; charset=utf-8"
    language="java" import="java.sql.*" errorPage="" %>

     

    The directive also sets the HTTP Content-Type header accordingly.

  3. As a good practice, markup the html page with a META tag to indicate the data is utf-8 encoded.

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

     

  4. Tells JDBC that the data in MySQL database is encoded in utf-8. The attributes for JSTL SQL tag to do this are:

     

    <sqla:setDataSource
    var="example"
    scope="application"
    driver="org.gjt.mm.mysql.Driver"
    url="jdbc:mysql://localhost:3306/test?
    autoReconnect=true&useUnicode=true&characterEncoding=utf-8" user="??"
    password="??"/>

     

  5. Tells the ServletRequest object that the data from the input html FORM is utf-8 encoded. This should be done before reading any data from the object. The scriplet to done this is:

    <% request.setCharacterEncoding("utf-8"); %>

     

  6. If you are using Notepad to edit the JSP, make sure to save the page as utf-8 type instead of the default ascii. Some html editors are clever enough to do it automatically by inspecting the HTML META tag.

转载于:https://www.cnblogs.com/jerryhong/archive/2009/06/26/1511382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值