jquery Ajax JSP return value garbled two-way solution

Source: Internet
Author: User

jquery ajax Web Effects p/jsp.html target=_blank >jsp Tutorial return value garbled two-way solution in the previous development of Ajax often encounter garbled, today we use jquery ajax+jsp, and out of garbled, Regular test found two kinds of processing jquery ajax garbled problem, see below.

<script>
$ (). Ready (function () {
var url= "/www.111cn.net/ajaxgetnewsclassfather.do";
var pars= "random=" +math.random ();
var newsclassfather= $ (' #newsclassfather ');
$.ajax ({
Url:url,
Data:pars,
Type: "Post",
Success:function (res) {
Newsclassfather.append (RES);
}
});
});

</script>

<%
Ajaxgetnewsclassfather.do

public void Ajaxgetnewsclassfather (writer writer, model model, HTTPS tutorials ervletrequest request, HttpServletResponse Response
{
Response.setcontenttype ("text/html");
Response.setcharacterencoding ("Utf-8");
String Dbname= cookieutil.getdbname (request, response);
List<newsclass> newsclasslist= Newsclassjdbcservice.findbybclassid (dbname,0);
String result= ""; for (Newsclass newsclass:newsclasslist)
{
result+= "<option value= '" + newsclass.getclassid () + "' >" + newsclass.getclassname () + "</option>";
}try
{
Writer.write (result);
}catch (IOException e)
{
E.printstacktrace ();
}
}
/*
The return value is all garbled

Solution One

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

Solve Ajax Scrambling method Two, add a filter

*/

Package com.zlm.tools;
Import java.io.ioexception;
Import Javax.servlet.filterchain;
Import Javax.servlet.filterconfig;
Import javax.servlet.servletexception;
Import Javax.servlet.servletrequest;
Import Javax.servlet.servletresponse;

public class Tounicode implements Javax.servlet.filter {
private string Tounicode = "iso-8859-1";//This is consistent with your web.

public void Destroy () {

}

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
Request.setcharacterencoding (This.tounicode);
Response.setcharacterencoding (This.tounicode);

Chain.dofilter (request, response);

}

public void init (Filterconfig filterconfig) throws Servletexception {

string unicode = Filterconfig.getinitparameter ("Tounicode");

if (Unicode!= null && unicode.length () > 0) {

This.tounicode = Unicode;

}

}

}

%>

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.