wood burning stoves 2.0*
The moose likes JSP and the fly likes Calling Java Method from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Calling Java Method from JSP" Watch "Calling Java Method from JSP" New topic
Author

Calling Java Method from JSP

Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 40
can i use javabean to call java class method from jsp? or actually it is not possible? because i am having some problem. i can use javabean to pass in value and after that i want to call out the method inside java class to store the values that is being entered by the user. for the info, i am using db4o as my database. so i need to create an object in order for me to store this data in.


Hope you guys could help me on this. Thanks alot
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
Hi Mag,

You can always invokes any reachable Java methods from within JSP scriptlet. If you are using jsp:useBean standard action to populate your Java Bean, it will store your Java Bean in the specific scope with the specified name you give. Also it will create a scripting variable using the id you given to the action.

For example,

<jsp:useBean id="userBean" class="domain.User">
<jsp:setProperty name="userBean" property="userName" value="${param.userName}" />
</jsp:useBean>
<%

// Using the scripting variable
out.println(userBean.getUserName());

// Default is page scope for your Java Bean
// So, you can retrieve your Java Bean using the following too

domain.User myUser = (domain.User)pageContext.getAttribute("userBean");
%>


Hope I understand your question correctly. Cheers.
[ November 02, 2006: Message edited by: Eddy Lee Sin Ti ]

SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 40
ok. thanks alot for it. i will look into it and try it out. if i have any enquiries, i will post it up again. Thanks alot.


sorry. got something to double check with you.
my first page called, add.jsp, second page is add2.jsp.
add.jsp will get the user input so practically there is no use of usebean inside the page? am i right? or maybe i am wrong. only after i pass in to the second page that is add2.jsp then only i use useBean to set and get the property. am i correct or maybe wrong? at the same time after i display, i should have store into the database by calling the store method inside java.

thanks alot.. correct if i am wrong
[ November 02, 2006: Message edited by: Mag Yeoh ]
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
Hi Mag,

For add.jsp, your code might have useBean action too if you would like to retrieve back the data user entered, maybe for repopulating the form due to validation errors. It will depend on how you want to structure your code.

Cheers.
Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 40
Emm.. I will think about this issue. for javabean method, is there anyway that i could test whether that the bean already store the scripting variable. at least i could sort out some of the possible mistakes that is happening. Thanks

Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 40
I was thinking, is it sometimes connected to the permissible value that i use? like page, request, session or application?

now i am looking on it through the net on each value and their meaning.
actually this problem i think to you all is very easy. i already trying to solve this problem for nearly 3weeks. really look very bad. =)
Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 40
can you help me to check whether my coding is correct or not?

because until now i still cannot solve this problem..i already hang up this problem for 3 week already...

Thanks
 
jQuery in Action, 2nd edition
 
subject: Calling Java Method from JSP