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

标签 javascript jquery ajax jsp servlets

我想知道如何从 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/

相关文章:

javascript - 使用 javascript 通过用户输入搜索数组

javascript - 异步提交表单以释放 UI

javascript - 在javascript中创建的div结构与实际的DOM不同

javascript - 为什么我的决策树不起作用?

javascript - 返回相应字符串的数组 int 值

javascript - JavaScript 数组的数据无需使用 push() 函数即可复制到另一个数组

javascript - 如何使用 Angular4 将参数传递到服务中

javascript - 为什么此输入始终保持可点击状态?

jQuery load() a href 链接加载到 div 中,不与我的函数交互,但该函数在初始页面上工作

javascript - 如何将 Net 中的日期时间转换为 JavaScript