1

i am returning a json array from java class as return type and trying to display data on jsp page. everything is ok (as debugging says). But dont know the method (code/syntax) for jsp/html page. class

JSONArray jsonArray = new JSONArray();

    public JSONArray get_user() {

        String sql = "SELECT * FROM USER_INFO";
        try {
            con = new connection.connect().getConnection();
            smt = con.createStatement();
            rset = smt.executeQuery(sql);

            while (rset.next()) {
                JSONObject obj = new JSONObject();
                obj.put("Email", rset.getString(2));
                obj.put("Password", rset.getString(3));
                jsonArray.put(obj);
            }
        } catch (Exception ex) {
            ex.getMessage();
        }

        return jsonArray;
    }

jsp

<%
controller.fetch_user obj = new controller.fetch_user();
obj.get_user();
%>
<table>
<tr><td>Email</td><td>Password</td></tr>

 *** here i want to display all the records ****

</table>
6
  • 1
    Possible duplicate of How to display Json object in jsp Commented Feb 19, 2016 at 9:05
  • noo ... they have used JSTL , i dont Commented Feb 19, 2016 at 9:08
  • You did not say that. Why don't you use JSTL? Commented Feb 19, 2016 at 9:12
  • simple ,, i don't want to use. i just want to display in normal jsp. if u say that jsonarray can not be printed without JSTL, then its another matter. Commented Feb 19, 2016 at 9:15
  • help me on this ,,, how to access the "obj" that has "jsonArray" returned fron Controller. Commented Feb 19, 2016 at 9:17

1 Answer 1

2

best solution for it instate of returning a json you can return pojo

Create your like

class UserInfo{
 private String email;
 private String Password;
 //setter and getter
}

and then reurn a list of userInfo

so your method will be

public List<UserInfo> get_user() {
    List<UserInfo> userInfoList = new AraayList<UserInfo>()
    String sql = "SELECT * FROM USER_INFO";
    try {
        con = new connection.connect().getConnection();
        smt = con.createStatement();
        rset = smt.executeQuery(sql);

        while (rset.next()) {
            UserInfo obj = new UserInfo();
            obj.setEmail(rset.getString(2))
            obj.setPassword(rset.getString(3))

            userInfoList.add(obj);
        }
    } catch (Exception ex) {
        ex.getMessage();
    }

    return userInfoList;
}
// for json object

JSONArray jsonArray = new JSONArray();

public JSONArray get_user() {

    String sql = "SELECT * FROM USER_INFO";
    try {
        con = new connection.connect().getConnection();
        smt = con.createStatement();
        rset = smt.executeQuery(sql);

        while (rset.next()) {
            JSONObject obj = new JSONObject();
            obj.put("Email", rset.getString(2));
            obj.put("Password", rset.getString(3));
            jsonArray.put(obj);
        }
    } catch (Exception ex) {
        ex.getMessage();
    }

    return jsonArray;
}

your jsp code is

<%
 controller.fetch_user obj = new controller.fetch_user();
 obj.get_user();
%>
<table>
<tr><td>Email</td><td>Password</td></tr>
for(UserInfo userInfo:obj){
<%
%>
<tr>
  <td><%=userInfo.getEmail()%><td>
  <td><%=userInfo.getPassword()%><td>
</tr> 
<%
 }
%>

</table>

and if your method still return jsonAraay the code will be

 <%
 controller.fetch_user obj = new controller.fetch_user();
JSONArray getArray = obj.get_user();
%>
<table>
<tr><td>Email</td><td>Password</td></tr>



<%
  for(int i = 0; i < getArray.size(); i++){
  JSONObject userInfo = getArray.getJSONObject(i);
%>
<tr>
  <td><%=userInfo.get("Email")%><td>
  <td><%=userInfo.get("Password")%><td>
</tr> 
<%
 }
%>

</table>

and in jsp code you can access this like in java you are access it

may be this will help you a lot

Sign up to request clarification or add additional context in comments.

4 Comments

but friend, i want to use json,, thats why i have used it.
okay now i have updated your jsp code as per the json araay method please have a look on it.
JSONObject userInfo = getArray.getJSONArray(i); For this i get error ,, json array cannot be converted into json object
and please refer developer.android.com/reference/org/json/JSONArray.html for all methods of jsonArray().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.