【问题标题】:How to send data to servlet using ajax without a submitting form如何在没有提交表单的情况下使用 ajax 将数据发送到 servlet
【发布时间】:2015-09-19 01:01:42
【问题描述】:

我是 servlet 的新手,我能够从 servlet 获取数据,但无法向其发送数据,我想在不使用提交表单的情况下执行此操作,请给我一些帮助

单击按钮后,它将转到 servlet 并返回文本,但不返回发送给它的值

这是我的 index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <title>SO question 4112686</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function() {                       
            $('#somebutton').click(function() {               
                $.get('GetUserServlet', function(responseText) { 
                    $('#somediv').text(responseText);        
                });
            });
        });
        $("#somebutton").click(function(){
        $.ajax
        (
        {
            url:'GetUserServlet',
            data:{name:'abc'},
            type:'get',
            cache:false,
            success:function(data){alert(data);},
            error:function(){alert('error');}
        }
    );
}
);
    </script>
</head>
<body>

    <button id="somebutton" onclick="showHint('GetUserServlet.java',   'travis');">press here</button>
    <div id="somediv"></div>
</body>

这是我的 servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "Update Sucessful";
String name = request.getParameter("name");


response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write( name + text);       // Write response body.

【问题讨论】:

  • 以前没用过,但我愿意尝试
  • 再看一遍,你已经是 :) 那些 $ 是 jQuery 调用。所以你已经在使用 jQuery.ajax() 函数了。给我一些来看看我现有的 ajax/servlet 交互以记住它是如何工作的:)

标签: java ajax jsp servlets


【解决方案1】:

您可以在此处使用 $.ajax() 或 $.post。因为你使用了 $.ajax()。请参考以下更正:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>SO question 4112686</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function() {                       
            $('#somebutton').click(function() {               
                $.get('GetUserServlet', function(responseText) { 
                    $('#somediv').text(responseText);        
                });
            });
        });


        $("#somebutton").click(function(){
         $.ajax({
            url:'GetUserServlet',
            data:{name:'abc'},
            type:'get',
            cache:false,
            success:function(data){
               alert(data);
               $('#somediv').text(responseText); 
            },
            error:function(){
              alert('error');
            }
         }
    );
}
);
    </script>
</head>
<body>

    <button id="somebutton">press here</button>
    <div id="somediv"> </div>
</body>

你的 servlet 应该是:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

public class GetUserServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    doPost(request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    String text = "Update successfull"; //message you will recieve 
    String name = request.getParameter("name");
    PrintWriter out = response.getWriter();
    out.println(name + " " + text);
  }

【讨论】:

    【解决方案2】:

    您可以为此目的使用 $.post 方法。
    这是我的解决方案
    index.jsp

    <!DOCTYPE html><html lang="en">
    <head>
       <title>SO question 4112686</title>
       <script src="http://code.jquery.com/jquery-latest.min.js"></script>
       <script>
         $(document).ready(function() {
             $("#somebutton").click(function() {
                 servletCall();
             });
    
         });
         function servletCall() {
             $.post(
                 "GetUserServlet", 
                 {name : "Message from jsp"}, //meaasge you want to send
                 function(result) {
                 $('#somediv').html('Here is your result : <strong>' + result + '</strong>'); //message you want to show
             });
         };
       </script>
    </head>
    <body>
         <button id="somebutton">press here</button>
         <div id="somediv"></div>
    </body>
    </html>
    

    GetUserServlet.java

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class GetUserServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String text = "<br>Message from servlet<br>"; //message you will recieve 
        String name = request.getParameter("name");
        PrintWriter out = response.getWriter();
        out.println(text + name);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2015-01-03
      • 2016-12-26
      • 2015-04-24
      相关资源
      最近更新 更多