从servlet中传值回JSP页面,数字和英文字母都显示,如果是汉字就会显示一串问号?
运行的时候显示出一串问号?如果是中文乱码的问题,那该如何解决呀!servlet代码:publicvoiddoGet(HttpServletRequestrequest,H...
运行的时候显示出一串问号?如果是中文乱码的问题,那该如何解决呀!
servlet代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("This ok!");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setHeader("Cache-Control", "no-cache");
String username = request.getParameter("username");
StringBuffer sb=new StringBuffer();
// sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
sb.append("username has exist");
System.out.println("username:"+username);
out.write(sb.toString());
out.close();
}
Jsp页面:
<script language="JavaScript">
var httpRequest;
function createHttpRequest(){
if(window.XMLHttpRequest){
httpRequest=new XMLHttpRequest();
}}
function check()
{
createHttpRequest();
var username=document.getElementById("username").value;
var url="servlet/TestServlet?username="+username;
httpRequest.open("get",url,true);
httpRequest.onreadystatechange=showResult;
httpRequest.send();}
function showResult(){
if(httpRequest.readyState==4){
if(httpRequest.status==200){
var result=httpRequest.responseText;
alert(result);
var checkResult=document.getElementById("checkResult").innerHTML="<B>"+result+"</B>";
}}}
</script>
<form name="myForm">
<table>
<tr><td>"用户名</td>
<td><input type="text" name="username" id="username" onblur="check()"></td>
</tr>
<tr><td>
<input type="text" name="password" name="password" ></td></tr>
</table>
</form>
<span id="checkResult"></span> 展开
servlet代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("This ok!");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setHeader("Cache-Control", "no-cache");
String username = request.getParameter("username");
StringBuffer sb=new StringBuffer();
// sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
sb.append("username has exist");
System.out.println("username:"+username);
out.write(sb.toString());
out.close();
}
Jsp页面:
<script language="JavaScript">
var httpRequest;
function createHttpRequest(){
if(window.XMLHttpRequest){
httpRequest=new XMLHttpRequest();
}}
function check()
{
createHttpRequest();
var username=document.getElementById("username").value;
var url="servlet/TestServlet?username="+username;
httpRequest.open("get",url,true);
httpRequest.onreadystatechange=showResult;
httpRequest.send();}
function showResult(){
if(httpRequest.readyState==4){
if(httpRequest.status==200){
var result=httpRequest.responseText;
alert(result);
var checkResult=document.getElementById("checkResult").innerHTML="<B>"+result+"</B>";
}}}
</script>
<form name="myForm">
<table>
<tr><td>"用户名</td>
<td><input type="text" name="username" id="username" onblur="check()"></td>
</tr>
<tr><td>
<input type="text" name="password" name="password" ></td></tr>
</table>
</form>
<span id="checkResult"></span> 展开
2个回答
展开全部
乱码方法有很多种,jsp和servlet中处理乱码的方法都不一样
jsp中:①在程序最顶端加上语句<%@ page contentType="text/html;charset=gbk"%>
②在你接收参数的上面加上request.setCharacterEncoding ("gbk");
③在你的页面中加上response.setContentType ("text/html;charset=gbk");
servlet中:①写入response.setContentType ("text/html;charset=gbk");
②request.setCharacterEncoding ("gbk");
一般这样做,你的程序就不会有乱码了,补充一下,request是处理提交参数的乱码,response是处理页面乱码
jsp中:①在程序最顶端加上语句<%@ page contentType="text/html;charset=gbk"%>
②在你接收参数的上面加上request.setCharacterEncoding ("gbk");
③在你的页面中加上response.setContentType ("text/html;charset=gbk");
servlet中:①写入response.setContentType ("text/html;charset=gbk");
②request.setCharacterEncoding ("gbk");
一般这样做,你的程序就不会有乱码了,补充一下,request是处理提交参数的乱码,response是处理页面乱码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询