java request.getParameter获取不到文本框的值
js代码functionisRightCode(){varcode=$("#veryCode").attr("value");code="c="+code;$.ajax(...
js代码
function isRightCode() {
var code = $("#veryCode").attr("value");
code = "c=" + code;
$.ajax( {
type : "POST",
url : "resultServlet",
data : code,
success : callback
});
}
function callback(data) {
$("#info").html(data);
}
页面verifyCode.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">
<script type="text/javascript"src="js/verifyCode.js"></script>
<script type="text/javascript"src="js/jquery.js"></script>
<title>test verify code</title>
</head>
<body>
<input id="veryCode"name="veryCode"type="text" />
<img id="imgObj"alt="" src="verifyCodeServlet"/>
<a href="#"onclick="changeImg()">换一张</a>
<input type="button"value="验证" onclick="isRightCode()"/>
<div id="info"></div>
</body>
</html>
产生结果servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass ResultServlet extends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
the request send by the client tothe server
publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String validateC = (String) request.getSession().getAttribute(
"validateCode");
String veryCode = request.getParameter("veryCode");
PrintWriter out = response.getWriter();
if (veryCode == null || "".equals(veryCode)) {
out.println("验证码为空");
} else {
if (validateC.equals(veryCode)) {
out.println("验证码正确");
} else {
out.println("验证码错误");
}
}
out.flush();
out.close();
}
}
WEB.XML配置无误
java request.getParameter打印出来为null 展开
function isRightCode() {
var code = $("#veryCode").attr("value");
code = "c=" + code;
$.ajax( {
type : "POST",
url : "resultServlet",
data : code,
success : callback
});
}
function callback(data) {
$("#info").html(data);
}
页面verifyCode.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">
<script type="text/javascript"src="js/verifyCode.js"></script>
<script type="text/javascript"src="js/jquery.js"></script>
<title>test verify code</title>
</head>
<body>
<input id="veryCode"name="veryCode"type="text" />
<img id="imgObj"alt="" src="verifyCodeServlet"/>
<a href="#"onclick="changeImg()">换一张</a>
<input type="button"value="验证" onclick="isRightCode()"/>
<div id="info"></div>
</body>
</html>
产生结果servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass ResultServlet extends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
the request send by the client tothe server
publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String validateC = (String) request.getSession().getAttribute(
"validateCode");
String veryCode = request.getParameter("veryCode");
PrintWriter out = response.getWriter();
if (veryCode == null || "".equals(veryCode)) {
out.println("验证码为空");
} else {
if (validateC.equals(veryCode)) {
out.println("验证码正确");
} else {
out.println("验证码错误");
}
}
out.flush();
out.close();
}
}
WEB.XML配置无误
java request.getParameter打印出来为null 展开
2个回答
展开全部
你在Servlet取的时候,名称是:“veryCode”,而在Javascript中拼装时用的是 "c". 应该统一就可以了。
javascrpt:
var code = $("#veryCode").attr("value");
code = "veryCode=" + code;
servlet:
String veryCode = request.getParameter("veryCode");
追问
谢谢! 问题已解决
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
request.getParameter("veryCode"); 改成 request.getParameter("c");
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询