JSP + json + data structure

HI,
I am new to jsp, I need use Json in JSP to display data in UI. Can anyone provide some example?

Thanks
chenjhuaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chenjhuaAuthor Commented:
My current code is like this:
function testDraw() {
      var request = $.ajax({
            url: "js/new_chart.json",
            type: "GET",
            data: {format: 'json'},
      });


new_chart.json is a json file with hardcoded  data, I need pull the data from database, instead of hardcode.

Thanks
0
gurpsbassiCommented:
Try something like:

var request = $.ajax({
            url: "js/new_chart.json",
            type: "GET",
            data: {format: 'json'},
            error: function() {
             console.log("something went wrong");
           }
,
            success: function(data) {
               var x = data.prop1;
               alert(x);
           }

      });

You're just missing the success and error handlers. Once you've got the data in the success handler, you can traverse it and use it in your jsp.
0
chenjhuaAuthor Commented:
gurpsbassi:
hanks for the post.
The code that I provide before is the existing code, it is hard code with Json data. I need to pull the data from database. Do you have any example about it?

Thanks
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gurpsbassiCommented:
Change the url to one that is mapped to a controller / servlet that does what you want.
If you need to code the controller/servlet yourself then simply pull the data from the database using the persistence layer you are using e.g. jdbc/hibernate etc and then convert the result set to Json using a library such as gson.
0
chenjhuaAuthor Commented:
I don't have a controller/servlet, it is pure jsp and call jdbc. Can you provide some example code?

Thanks
0
gurpsbassiCommented:
I would not recommend you initiating a query to the database from your jsp.
You need to create a HTTP end point using whatever framework you are using. Are you using struts or something similar?

For me to provide example, I need to know the technology stack you are using.
0
chenjhuaAuthor Commented:
Actually there is no framework, it is pure JSP JDBC call the database.

Thanks
0
gurpsbassiCommented:
Ok, so if you can't create a servlet then you could just embed the JDBC code in as a scriptlet.
If it was my choice I would create a server side component such as a servlet at least to do this.

Scriplet code will be something like this: (not tested)


<%
   String connectionURL = "connectionstring'";
   Connection connection = null;
   Statement statement = null;
   ResultSet rs = null;
   
 
%>
 
 <% Class.forName("com.mysql.jdbc.Driver").newInstance();
 
       connection = DriverManager.getConnection(connectionURL, "","");
       statement = connection.createStatement();
       rs = statement.executeQuery("SELECT * FROM mytable");

      // traverse the result set and put it into a Json object.
  %>

Again, I must reiterate.....this is a very poor way of doing this. Why don't you simply create a servlet instead?
0
chenjhuaAuthor Commented:
Thank you very much for the examples.
Can you also provide a servlet example?
The reason that this is poor is because it doesn't separate the business and UI layers, right?


Thanks
0
gurpsbassiCommented:
The reason that this is poor is because it doesn't separate the business and UI layers, right?

Yes for the most part.


I would look up some servlet example on the web. All you need is a get method on the servlet that gives you back some JSON. You will need to learn how to configure your servlet in web.xml too.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.