*
The moose likes HTML, CSS and JavaScript and the fly likes JSP Ajax call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JSP Ajax call" Watch "JSP Ajax call" New topic
Author

JSP Ajax call

Maheshwari Kirthi
Greenhorn

Joined: Jan 24, 2011
Posts: 18
Working on a J2EE application with the framework
JSP -> ControllerServlet -> EJB Driver -> Bussiness Objects -> and the round trip back to JSP.

1. Calling an AJAX call on a button click,

2. JSP has a div id name "Results" which will be displayed as a POPUP after the ajax call.
3. The EJB process does some DB query and has the results in a Java Object with get/set methods put in the request attribute.
4. I want to populate the resuts div with the data from the above mention java object which is available in the request scope.


Thanks in Advance,
Kirthi.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

I'm not sure what your question is, because you didn't ask one, but here are some observations:
  • Why aren't you using the .load() method rather than $.ajax()? Always use the simplest tool that does what you need.
  • The selector '.result' will not match an element with the id of result. What will it match?
  • Why the absolute URL for the request? Bad idea. Use a server-relative address that starts with the context path.


  • And please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

    I've gone ahead and added the code tags for you. See how much easier the code is to read?


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Maheshwari Kirthi
    Greenhorn

    Joined: Jan 24, 2011
    Posts: 18
    1. I did not use load, because after ajax call, I want only the particular div to have the updated data, and this div is displayed as a modal popup.
    2. In my actual code I have used the contextPath, just for the forum I pasted the absolute URL.
    3. My question is I want to populate the results div (which is just an empty div as part of the JSP) with the data returned by the server which is available in the request scope.



    Thank you.
    Kirthi.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60765
        
      65

    Maheshwari Kirthi wrote:1. I did not use load, because after ajax call, I want only the particular div to have the updated data, and this div is displayed as a modal popup.

    None of that precludes the use of .load(). Though I don't know what you mean by "modal popup" -- the term is ambiguous.

    2. In my actual code I have used the contextPath, just for the forum I pasted the absolute URL.

    It's not a good idea to post something different from what you are actually using. It wastes time, and if you waste people's time, they'll stop trying to help you.

    3. My question is I want to populate the results div (which is just an empty div as part of the JSP) with the data returned by the server which is available in the request scope.

    Can't be done. Request scope is a server-side concept. You need to return whatever HTML fragment you want to put into the page as the response body.

    Maheshwari Kirthi
    Greenhorn

    Joined: Jan 24, 2011
    Posts: 18
    Thanks for the reply.
    I'm planning to use JSONObject from the Servlet,.
    1. Created a LoadDataServlet

    where personDTO is a data transfer java bean object with get/set methods.

    2. How do I parse the JSON object in the javascript , should I download json.js and do json.parse, or is there a better way of doing it?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60765
        
      65

    Ah, if you are returning JSON data rather than an HTML fragment, then you likely want to use $.getJSON(). The already-parsed JSON data will be passed to the success handler.

    So what you said earlier made no sense -- you said you wanted to load something into a <div>. You don't load data into a <div>, you load HTML. So which is it?
    Maheshwari Kirthi
    Greenhorn

    Joined: Jan 24, 2011
    Posts: 18
    No I still need to populate a div element, I will create the htmlFragment as a string by parsing the JSON data and will load the div using the innerText property.

    Thanks,
    Kirthi.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60765
        
      65

    Maheshwari Kirthi wrote:No I still need to populate a div element, I will create the htmlFragment as a string by parsing the JSON data

    Would it not be easier to create the HTML fragment on the server using something like JSP?

    And building up HTML in JavaScript to load into an element is a really really bad way to do it. Use the jQuery DOM manipulation API to create new elements, not HTML strings.

    and will load the div using the innerText property.

    And even if you were going to do it this way, innerText is wrong. You can't inject an HTML fragment with innerText. In any case, use jQuery.
    Maheshwari Kirthi
    Greenhorn

    Joined: Jan 24, 2011
    Posts: 18
    Thanks for the help, yes it worked , I used $.getJSON() and using JQUERY DOM manipulation to create html elements.
    Servlet code segment:


    Javascript :



    One more question: Any JSON servlet exmaple where we write array of JSON objects to the servlet response and read the array in the Javascript?

    Thanks,
    Kirthi.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: JSP Ajax call
     
    Similar Threads
    Setting radio button value dynamically and rendering partially after ajax request
    jQuery UI tabs not loading in the same tab
    Replacing the contents of a div using an AJAX request made from the server-side?
    client could not get update session data
    Need Help With Dynamic text box in JSP