Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我想知道如何在JSPServlet web项目上使用ajax和jquery获得响应和post响应_Javascript_Jquery_Ajax_Jsp_Servlets - Fatal编程技术网

Javascript 我想知道如何在JSPServlet web项目上使用ajax和jquery获得响应和post响应

Javascript 我想知道如何在JSPServlet web项目上使用ajax和jquery获得响应和post响应,javascript,jquery,ajax,jsp,servlets,Javascript,Jquery,Ajax,Jsp,Servlets,我想知道如何从servlet中执行get和post,并在jsp页面中使用jquery ajax来获取响应和发布。我已经完成了doget。如果可能的话,我还想从jsp页面中删除jstl。请帮助我。并提前感谢 这是我的控制器类 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-gen

我想知道如何从servlet中执行get和post,并在jsp页面中使用jquery ajax来获取响应和发布。我已经完成了doget。如果可能的话,我还想从jsp页面中删除jstl。请帮助我。并提前感谢

这是我的控制器类

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);
  }

}

}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
//从数据库(即模型类)获取数据
试一试{
List scriptitems=modelDBUtil.getScriptList();
setAttribute(“scriptItems”,scriptItems);
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
/*字符串itemsfood[]={“biriyani”,“rice”};
request.setAttribute(“itemsfood”,itemsfood)*/;
//重定向到其他页面
RequestDispatcher=request.getRequestDispatcher(“scriptviewer.jsp”);
转发(请求、响应);
}
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
//TODO自动生成的方法存根
doGet(请求、响应);
}
}
}
现在我的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> 

菜单
脚本项

${items.id}${items.command} ${item.command} 提交
以下是如何从我的jsp页面中删除jstl并使其成为html

您需要使用这些jquery cdn,也可以直接使用javascript

另外,您需要下载Gson库以从servlet获得响应。 此代码将使用
get
方法

这是您可以将
项目ood
发送到
html或jsp
将您的
doGet
方法修改为:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<Script> scriptitems=modelDBUtil.getScriptList();
        request.setAttribute("scriptItems", scriptitems);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    String itemsfood[]={"biriyani","rice"};
    List<String> list = Arrays.asList(itemsfood);
    String json = new Gson().toJson(list);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json); 

}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
试一试{
List scriptitems=modelDBUtil.getScriptList();
setAttribute(“scriptItems”,scriptItems);
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(SQLE异常){
e、 printStackTrace();
}
字符串itemsfood[]={“biriyani”,“rice”};
List List=Arrays.asList(itemsfood);
字符串json=new Gson().toJson(列表);
setContentType(“应用程序/json”);
响应。setCharacterEncoding(“UTF-8”);
response.getWriter().write(json);
}
和HTML或jsp代码,即:


$(文件)。准备好了吗(
函数(){
$.get(“appendfile或(在此处添加您的serveletName)”,函数(
回应(约翰逊){
var$select=$(“#项”);
$select.find(“option”).remove();
$.each(responseJson,函数(值,键){
$(“”).val(键).text(键).appendTo($select);
});
});
});
菜单
选择
提交

以下是如何将json从JSP发送到Servlet的方法。请参阅此代码 评论

  $("#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 
          }
        });

      });
    }

您的问题和代码不清楚。你在问什么?你能再解释一下你想说什么吗?请花点时间阅读帮助页面,我已经编辑过了…现在你能帮我写一篇关于ajaxYes的帖子吗,你想用post做什么?是的,我想用servlet发布表单数据并在mysql数据库中更新请帮助@v8-E我无法发送到servlet中的doPost方法解析json,我想我需要关于如何使用ajax从jsp页面发送json的帮助。谢谢你帮助我找到第二个答案,但你没有告诉我如何解析它通过doPostAnd中的gson,非常感谢您对我的帮助。对于每一个疑问@v8 EI都不明白您想说什么,这段代码将把您的所有数据从JSP发送到Servlet,而不是JSP。您可以使用HTML。我的建议是使用HTML而不是JSP。这段代码也适用于JSP。如果您的问题是关于解析的,则不需要库来完成,只需下载
Gson Jar
并将其添加到构建路径。阅读更多关于Gaon,Happy Codding的信息要获取这些值,请使用
request.getParameter(“您在JSP中使用的表单字段的确切名称”)
在运行代码时,我不是从表单中的标记中获取所选值,而是获取整个get-request部分。您的帮助非常宝贵@-v8-E