2016-02-18 57 views
0

我正在編寫一個與常規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);轉到另一頁。在我的情況下會有什麼替代方案? 請讓我知道我哪裏錯了,我該如何解決這個問題。

回答

2

在Ajax的情況下,您不會發送任何表單數據。使用$.serialize連載的形式數據並將其發送到服務器:

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

Hi @wero,感謝您的快速回復。我創建了一個參考樣本,如果我不想發送任何參數,例如觸發查詢'select * from table'並在'index1.jsp' – user3872094

+0

@ user3872094中打印結果,您不需要發送任何數據,請求體可以是空的。但是如果你希望'request.getParameter()'在服務器上返回一些值,你應該發送一些數據。 – wero

+0

否則我會在某人後張貼原件。再次感謝:-) – user3872094

0

加入FORMDATA在你的js後檔
檢查瀏覽器控制檯

你可以找到你的整個index1.jsp在控制檯中打印的html內容 與$ {name}替換爲您輸入的名稱。
AJAX建議呼叫保持在同一頁面中,並在呼叫後不更新頁面的情況下更新頁面內容。
在上面的AJAX代碼中,即使在AJAX調用完成後,您也只在index.jsp中。
您可以使用JavaScript window.location.href = <文件名>切換到其他頁面(index1.jsp你的情況)AJAX調用後。但在你的情況下,頁面切換到index1.jsp但作爲新的請求。您可以在網址欄瀏覽器中觀察到相同的結果。因爲您在請求範圍中存儲名稱屬性。您不會看到您在index1.jsp的名稱字段中輸入的值。

+0

你好,這是目前發生在我的情況,數據打印與控制檯中的值。我正在談論我的實際文件。我怎樣才能解決這個問題? – user3872094