-1

I'm trying to pass an Object type from Java class to Jsp page.

After going through many sites am bit confused on how to go about with this. My Java file has this content :

    Object categoryelements;
    Iterator it=elements.iterator();
    while (it.hasNext())
    {
        JSONObject innerObj= (JSONObject)it.next();
        categoryelements= innerObj.get("category");
        System.out.println(categoryelements);
    }

I want to pass this categoryelements to JSP page.

Is it possible to do so? Am making use of JSP page, Servlet and a Java page

Can you please provide a solution for this?

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
Krithi
  • 1
  • 2
  • What is the value in `categoryelements`? – VPK Jan 22 '15 at 07:01
  • please post your jsp , and please tell us who would call the code you provided in the question –  Jan 22 '15 at 07:02
  • Have a look at this post which seems to be relevant to your question. http://stackoverflow.com/questions/5414600/pass-data-from-java-servlet-to-jsp – Mooolo Jan 22 '15 at 07:05
  • @VPK categoryelements contains string elements. Am using JSONObject as a result of which I assigned it as an Object type. – Krithi Jan 22 '15 at 09:04
  • @Krithi, then you can directly get the values once you assign it in request attribute, have you tried that? – VPK Jan 22 '15 at 09:07
  • My jsp page contains a dropdown list that must be populated with the values from 'categoryelements'. @Amrola – Krithi Jan 22 '15 at 09:13
  • @VPK Thanks! I just tried doing that. I think I've got it. Can u tell me how to populate that into a dropdownlist of my jsp page – Krithi Jan 22 '15 at 09:16
  • @Amrola I've created an object in my Servlet code to access the Java page containing 'categoryelements'. Something like this in my Servlet, RetrieveCategory ret=new RetrieveCategory(); request.setAttribute("categoryelements",ret.categoryelements); And in my JSP page I've retrieved like this, <%= request.getAttribute("categoryelements") %> Am I doing it the right way? – Krithi Jan 22 '15 at 09:24
  • 1
    please update your question , and add the source code –  Jan 22 '15 at 09:39

3 Answers3

0

Your HTTPRequest object will be having setAttribute, getAttribute & removeAttribute methods, it will internally hold a map [Map<String,Object>] to keep the attributes, you can set the key & value pair and get it in the JSP using the implicit request object

Saravana
  • 12,647
  • 2
  • 39
  • 57
-1

If categoryelements contains plain string then set the request attribute like this,

request.setAttribute("categoryelements", categoryelements.toString());

And in JSPfetch the value in for example a div like this,

<div>${categoryelements}</div>

Edit:-

To access the request attribute in dropdownlist you can use the JSTL library which is helpful in handling the flow of execution in JSP or HTML. You can add this library in your JSP page like this,

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

And then use them to iterate the list as follows,

<select name="selectFieldName">
    // following line of code works as Java for loop and here 'listRequestAttribute' can be any list type object e.g. List<String>
    <c:forEach items="${listRequestAttribute}" var="currentListItem">
        // '${currentListItem}' will hold the current object in the loop iteration.
        <option value="${currentListItem}" >
            ${currentListItem}
        </option>
    </c:forEach>
</select>
VPK
  • 3,010
  • 1
  • 28
  • 35
  • I've created an object in my Servlet code to access the Java page containing 'categoryelements'. Something like this in my Servlet, RetrieveCategory ret=new RetrieveCategory(); request.setAttribute("categoryelements",ret.categoryelements); And in my JSP page I've retrieved like this, <%= request.getAttribute("categoryelements") %> Am I doing it the right way? – Krithi Jan 22 '15 at 09:22
  • In my JSP page, this is the code in ,
    ${categoryelements}
    But the values are not getting populated
    Category/Tagg:
    – Krithi Jan 22 '15 at 09:38
  • Update- In Servlet: RetrieveCategory ret=new RetrieveCategory();request.setAttribute("categoryelements",ret.categoryelements); In JSP :
    ${categoryelements}
    Category/Tagg:
    – Krithi Jan 22 '15 at 09:45
  • Sorry if I wasn't clear. Inside body of JSP page :
    ${categoryelements}
    // this is where am retrieving the contents of categoryelements as u said Nothing is printed in the browser. Values are'nt getting populated. 'categoryelements' contains string elements to be populated in the drop down
    – Krithi Jan 22 '15 at 10:15
  • Have you tried with `
    <%= request.getAttribute("categoryelements") %>
    `? Also in ` ` you should use the loop variable `${entry}` instead of `${categoryelements}`.
    – VPK Jan 22 '15 at 10:19
  • It prints null in the browser :( – Krithi Jan 22 '15 at 10:22
  • Did you checked if the value of `categoryelements` is printed correctly in the servlet class? And your servlet class have no compilation errors? If not the try to use session instead of request. – VPK Jan 22 '15 at 10:31
  • Value of categoryelements is printed as null which means data isn't retrieved in the Servlet. What can be done to rectify that? – Krithi Jan 22 '15 at 11:44
  • When are you setting the value in the `categoryelements`? Make sure you set the value properly before setting the request attribute. – VPK Jan 22 '15 at 12:42
-1

in Java:

Object categoryelements;
List<String> result = new ArrayList<String>();
Iterator it=elements.iterator();
while (it.hasNext())
{
    JSONObject innerObj= (JSONObject)it.next();
    result.add(innerObj.get("category"));
}
request.setAttribute("result", result);

in JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="result" class="java.lang.List" scope="request" />
...

<c:forEach var="item" items="${result}" >
    <c:out value="${item}"/>
</c:forEach>

This is just a simple example. You might consider using classes other than Object to provide better control over your output. The Gson library might help you here.

paul
  • 13,312
  • 23
  • 81
  • 144