Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do send value in JSP as response to ajax

 
Vernon de Klerk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vernon de Klerk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. As a newcomer to JSP, I highly suggest reading:
  • The Secret Life of JSPs
  • The Front Man
  • .
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic