我正在編寫一個與常規jsp-servlet交互正常工作的程序。但是,當我提交相同的Ajax,它不能正常工作。以下是我的代碼。無法從servlet傳遞數據
JSP-Servlet的(W/O的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>
控制器
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)
},
});
});
控制器
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);
轉到另一頁。在我的情況下會有什麼替代方案? 請讓我知道我哪裏錯了,我該如何解決這個問題。
Hi @wero,感謝您的快速回復。我創建了一個參考樣本,如果我不想發送任何參數,例如觸發查詢'select * from table'並在'index1.jsp' – user3872094
@ user3872094中打印結果,您不需要發送任何數據,請求體可以是空的。但是如果你希望'request.getParameter()'在服務器上返回一些值,你應該發送一些數據。 – wero
否則我會在某人後張貼原件。再次感謝:-) – user3872094