后端使用Servlet的request.getparameter()方法接收不到参数
在做Ajax发送POST请求时,碰到了后端Servlet接收不到参数的问题。
下面附上代码:
verify.jsp文件代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录验证</title>
</head>
<body>
<!--这里不需要放在form标签内,因为下面已经用了URL请求参数-->
用户名<input type="text" id="username" name="username"><br><br>
密码 <input type="password" id="password" name="password"><br><br>
<button type="button" id="btn" onclick="userVerify()">登录</button>
<label id="result"></label>
<script>
var xhr = false;
function createXHR(){
try{
xhr = new XMLHttpRequest();
}catch(e){
try{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e1){
xhr = false;
}
}
if(!xhr)
alert("初始化XMLHttpRequest对象失败!");
}
function userVerify(){
//获取界面上的元素value
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
//通过XMLHttpRequest发送POST请求
createXHR();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200)
document.getElementById("result").innerHTML = xhr.responseText;
}
xhr.open("POST","LoginServlet");
xhr.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
xhr.send("username=" + username + "& password=" + password);
}
</script>
</body>
</html>
LoginServlet.java文件代码
package com.mialab.ajax.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置请求和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取请求参数
String username = request.getParameter("username");
String pwd = request.getParameter("password");
//登录验证
if(username==null||pwd==null||username.equals("")||pwd.equals("")) {
out.print("用户名或密码不能为空!");
}
else if("admin".equals(username) && "123".equals(pwd) ) {
out.print("登陆成功");
}
else
out.print("用户名或密码错误!");
}
}
结果:不论输入什么,都显示用户名或密码能为空!

显然,后端并没有接收到username和password两个参数的值。
百度了很久request.getparameter取不到值的原因——①没有用<form>表单提交,但是这里下面已经用了URL请求参数,所以是没必要用<form>的;②没有设置name属性;③没有设置请求头xhr.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");④enctype="application/x-www-form-urlencoded"是默认的编码方式。若以<form enctype="multipart/form-data">这种方式提交数据,就要用request.getInputStream()或request.getReader()来获取提交的数据 ,用 request.getParameter()是获取不到提交的数据的。
问题:因为这里用了URL请求参数了,所以没有必要再使用<form>表单提交数据了,所以排除第①个原因。很明显也不是其它三个原因。所以花了两天都没有找到错误真的是愁死了,最后还是请教了大佬,看了半天才发现是xhr.send()传参数时&后面多加了一个空格的问题。
因为编码习惯问题喜欢用空格来分隔,但是&与变量之间不能有空格,加了空格就传不了参了,这里前端和后端都传不了。
解决方法:去掉&与password之间的空格,即

改正后的结果显示:
- 用户名或密码错误测试

- 用户名和密码正确测试

本文介绍了后端使用Servlet的request.getParameter()方法接收不到POST请求参数的问题。在Ajax发送请求时,发现无论输入什么,始终提示用户名或密码为空。经过排查,发现错误在于URL请求参数中,&符号与变量之间存在空格导致传参失败。解决方案是删除&符号前后的空格,确保正确的URL格式。
连接且不能有空格!!!&spm=1001.2101.3001.5002&articleId=99235294&d=1&t=3&u=e301725e0e914893b814186efe0c15e8)
1835

被折叠的 条评论
为什么被折叠?



