Skip to main content

JSP and Servlet Form Submission without page refresh show results on the same page using Jquery AJAX
























Code Snippet

HTML Form 

<form id='ajaxform' name='ajaxform' action='ajaxformexample' method='post'>
 First Name: <input type='text' id='firstname' name='firstname' size='30' required/><br/>
 Last Name: <input type='text' id='lastname' name='lastname' size='30'required/><br/>
 Email:  <input type='email' id='emailid' name='emailid' size='30'required/><br/>
 Password:  <input type='password' id='pwd' name='pwd' size='30'required/><br/>
 <input type='Submit'/> 
 <div id='content'>
</div>

</form>
the above HTML Form uses post method and url servlet redirect to "ajaxformexample"



Javascript Code

 var form = $('#ajaxform'); // id of form tag
 form.submit(function () {

 $.ajax({
 type: form.attr('method'),  //post method
 url: form.attr('action'), //ajaxformexample url
 data: form.serialize(), // serialize input data values
 success: function (data) {
 var result=data;
 $('#content').html(result); //showing result

 }
 });

 return false; // not refreshing page

 });

the above javascript code uses Ajax jquery concept and parameters type, url,data, and finally success function to show result from specified action SERVLET performed results back to same page and in the below code snippet Servlet uses doPost method to get values in the input fields;

public void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws IOException {
  resp.setContentType("text/html");
  
  PrintWriter out=resp.getWriter();
  String fname= req.getParameter("firstname");
  String lname= req.getParameter("lastname");
  String email= req.getParameter("emailid");
  String password= req.getParameter("pwd");
  out.println("<br/><br/>First Name = "+fname+"<br/><br/>Last Name = "+lname+"<br/><br/>Email = "+email+"<br/><br/>Password = "+password);
 }

index.jsp Page








servlet Java





 Output


Comments

  1. actually i used this code and when i enter the submit button the page get refreshed...pls suggest the solution

    ReplyDelete
    Replies
    1. have u used jquery code which will return false as below code ?

      var form = $('#ajaxform'); // id of form tag
      form.submit(function () {

      $.ajax({
      type: form.attr('method'), //post method
      url: form.attr('action'), //ajaxformexample url
      data: form.serialize(), // serialize input data values
      success: function (data) {
      var result=data;
      $('#content').html(result); //showing result

      }
      });

      return false; // not refreshing page

      });

      Delete
  2. And If I want to show in the same form, How I can do that?

    ReplyDelete
  3. Hi, why it opening the page: http://localhost:8080/SisGestWeb/AjaxformexampleServlet instead of displaying the result in the same page?

    ReplyDelete
  4. thnx so much...nicly explaned for beginners very usefulll

    ReplyDelete
  5. It doesn't work. The code reloads the page and open another page. Someone can help me ?

    ReplyDelete
  6. i need a deep explanation which is most helpful for me to develop a technical skills quickly.

    Java Training in Chennai

    ReplyDelete
  7. Suppose we used bootstrap and we want show any error then how i show ...in simple words vaidation/.

    ReplyDelete
  8. can you provide me jquery code that make crud operation in SPA.

    ReplyDelete
  9. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    python training in OMR
    python training in Bangalore
    python training in Bangalore

    ReplyDelete
  10. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    java training in chennai | java training in bangalore


    java training in tambaram | java training in velachery

    ReplyDelete
  11. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
    online Python certification course | python training in OMR | Python training course in Chennai

    ReplyDelete
  12. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    Data Science Training in Indira nagar
    Data Science training in marathahalli
    Data Science Interview questions and answers



    ReplyDelete

  13. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.


    AWS Training in BTM Layout |Best AWS Training in BTM Layout

    AWS Training in Marathahalli | Best AWS Training in Marathahalli


    Selenium Interview Questions and Answers

    AWS Tutorial |Learn Amazon Web Services Tutorials |AWS Tutorial For Beginners

    ReplyDelete
  14. Very well written blog and I always love to read blogs like these because they offer very good information to readers with very less amount of words....thanks for sharing your info with us and keep sharing.
    angularjs Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    angularjs-Training in pune

    ReplyDelete
  15. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  16. I would assume that we use more than the eyes to gauge a person's feelings. Mouth. Body language. Even voice. You could at least have given us a face in this test.
    python training in chennai
    python course institute in chennai

    ReplyDelete
  17. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    angularjs-Training in pune

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in chennai

    automation anywhere online Training

    angularjs interview questions and answers

    ReplyDelete
  18. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

    rpa training in bangalore | best rpa training in bangalore
    rpa online training

    ReplyDelete
  19. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

    rpa training in bangalore | best rpa training in bangalore
    rpa online training

    ReplyDelete
  20. Very nice post here and thanks for it .I always like and such a super contents of these post.
    Excellent and very cool idea and great content of different kinds of the valuable information's.

    Java training in Bangalore



    ReplyDelete
  21. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
    Data science course in bangalore

    ReplyDelete
  22. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  23. Really very happy to say that your post is very interesting. I never stop myself to say something about it. You did a great job. Keep it up.
    We have an excellent IT courses training institute in Hyderabad. We are offering a number of courses that are very trendy in the IT industry. For further information, please once go through our site.
    AWS Training In Hyderabad

    ReplyDelete
  24. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.data science course in Hyderabad

    ReplyDelete
  25. Thank you for posting informative insights, I think we have got some more information to share with! Do check out
    oracle training in chennai and let us know your thoughts. Let’s have great learning!

    ReplyDelete
  26. how do I clear the form fields after submission?

    ReplyDelete
  27. I enjoy it for creating the details, keep up the truly amazing perform continuing
    data scientist training in hyderabad

    ReplyDelete
  28. Finish the Selenium Training in Chennai from Infycle Technologies, the best software training institute in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Java, Hadoop, Big Data, Android, and iOS Development, Oracle, etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

    ReplyDelete

Post a Comment

Share this to your friends

Popular posts from this blog

"How to maintain or retain tabs in same tab after button click events or postback?" using JQuery in ASP.NET C#

In this post I'll share an details about " How to maintain or retain tabs in same tab after button click events or postback? " Step 1: you need to download Jquery and JQueryUI Javascript libraries from this site http://jqueryui.com/ Step 2: As usually you can create ASP.NET website from Visual Studio IDE and add Jquery and JqueryUI plugins in the header section of aspx page. Step 3: Add HiddenField control inside aspx page which is very useful to retain tab in same page Step 4: Use the HiddenField ID in Jquery code to indicate that CurrentTab Index Step 5: In code Behind, using Enumerations concept give the tab index values as user defined variable  Step 6: Use the Enum values in every Button click events on different tabs to check that tab could be retained in the same tab Further, Here I'll give the code details and snap shot pictures, 1. Default.aspx: Design Page First Second Third ...

Guidewire Related Interview Question and answers part 1

common Guidewire questions and answers 20 Guidewire BC Q&A Top 100 Guidewire Interview FAQ Guidewire Claimcenter 20 Interview Questions Guidewire Rating concepts

Login and Registration forms in C# windows application with Back end Microsoft SQL Server for data access

In this article, I'm gonna share about how to make login and register form with MS SQL database; 1. Flow Chart Logic 2. Normal Features 3. Form Designs Login Form Design Sign in Form Design Password Retrieve Form 4. Database Design and SQL queries and Stored Procedure Create new Database as "schooldata" create table registerdata (  ID int identity,  Username nvarchar(100),  Password nvarchar(100),  Fullname  nvarchar(100),  MobileNO nvarchar(100),  EmailID nvarchar(100)  ) select * from registerdata create procedure regis (  @Username as nvarchar(100),  @Password as nvarchar(100),  @Fullname as nvarchar(100),  @MobileNO as nvarchar(100),  @EmailID as nvarchar(100)  ) as begin insert into registerdata (Username, Password, Fullname, MobileNO,EmailID) values (@Username, @Password, @Fullname, @MobileNO, @EmailID) ...