0

I need help with ajax request, could you show an example elementarny ajax request with a text field and a servlet java

  • In google all revised but could not understand –  Sep 16 '14 at 08:48
  • Did u check this ? : http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax – Rockstar Sep 16 '14 at 09:02
  • I saw it as a very good example but it does not show how to send data from a Web page to a servlet –  Sep 17 '14 at 10:58

1 Answers1

0

Its simple dear,

1.first create one textfield like following:

<input type="text"  id="newCatName" name="newCatName"/>

2.Create one button

<button  onclick="ajaxCall();">Ajax Call</button>

3.Use following function for ajax call:

function ajaxCall(){
      var catName = $("#newCatName").val(); 
      $.ajax({
          type: "POST",
          url: "MyServlet",
          data: {"catName":catName},
          success: function()
          {  
              alert('success);
          },
          error:function()
          {
              alert("error");
          },
        });
      }

4.Create one servlet with Name MyServlet as following:

public class MyServlet extends HttpServlet {      
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String catName = request.getParameter("catName");
    }    

  }
Mahesh
  • 1,063
  • 1
  • 12
  • 29
  • 1
    Answering these sorts of questions encourages OP to ask them again in the future. These types of questions aren't a very good fit for this Q&A because we are not a tutorial site. We give specific answers to specific programming problems. While you've given a good answer, it's not to a very valuable question. And a quick note on your Javascript, pulling down the entire `jQuery` library for a single AJAX request is certainly not recommended. – christopher Sep 16 '14 at 09:20
  • tried everything but this example does not work for some reason –  Sep 17 '14 at 10:23
  • can you tell me whats happened when you run? which error you getting? – Mahesh Sep 17 '14 at 11:03