日期:2014-05-17  浏览次数:20747 次

ajax技术,空值,request.getParameter("name");name=null;
在jsp中运用ajax技术,
这是jsp中代码:
<script src="js/login.js" type="text/javascript"></script>

  <div id = "logindiv" >
  <form name = "loginForm" method="post" action="">
  用户名:<input type="text" name="user" > &nbsp; <input type="button" value="登录" onclick="login()"><br>
  密 码: <input type="password" name="password"> 

这是js的内容:
var xmlHttp = false;
  
  function init(){
  if(window.XMLHttpRequest){
  xmlHttp = new XMLHttpRequest();
  }else if(window.ActiveXObject){
  try{
  xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
  }catch(e){
  try{
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }catch(e){
  window.alert("该浏览器不支持Ajax");
  }
  }
  }
  }

function login(){
  var name=document.loginForm.user.value;
  var word = document.loginForm.password.value;
  var url = "com.music.servlet/LoginServlet?name =" + name +
  "&word =" + word;
  if(name == ""){
  alert("用户名不能为空");
  document.loginForm.user.focus();
  return;}else if(word == ""){
  alert("请输入密码");  
  document.loginForm.password.focus();
  return;}  
   
  xmlHttp.open("post",url,true);
  xmlHttp.onreadystatechange = function(){
  if(xmlHttp.readyState == 4){
  logindiv.innerHTML = xmlHttp.responseText;
  } else{
  logindiv.innerHTML = "正在登陆,请稍侯……";}
  };  
  xmlHttp.send();
  return;  
  }

servlet内容:
  String name = request.getParameter("name");//name=null
  String password = request.getParameter("word");//password=null
  String loginState = "Fail";
  String targetUrl = "/jsp/loginFail.jsp";
  //验证用户
  .......
  //登录失败
  request.setAttribute("loginSate",loginState);
  ServletContext application = this.getServletContext();
  RequestDispatcher rd = application.getRequestDispatcher( targetUrl);
  rd.forward(request, response);
}

问题所在将项目放在tomcat中运行 
name=null
password=null即不能通过request.getParameter获取到相应值

------解决方案--------------------
你的代码写到了
doPost方法中?
告诉你,肯定是null这个没有问题。
------解决方案--------------------
哦原来是这样name =
------解决方案--------------------
<body onload="init()">


<script type="text/javascript">
var xmlHttp = null;
 
function init(){
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
alert(xmlHttp);
}

function login(){
va