gpt4 book ai didi

java - JSP Ajax Servlet - 在 JSP 中获取返回值

转载 作者:行者123 更新时间:2023-11-30 06:20:32 25 4
gpt4 key购买 nike

我有两个表,当我点击第一个时,其中一个表被加载了。所以我在我的 JSP 中使用 Ajax 来做到这一点:

  $("#tablesorter-demo tr").click(function(){
$('#tablesorter-demo tr').not(this).removeClass('hilite');
$(this).toggleClass('hilite');

$.ajax({
type: "post",
url: "gerer_cf_ad",
data: "name=test",
success: function(data){

}
});
});

在我的 JAVA 中:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String forward = "/geCfAd/acc_getes.jsp";

try {
if(request.getParameter("name") != null){
codeCentrale = "5874";
libelle = "CApes";
request.setAttribute("codeCentrale ",codeCentrale );
request.setAttribute("libelle",libelle);
}getServletContext().getRequestDispatcher(forward).forward(request, response);
} catch (Exception e) {
e.getMessage();
}
}

所以,我想在我的 JSP 中获取值:libelle 和 codeCentrale,以便将其添加到 may 表中,但我不能..

我的表格代码:

 <td id="td_codeCentrale SPCentrale" > <% if(request.getAttribute("codeCentrale ") != null){
out.print(request.getAttribute("codeCentrale "));
} %></td>
<td id="td_libelleCentrale" ><% if(request.getAttribute("libelle") !=null){
out.print(request.getAttribute("libelle"));
}%></td>


最佳答案

您可以通过两种方式获取它。

1) 从请求或 session 中获取

在JSP页面中编写如下代码获取值。

String value = session.getAttribute("libelle"); OR
String value = request.getAttribute("libelle");

2) 您可以在 Ajax 响应中传递值。

在 Servlet 上编写如下代码。

out.println(libelle);

在 Ajax 中,您将获得响应数据。

$.ajax({
type: "post",
url: "gerer_caf_adherente",
data: "name=test",
success: function(data){
alert(data); // your response will print here.

}
});

关于java - JSP Ajax Servlet - 在 JSP 中获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21676212/

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