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?
 
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
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.