1

I am new to Java EE programming. Following my understanding on jsp. Corret me if I am wrong
- JSP pages are converted to servlet first then to html and resulted html page is displayed in browser.

Now suppose jsp page is displayed in browser i.e now it is html page and I have a java List which have names or some sort of data that I want to print on the currently loaded page. I can get the List object using ajax but the how will I display it on html as html cant render java collections.

Correct me wherever I misunderstood the flow or basic concepts.

Thanks.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Sandeep Kumar
  • 13,799
  • 21
  • 74
  • 110
  • I'm not sure whether this is applicable: Serialize the data as JSON and use AJAX to load the data; then use Javascript to display the data in the JSON received. – nhahtdh Jun 20 '12 at 07:15
  • or, you can use iframe (displaying another JSP page which deal your java List data) embedded in current JSP page. AJAX is another option. – LiuYan 刘研 Jun 20 '12 at 07:20

3 Answers3

3

You could use ajax (using jQuery would be easy) to make a call to your Servlet

function callMe(){
    $.ajax({
      type: "POST",
      url: "/someServlet",
      data: { param1: "val1" , param2: "val2" }
    }).done(function( data) {
       //TODO
    });
}

Now on Servlet, in doPost(), Use Gson to generate JSON representation for your collection

String parameter1 = request.getParameter(param1);
String parameter2 = request.getParameter(param2);
//call to service to generate the collection
//for example List<Employee>
List<Employee> employees  = someService(parameter1, parameter2);
//using google's gson
Gson gson = new Gson();
String json = new Gson().toJson(employees);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

Now we have response in javascript function as a array of javascript objects, So modify it to

    }).done(function( data) {
        //some processing for display
        var len = data.length
        for (var i=0; i<len; ++i) {
               var employeeFirstName =  data[i].firstName;
               var employeeLastName =  data[i].lastName;
               //set it to some DIV, or do the processing you want
        }
      }

    });

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • +1 "From jsp you could use ajax". Note that this means "from the HTML as output by your JSP that has been sent and lives in the browser now". At that point, it is pretty irrelevant if that page was done with JSP. In particular, the servlet that sends out the Ajax reply has nothing to do with the JSP, either. – Thilo Jun 20 '12 at 07:21
  • @JigarJoshi: Thanks buddy for this easy solution. But what if the list has a Map like List myList = new List>(). – Sandeep Kumar Jun 20 '12 at 07:26
  • Same would work, Try generating JSON representation for your data structure that is compatible with javascript object – jmj Jun 20 '12 at 07:28
0

You need to send the contents of your list as text to the user's browser (which is what normally happens).

A convenient format for transferring the contents of the list between the browser and the server is JSON, due to its simple readability with JavaScript and easy generation on the server.

You can then display the returned text in whatever way you like using JavaScript.

Kallja
  • 5,362
  • 3
  • 23
  • 33
0

A JSP is compiled into a java servlet class, which can handle HTTP requests. When the servlet is deployed to an application server HTTP requests are passed to the servlet for handling: an HTTP response is generated which normally contains some HTML, a status code, etc.

So it's the java code in the servlet which loops over your list and presumably generates the appropriate HTML to render that list in a browser.

Whether or not it's an AJAX request or not doesn't really matter. Rather than rendering a full HTML page, the AJAX request would probably be handled by a different servlet, which generates only a partial page - perhaps just the <ul><li>...</li></ul> to render your list. The javascript in your HTML page can then take care of updating the user interface by replacing the old version of the list.

Martin Dow
  • 5,273
  • 4
  • 30
  • 43