简体   繁体   中英

How to pass a java object to jsp page

I have a serveresource method which is invoked on clicking a link. The serveresource method takes the input from the parameter that was passed and retrieves a row from the database. Now all the values in a row are in set using the mutator methods. I have everything in a java object. I need to pass this object to a jsp page to print the values of a single row on the jsp page. I am not sure how to handle that java object in the jsp page instead of setting each value as an attribute in the serveresource method. Need help from experts.. Thanks in Advance

UPDATE

It was because I have an Ajax call and when I set values it is in a completely different life cycle which is causing the problem. I figured it out.

You should defining the java object as Bean in JSP . The Bean in JSP can be defined using < jsp:useBean..> standard jsp tag. And set and get property using < jsp:setProperty..> and < jsp:getProperty..> standard jsp tags.

Refernces:

The usual method is to add it to the HttpServletRequest object, thus:

MyBean myBean = new MyBean();
myBean.setValue("something);
myBean.setAnotherValue("something else");

// ... stuff ... 

request.setAttribute("myBean", MyBean);

This can be accessed from the jsp page using EL thus:

<table>
  <tr>
    <td>${myBean.value}</td>
    <td>${myBean.anotherValue}</td>
  </tr>
</table>

you can bind with request object

In Servlet or JSP
request.setAttribute("strIdentifire", yourJavaObject);


In JSP
YourJavaObjectClass obj = (YourJavaObjectClass)request.getAttribute("strIdentifire");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM