This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes how to call a javabean method from jsp using setproperty 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 "how to call a javabean method from jsp using setproperty" Watch "how to call a javabean method from jsp using setproperty" New topic
Author

how to call a javabean method from jsp using setproperty

prasad chowdary
Greenhorn

Joined: Feb 18, 2012
Posts: 18
hi, i faced a problem in jsp how to call a method from javabeans in jsp using setProperty. i follow the javabean specification

javabean


package info.tech;

import java.sql.*;

public class EmployeeJavaBean {

String ename;
String desg;
String dummy;

public String getDesg() {
return desg;
}
public void setDesg(String desg) {
this.desg = desg;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}

public void setStoreData(String dummy) throws SQLException{

// business code
}


}


JSPjsp



<jsp:useBean id="empobj" class="info.tech.EmployeeJavaBean"></jsp:useBean>
<jsp:setProperty property="ename" name="empobj"/>
<jsp:setProperty property="desg" name="empobj"/>
<jsp:setProperty property="StoreData" name="empobj"/>


ERROR

i got an exception in jsp in the property StoreData .














Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018

setStoreData maps to the property 'storeData' rather than 'StoreData'.
Yes, case is important.

try:

prasad chowdary
Greenhorn

Joined: Feb 18, 2012
Posts: 18
hi stefen thanks for reply, it resole .

but, my form data is not storing please solve it


public void setStoreData(String dummy) throws SQLException{
Connection con = null;
PreparedStatement pstmt = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","prasad","abc");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();


try {
pstmt = con.prepareStatement("insert into emp_details values(?,?)");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

pstmt.setString(1,"ename");
pstmt.setString(2, "desg");
pstmt.executeUpdate();


}
}


}
prasad chowdary
Greenhorn

Joined: Feb 18, 2012
Posts: 18
sorry, i got it and i forget to close the catch block and the entire logic in that block . so thats why data is not stored.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
suggestions:
1 - use code tags
2 - why do it in multiple try/catch blocks? One should be sufficient
3 - clean up / close your statement/connection after use in a finally block
4 - If you're going to catch the exception, don't just swallow it. If all you do is print the stack trace, I would just let it fall through to the next level.
5 - For efficiency, investigate using connection pooling / JNDI Datasource

prasad chowdary
Greenhorn

Joined: Feb 18, 2012
Posts: 18
hi Stefen,
thanks for your valuable suggestion.
shravan nitta
Greenhorn

Joined: Sep 27, 2012
Posts: 1
Hello Prasad,
Instead of writing another method in Java Bean it won't work...Declare one String variable and write getter and setter methods for it.Now Append Your JDBC Code in Setter method.
Try the shown below code as it is your doubts will clarify...

package info.tech;
import java.sql.*;
public class EmployeeJavaBean {
String ename;
String desg;
String storeData;
public void setStoreData(String storeData) {
//JDBC code to store data
}
public String getStoreData(){
return storeData;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getDesg() {
return desg;
}
public void setDesg(String desg) {
this.desg = desg;
}
}
-------@@@@-----
JSP Page
---------
<jsp:useBean id="empobj" class="info.tech.EmployeeJavaBean"></jsp:useBean>
<jsp:setProperty name="empobj" property="ename"/>
<jsp:setProperty name="empobj" property="desg"/>
<jsp:setProperty name="empobj" property="storeData" value="xyz"/>
---------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to call a javabean method from jsp using setproperty