gpt4 book ai didi

javascript - 我想知道如何在 jsp servlet web 项目上使用 ajax 和 jquery 获得响应和发布响应

转载 作者:行者123 更新时间:2023-11-30 14:27:01 27 4
gpt4 key购买 nike

我想知道如何从 servlet 获取和发布并使用 jquery ajax,即在 jsp 页面中获取响应并发布它。我已经完成了 doget。如果可能的话,我想从我的 jsp 中删除 jSTL页。请帮助我。在此先感谢

这是我的 Controller 类

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//get the data from database ie the model class
try {
List<Script> scriptitems=modelDBUtil.getScriptList();
request.setAttribute("scriptItems", scriptitems);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/*String itemsfood[]={"biriyani","rice"};
request.setAttribute("itemsfood",itemsfood)*/;



//redirect to a different page
RequestDispatcher dispatcher =request.getRequestDispatcher("scriptviewer.jsp");

dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

}

现在我的jsp页面请帮帮我

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ 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>Menu</title>
</head>
<body>
<p>Script Items</p>

<c:forEach var="items" items="${scriptItems}">


${items.id} ${items.command}
</c:forEach>
<form action="appendfile.jsp" method="post">
<select name="department">
<c:forEach var="item" items="${scriptItems}">
<option value="${item.id}">${item.command}</option>
</c:forEach>
</select>
<button type="submit" id="idsubmit">Submit</button>
</form>




</body>
</html>

最佳答案

这里是如何从 JSP 发送 json 到 Servlete 请引用这段代码有评论

  $("#mForm").submit(function(e) {  //your form ID chane it here 
e.preventDefault();
var $form = $(this);
if (!$form.valid()) return false;

var url = "yourServeleteName";
$.ajax({
type: "POST", //the method you want invoke
url: "yourServeleteName", //Add here your servelete name
data: $("#mForm").serialize(), //your form ID remove default action from your form
success: function (data) //This is to handle if you want any response from Servlete
{
alert(data); //it will alret you message you send from servelete
//$("#mForm")[0].reset(); /if any form you can reset it option
}
});

});
}

关于javascript - 我想知道如何在 jsp servlet web 项目上使用 ajax 和 jquery 获得响应和发布响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51758435/

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