java+ajax提交后乱码与返回乱码解决

 login.jsp代码清单如下:
<% @ page language="java" contentType="text/html;charset=utf-8" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
  
< head >
    
< title > login title >
    
< script  language ="javascript" >
        
//创建xmlhttprequest对象
        var xmlHttp = false;
        
try{
            xmlHttp 
= new ActiveXObject("Msxml2.XMLHTTP");
        }
catch(e){
            
try{
                xmlHttp 
= new ActiveXObject("microsoft.XMLHTTP");
            }
catch(e1){
                
try{
                    xmlHttp 
= new XMLHttpRequest();
                }
catch(e2){
                    xmlHttp 
= false;
                }
    
            }

        }

        
if(!xmlHttp)
        
{
            alert(
"server is error");
        }

        
function login()
        
{
            
var userName = document.getElementById("userName");
            
var pwd = document.getElementById("pwd");
            
if(userName.value == 0 || pwd.value == 0)
            
{
                alert(
"请输入用户名或密码!");
                userName.focus();
                
return false;
            }

            
else
            
{
                
//注意这个提交的地址,传递的参数都放在encodeURI()函数当中,它把字符串做为一个uri进行编码,最大的一个好处就是可以处理中文乱码
                var url="http://localhost:8080/ajaxXML/loginServlet?userName="+encodeURI(userName.value)+"&pwd="+encodeURI(pwd.value);
                xmlHttp.open(
"GET",url,true);
                xmlHttp.onreadyStatechange 
= show;
                xmlHttp.send(
null);
                document.getElementById(
"message").innerHTML="正在登录,请稍后..........";
            }

        }

        
function show()
        
{
            
if(xmlHttp.readyState == 4)//响应与完成,可以访问服务器并响应它
            {
                
if(xmlHttp.status == 200)//响应状态一切顺利
                {
                    
var xmlDOM = xmlHttp.responseText;                
                    document.getElementById(
"message").innerHTML = xmlDOM;
                }

            }

        }

    
script >
    
< style  type ="text/css" >
    
    
style >
  
head >
  
< body >
  
< center >
      
< div  id ="message" > div >
      
< table  align ="center"  cellPadding ="0"  cellSpacing ="1"  bgcolor ="blue"  width ="180" >
      
< tr  bgcolor ="#ffffff" >
        
< td > userName: td >< td >< input  type ="text"  id ="userName"  name ="userName" /> td >      
      
tr >
      
< tr  bgcolor ="#ffffff" >
        
< td > password: td >< td >< input  type ="password"  id ="pwd"  name ="pwd" /> td >      
      
tr >
      
< tr  bgcolor ="#ffffff"  align ="center" >
          
< td  colspan ="2" >< input  align ="center"  type ="button"  value ="login"  onclick ="return login();" /> td >
      
tr >
      
table >
  
center >
  
body >
html >
LoginServlet 代码如下:
public   void  doGet(HttpServletRequest request, HttpServletResponse response)
            
throws  ServletException, IOException  {
        
//注意这句代码,设置服务器响应客户端时的编码格式,注意要写GBK
        response.setContentType("text/html;charset=GBK");
        PrintWriter out 
= response.getWriter();
        String userName
=request.getParameter("userName");
        String pwd 
= request.getParameter("pwd");
        
if(userName.equals("陈涛"&& pwd.equals("chen")){
            out.print(
"登录成功");
        }
else{
            out.print(
"登录失败");
        }

        out.flush();
        out.close();
    }

有兴趣的朋友可以试一下,不管是请求处理乱码还是响应信息乱码都可以处理,如果在请求时还是出现乱码问题,如果提交的是GET方法在servlet 的 doGet 方法中加入如下代码,

userName = new String(userName.getBytes("iso-8859-1"),"gbk");jsp页面不用改动;

有需要的朋友可在在此地址下载:http://d.download.csdn.net/down/383565/ctojxzsycztao

你可能感兴趣的:(java+ajax提交后乱码与返回乱码解决)