This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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: 61450
    
  67

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: 61450
    
  67

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: 61450
    
  67

P.S. As a newcomer to JSP, I highly suggest reading:
  • The Secret Life of JSPs
  • The Front Man
  • .
     
    Consider Paul's rocket mass heater.
     
    subject: How do send value in JSP as response to ajax