0
<form id="form1" name="form1" method="post">
    User Name:<input type="text" name="user" id="user"/>
    Password <input type="password" name="pass" id="pass"/>
    Phone <input type="number" name="phone" id="phone"/>
    Email <input type="email" name="email" id="email"/>
    State <input type="text" name="state" id="text"/>
    <input type="button"  onclick="return register();" value="submit"/><br/>
    <div id="welcometext"></div>
</form>
function register() { 
    var val = $('#form1 :input').serialize*();
    $.post('ActionServlet', { data: val }, function (responseText) {
        $('#welcometext').text(responseText);
    });       
}

Now i want to get this data in Servlet and add to mysql database and fetch data and response that data to the form in div tag. Please help me.

2 Answers2

0

Not sure if you are using spring controller, but hope this snippet will be helpful for you

var formFields = {
            "user" : $("#user").val(),
            "password" :$("#pass").val(),
             //.... other fields
 }

function register() { 
    $.post('ActionServlet', { data: formFields}, function (responseText) {
        $('#welcometext').text(responseText);
    });       
}

Servlet

import javax.servlet.*;
import javax.servlet.http.*;

public class ActionServlet extends HttpServlet {
  public String getData(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
      String name = request.getParameter("parameter you want to retrive");
    return "response you want to send";
  }

NOTE: You form has a field password, if you are using ajax to send this data , password which is a secure identity will be visible in request . So it is better to use form submit rather than using ajax to submit the form

brk
  • 48,835
  • 10
  • 56
  • 78
0

you can use jquery ajaxForm http://malsup.com/jquery/form/

$('#form1').ajaxForm(function(response){
  //response from server
});
madhairsilence
  • 3,787
  • 2
  • 35
  • 76