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 JSP and the fly likes Assign response text from AJAX to a JSP tag attribute 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 » Java » JSP
Bookmark "Assign response text from AJAX to a JSP tag attribute" Watch "Assign response text from AJAX to a JSP tag attribute" New topic
Author

Assign response text from AJAX to a JSP tag attribute

Prabhat Kumar N
Greenhorn

Joined: Aug 12, 2009
Posts: 20
I am using AJAX to get a string which I want to assign to a String attribute in custom JSP tag. For example,


Is there any direct or indirect way to achieve this ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

There is no way to achieve that. The JSP executes on the server, the Ajax request can;t be sent back to server until after the JSP finishes and the HTML page has been sent to the browser.

Please read this article to understand what JSP is and how it operates, and why what you are asking is impossible.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

P.S. If you need the result of a request while JSP is executing on the server, investigate the <c:import> tag.
Prabhat Kumar N
Greenhorn

Joined: Aug 12, 2009
Posts: 20
Bear Bibeault wrote:P.S. If you need the result of a request while JSP is executing on the server, investigate the <c:import> tag.


Yes. This will do. I went through <import> tag description. But how do I use the result of AJAX (xmlhttp.responseText) in the imported page Must I use something like this ?


How do I assign javascript value to c:param 's value ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

If you use <c:import> you won't be using Ajax.
Prabhat Kumar N
Greenhorn

Joined: Aug 12, 2009
Posts: 20
Bear Bibeault wrote:If you use <c:import> you won't be using Ajax.


Actually, I am making a AJAX request sending form values to a servlet which processes and sends me a JSON object as string. I am want to use that JSON object in my custom tag. Is it possible to use c:import instead ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

<c:import> will simply grab whatever the response is. If it's JSON, then that's what you'll get.

At this point, just try it out.
Prabhat Kumar N
Greenhorn

Joined: Aug 12, 2009
Posts: 20
Bear Bibeault wrote:<c:import> will simply grab whatever the response is. If it's JSON, then that's what you'll get.

At this point, just try it out.


Thanks for helping me out so far.
I understand how it works. But the point is I need to send elements from the form to the servlet.

The flow looks like this

First I contact servlet.


The above code will return me a JSON string. I need to use this JSON string and send it as parameter to my page like this



Please let me know how ?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assign response text from AJAX to a JSP tag attribute