Java 如何使用ajax将数据发送到servlet而无需提交表单

Java 如何使用ajax将数据发送到servlet而无需提交表单,java,ajax,jsp,servlets,Java,Ajax,Jsp,Servlets,我是servlet新手,我可以从servlet获取数据,但无法向它发送数据,我想在不使用提交表单的情况下执行此操作,我可以获得一些帮助吗 单击按钮,它将转到servlet并返回文本,但不返回发送给它的值 这是我的index.jsp <!DOCTYPE html> <html lang="en"> <head> <title>SO question 4112686</title> <script src="http:

我是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>

为此,您可以使用$.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>

所以问题4112686
$(文档).ready(函数(){
$(“#somebutton”)。单击(函数(){
servletCall();
});
});
函数servletCall(){
美元邮政(
“GetUserServlet”,
{name:“来自jsp的消息”},//要发送的消息
功能(结果){
$('#somediv').html('这是您的结果:'+result+'');//要显示的消息
});
};
按这里
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);
}
}
import java.io.IOException;
导入java.io.PrintWriter;
导入javax.servlet.*;
导入javax.servlet.http.*;
公共类GetUserServlet扩展了HttpServlet{
public void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
doPost(请求、响应);
}
public void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
String text=“
来自servlet的消息
”;//您将收到的消息 字符串名称=request.getParameter(“名称”); PrintWriter out=response.getWriter(); out.println(文本+名称); } }
您可以在此处使用$.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>

以前从未使用过它,但我愿意再看一眼,你已经是:)那些$是jQuery调用。所以您已经在使用jQuery.ajax()函数了。让我看一下我现有的ajax/servlet交互,记住它们是如何工作的:)
<!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>
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);
  }