我试图在servlet中获取参数,但我收到的所有东西都是null值。 我的jsp代码:
<form id="loginForm" name="loginForm" class="loginForm" method="POST" >
<table cellpadding="10px" border="0">
<tr>
<td class="textLogin">Username :</td>
<td class="inputColumn">
<input id="inputUserName" class="inputText" type="text" name="username" value="Username"
onfocus="if(this.value=='Username'){this.value='';}" onblur="if(this.value==''){this.value='Username';}"/>
</td>
</tr>
<tr>
<td class="textLogin">Password :</td>
<td class="inputColumn">
<input id="inputPassword" class="inputText" type="password" name="password" value=""
onfocus="" onblur=""/>
</td>
</tr>
<tr>
<td class="textLogin" colspan="2">
<span id="noityText" ></span>
</td>
</tr>
<tr>
<td class="resetColumn"><button class="basicButton" id="resetLogin" type="reset" >Reset</button></td>
<td><button class="basicButton" id="submitLogin" type="submit" >Submit</button></td>
</tr>
</table>
</form>
JavaScrpit:
$(document).ready(function() {
$("#loginForm").submit(function(e){
e.preventDefault();
});
$("#submitLogin").click(function(){
var userName=$("#inputUserName").val();
userName=userName.trim();
var password=$("#inputPassword").val();
password=password.trim();
//get the form data and then serialize that
dataString = $("#loginForm").serialize();
dataString ="username"+userName;
console.log(dataString);
if((userName=='')||(userName=='Username')){
//$("#noityText").html("<span id='noityText' >Vui lòng điền tên đăng nhập</span>");
$("#noityText").text("Vui lòng điền tên đăng nhập");
}
else{
$("#noityText").text("");
}
$.ajax({
type:"POST",
url:"/MavenWebApp/login",
data:dataString,
dataType:"json",
success:function(data,textStatus, jqXHR){
if(data.success){
$("#noityText").text("Đăng nhập thành công :D");
console.log(data);
}
else{
$("#noityText").text("Đăng nhập thất bại :D");
console.log(data);
}
},
error:function(jqXHR,textStatus, errorThrow){
$("#noityText").text("Errors");
},
beforeSend:function(jqXHR,settings){
$("#noityText").text("Start send");
},
complete:function(jqXHR,textStauts){}
});
});
});
和servlet:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("In servlet :D ");
String userName=request.getParameter("username");
//userName=userName.trim();
String password=request.getParameter("password");
//password=password.trim();
System.out.println(password);
PrintWriter out = response.getWriter();
response.setContentType("text/html");
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
response.setHeader("Access-Control-Max-Age", "86400");
Gson gson = new Gson();
JsonObject myObj = new JsonObject();
if (this.getUser(userName, password)){
myObj.addProperty("success", true);
}else{
myObj.addProperty("success", false);
}
out.println(myObj.toString());
out.close();
}
这是eclipse中的文本显示:
In servlet :D - service
null
null
servlet中的字符串userName和密码为null!我怎么能解决这个错误? :d 对不起,如果我的问题让你不舒服,因为我是初学者,我的英语不好:D
答案 0 :(得分:0)
//get the form data and then serialize that
dataString = $("#loginForm").serialize();
看起来没问题(但检查这个字符串是什么),但是然后用
覆盖它 dataString ="username"+userName;
为什么要覆盖它?如果这样做,您至少需要保持表单编码(例如,参数名称和值之间需要=
)。
答案 1 :(得分:0)
尝试使用serializeArray() -
dataString = $("#loginForm").serializeArray();
删除此行 -
dataString ="username"+userName;