This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Java Method from JSP

 
Mag Yeoh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mag Yeoh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic