我需要使用java从我的Web应用程序中获取我的Model项目的属性,但我还需要发送和整数作为参数。我读了JQuery API Doc,但我对AJAX和JQuery很新,我仍然觉得很难理解。
这是我的代码:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
int x; // x = parameter recieved from AJAX
//data is an instance from the Model class
String text = data.getNews().getNewsInPosition(x).getTitle(); //I send correct postition to my ArrayList
response.setContentType("text/html"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8");
response.getWriter().write(text);
}
这是我的ServletControlB doGet函数:
{{1}}
我该如何解决这个问题?有没有更好的方法来解决这个问题?
答案 0 :(得分:0)
您可以将作为String接收的请求参数解析为整数。
String x = request.getParameter("yourParameterName"); //get the parameter as String
int x1 = Integer.parseInt(x); //set the parameter here
答案 1 :(得分:0)
尝试使用jquery.get()的data object
,
$(document).ready(function () {
$.get('ServletControlB',{id:1}, //<-- data object having id as key
function (responseText) {
$('#divnombre').text(responseText);
});
});
在java中使用,
int x = Integer.parseInt(request.getParameter("id"));
答案 2 :(得分:0)
当您从jquery发送GET请求时,请在URL中附加查询参数:
$.get('ServletControlB?yourparam=paramValue', function (responseText)
你可以在servlet中检索doGet,如下所述:
String yourParamValue = request.getParameter("yourparam");
答案 3 :(得分:0)
valor
控制器中的执行以下操作
$(document).ready(function () {
// Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...
$.get('ServletControlB?x=' + valueOfX, function (responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
$('#divnombre').text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
});
答案 4 :(得分:0)
我认为你必须改变你的前端,如下所示。
$.get('ServletControlB',{ param: '1' }, function (responseText) {
$('#divnombre').text(responseText);
});