Garbled characters in Ajax and JSP values

Source: Internet
Author: User

1. jsp header <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>

2. Request. setcharacterencoding ("UTF-8"); character encoding of the request submitted on the JSP page

3. the header information of the JSP page Processing request is the same as that of the first and second

4. transcode the request on the page

<%    String err = request.getParameter("err");    byte[] b = err.getBytes("iso-8859-1");    err = new String(b,"UTF-8");    response.setCharacterEncoding("utf-8");    out.println(err);    System.out.println(err);%>

5. Pay attention to the location relationship between response. setcontenttype ("text/html; charset = UTF-8"); and printwriter out = response. getwriter,

Remember to put printwriter out = response. getwriter (); behind response. setcontenttype ("text/html; charset = UTF-8"); otherwise, the encoding will be invalid.

The problem is fixed. I don't know why. If I don't transcode it, garbled characters will occur. The reason is unclear! I am so happy now. I feel like this world is wonderful ~

 

The following is an example of the input username for Ajax verification. Ajax will pass the incorrect Chinese information to the Ajax. jsp page. Ajax. jsp will accept the information and return the Chinese information to Ajax.

Index. jsp

 

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; request. setcharacterencoding ("UTF-8"); %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

Ajax. jsp processing requests

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%    String err = request.getParameter("err");    byte[] b = err.getBytes("iso-8859-1");    err = new String(b,"UTF-8");    response.setCharacterEncoding("utf-8");    out.println(err);    System.out.println(err);%>

 

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.