0

I am new with servlet, I want to get the data in the servlet using ajax and jquery. it execute but i did not get data in servlet on the click of the submit button the only error part is execute of ajax my index page is

 I am new with servlet, I want to get the data in the servlet using ajax and jquery. it execute but i did not get data in servlet

on the click of the submit button the only error part is execute of ajax my index page is

<html>
    <head>
        <title>First jQuery Example</title>
        <script type="text/javascript"
            src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
        </script>
    </head>
    <body>
        <div align=center>
            <form id="myform">
                <pre>
                    Name:
                    <input type="text" name='name' id='name' />
                    Email:
                    <input type='text' name='email' id='email' />
                    Address:
                    <input type='text' name='address' id='address'>
                        <input type='submit' value='submit' id='mysubmit'>
                </pre>
            </form>
        </div>
        <script type="text/javascript">
            $(document).ready(function() {
                alert("Onready the page");
                $("#myform").on('submit',(function(e){
                    alert("button clicked");
                    e.preventDefault();
                    var name=$("#name").val();
                    var address=$("#address").val();
                    var email=$("#email").val();
                    $.ajax({
                        url: "reg",
                        type: "GET",
                        data:{name:name,email:email,address:address},
                        contentType: false,
                        cache: false,
                        processData:false,
                        success: function(data){
                            alert(data);
                        },
                        error: function(){
                            alert("error");
                        }
                    });
                }));
            });
        </script>
    </body>
</html>


Ankit Khettry
  • 997
  • 1
  • 13
  • 33
Navin kumar
  • 21
  • 1
  • 4

2 Answers2

3

1 ) import latest jquery file
2 )

<script>$(".your form class").on('submit',(function(e){
        e.preventDefault();
        $.ajax({
            url: "Servlet Path",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            success: function(data){
                alert(data);
            },
            error: function(){
                alert("error");
            }           
        })
    }));</script/>
Pratik Bhalodiya
  • 736
  • 7
  • 14
0

You don't need ajax to send data to your servlet. You need to define a route to your servlet in web.xml, and mention a submit action for your form. For example,

 <servlet>
        <servlet-name>HelloForm</servlet-name>
        <servlet-class>HelloForm</servlet-class>
 </servlet>

 <servlet-mapping>
        <servlet-name>HelloForm</servlet-name>
        <url-pattern>/HelloForm</url-pattern>
 </servlet-mapping>

This is the entry for your application's web.xml file and

<form action="HelloForm" method="GET">

This needs to be your form tag for an example servlet with class name 'HelloForm'. Clicking the submit button of this form will send your form field data to your servlet. Take care of the 'name' of the form fields. To access a field named 'field 1' (Example Ninput name="field1">), the code in your servlet needs to be request.getParameter("field1").

Hope this clarifies your doubts.

Ankit Khettry
  • 997
  • 1
  • 13
  • 33