gpt4 book ai didi

java - 无法从servlet获取传递的数据

转载 作者:行者123 更新时间:2023-12-02 03:55:42 26 4
gpt4 key购买 nike

我正在编写一个可以与常规 jsp-servlet 交互正常工作的程序。但是当我用 ajasx 提交相同的内容时,它无法正常工作。下面是我的代码。

JSP-Servlet(不带 Ajax)

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="f1" id="f1" method="post" action="Controller">
<input type="text" name="name1" id="name1" /> <input type="submit" />
</form>
</body>
</html>

Controller

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nsme = request.getParameter("name1");
request.setAttribute("name", nsme);
request.getRequestDispatcher("index1.jsp").forward(request, response);
}

index1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="text" value="${name}" />
</body>
</html>

使用 Ajax

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
</head>
<body>
<form name="f1" id="f1">
<input type="text" name="name1" id="name1" /> <input type="submit"
name="x" id="x" />
</form>
<script type="text/javascript" src="SampleJS.js"></script>
</body>
</html>

SampleJS.js

$('#x').click(function() {
$.ajax({
type : 'POST',
url : 'Controller',
success : function(data) {
console.log(data)
},
});
});

Controller

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nsme = request.getParameter("name1");
request.setAttribute("name", nsme);
request.getRequestDispatcher("index1.jsp").forward(request, response);
}

index1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="text" value="${name}" />
</body>
</html>

这里,当我使用第一种方法时,名称打印正确。当我将它与 AJAX 一起使用时,令我惊讶的是文本框中没有打印任何内容。

基本上,我们使用request.getRequestDispatcher("GetCaseData.jsp").forward(request, response);转到另一个页面。在我的情况下,它的替代方案是什么?请让我知道我哪里出了问题以及如何解决这个问题。

最佳答案

在 Ajax 情况下,您不发送任何表单数据。使用$.serialize序列化表单数据并发送给服务器:

$('#x').click(function() {
var formData = $("#f1").serialize();
$.ajax({
type : 'POST',
url : 'Controller',
data : formData,
success : function(data) {
console.log(data)
},
});
});

关于java - 无法从servlet获取传递的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35485531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com