File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes How do send value in JSP as response to ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How do send value in JSP as response to ajax" Watch "How do send value in JSP as response to ajax" New topic
Author

How do send value in JSP as response to ajax

Vernon de Klerk
Greenhorn

Joined: Sep 01, 2012
Posts: 2
Hi guys need big help here please. Please take note, I am new to java
Ok i have 3 jsp one is called homePage.jsp


homePage.jsp contains


This is also in the homePage.jsp


Second jsp is clientExtra.jsp
Here I get data on the HTML page and send them to the addExtraItem() in the homePage.jsp


The third jsp is addExtras.jsp



Now all I want to know is, how do I display or send the String result from addExtras.jsp back to the homePage.jsp? or the clientExtra.jsp? The string will contain id it was successful or what field was incorrectly entered.
I want to show the client what was wrong.
Thank you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

Firstly, you should not be putting Java code in a JSP. That's a bad practice that have been obsolete for 10 years! Please read this JspFaq entry.

Secondly, when using Ajax, you communicate data back to the page that invoked the Ajax request via the response. JSP variables and the like are useless in this scenario.

If you want to return data, the best approach is to return it as JSON.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vernon de Klerk
Greenhorn

Joined: Sep 01, 2012
Posts: 2
Bear Bibeault wrote:Firstly, you should not be putting Java code in a JSP. That's a bad practice that have been obsolete for 10 years! Please read this JspFaq entry.

Secondly, when using Ajax, you communicate data back to the page that invoked the Ajax request via the response. JSP variables and the like are useless in this scenario.

If you want to return data, the best approach is to return it as JSON.


Tanks for the JSON tip Ill clean my code as soon as I understand JSON ( YES I am a noob I have less than 4 month of Java experience)
May I ask if you can show me a simple example to use the response?


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

In a servlet (you never use a JSP for anything other than to produce HTML) you could use any JSON library (such as Google's Gson library) to produce the JSON equivalent of your data and simply write that as the response.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

P.S. As a newcomer to JSP, I highly suggest reading:
  • The Secret Life of JSPs
  • The Front Man
  • .
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: How do send value in JSP as response to ajax
     
    Similar Threads
    How to set timer for select the time am to pm on front end in a jsp page like calender for date?
    resultset is not working properly
    How to update a image using servlet?
    Nested Tag (in Form of Table ) :: not able to submit masterForm
    Having problem using Ajax