Why control is not going to the bean?

[nobr]Hi all,

I am having some problem while accessing a bean. Here is the program i am trying to do. I have two .jsp page and one bean class.

First jsp page is Login.jsp

<html>

<head>

<title>Login Page</title>

</head>

<body>

<center>

<form method=post action=SQLTool.jsp>

<table>

<tr>

<td>User Name : </td>

<td><input type=text name=userName></td>

</tr>

<tr>

<td>Password : </td>

<td><input type=password name=password></td>

</tr>

<tr>

<td><input type=reset></td>

<td><input type=submit value="Login"></td>

</tr>

</table>

</form>

</center>

</body>

</html>

And the second jsp page is SQLTool.jsp

<jsp:useBean id="theBean" class="myBeans.SQLToolBean">

<%

try{

Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");

}catch (Exception e){

out.println(e.toString());

}

%>

</jsp:useBean>

<jsp:setProperty name="theBean" property="userName"/>

<jsp:setProperty name="theBean" property="password"/>

<jsp:setProperty name="theBean" property="conURL" value="jdbc:db2://192.168.7.170:6799/sample"/>

<jsp:setProperty name="theBean" property="sql"/>

<html>

<head>

<title>SQL Tool</title>

</head>

<body>

<br><h2>SQL Tool</h2><br>

Please type your SQL statement in the following box.

<br><br>

<form method=post>

<input type=hidden name=userName value="<jsp:getProperty name="theBean" property="userName"/>">

<input type=hidden name=password value="<jsp:getProperty name="theBean" property="password"/>">

<textarea name=sqlCmdArea cols=80 rows=8>

<jsp:getProperty name="theBean" property="sql"/>

</textarea>

<br>

<input type=submit>

</form>

<br><hr><br>

<%= theBean.getResult() %>

</body>

</html>

And here is my bean class SQLToolBean.java

package myBeans;

import java.sql.*;

publicclass SQLToolBean{

private String sql ="";

private String userName ="";

private String password ="";

private String conURL ="";

publicvoid setSql(String sql){

if(sql !=null)

this.sql = sql;

}

public String getSql(){

return sql;

}

publicvoid setUserName(String name){

if(name !=null)

this.userName = name;

}

public String getUserName(){

return userName;

}

publicvoid setPassword(String password){

if(password !=null)

this.password = password;

}

public String getPassword(){

return password;

}

publicvoid setConURL(String url){

if(url !=null)

this.conURL = url;

}

public String getResult(){

System.out.println("++++++++++++++++++++++++++++Inside the getResult() of the bean");

if(sql ==null || sql.equals(""))

return"";

//System.out.println("++++++++++++++++++++++++++++Inside the getResult() of the bean");

StringBuffer result =new StringBuffer(1024);

try{

Connection con = DriverManager.getConnection(conURL, userName, password);

Statement stat = con.createStatement();

if(sql.toUpperCase().startsWith("SELECT")){

result.append("<TABLE BORDER=1>");

ResultSet rs = stat.executeQuery(sql);

ResultSetMetaData metaData = rs.getMetaData();

//Write table headings

int columnCount = metaData.getColumnCount();

result.append("<TR>");

for(int i = 0; i <= columnCount; i++){

result.append("<TD><B>" + metaData.getColumnName(i) +"</B></TD>\n");

}

result.append("</TR>");

while(rs.next()){

result.append("<TR>");

for(int i = 0; i <= columnCount; i++){

result.append("<TD>" + rs.getString(i) +"</TD>");

}

result.append("</TR>");

}

rs.close();

result.append("</TABLE>");

}else{

int i = stat.executeUpdate(sql);

result.append("Records(s) affected: " + i);

}

stat.close();

con.close();

//result.append("</TABLE>");

}catch(SQLException e){

result.append("<B>Error</B>");

result.append("<BR>");

result.append(e.toString());

}catch(Exception e){

result.append("<B>Error</B>");

result.append("<BR>");

result.append(e.toString());

}

System.out.println("++++++++++++++++++++++++++++At the end of the getResult() of the bean");

String printMe = result.toString();

if(printMe =="")

System.out.println(printMe);

else

System.out.println("No data available");

return printMe;

}

}

The problem is <%= theBean.getResult() %>

. Why i couldn't call the getResult method? What i am missing here?

Thanks for your attention and help.[/nobr]

[9459 byte] By [max25a] at [2007-10-1 22:05:43]
# 1
Anybody any help, any suggestion plesase..
max25a at 2007-7-13 8:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

The jsp:setProperty, which when defined the way you have,

<jsp:setProperty name="theBean" property="sql"/>

depends on a request parameter called "sql" being present in the request.

However your textarea field which delivers the sql query as a request params to your jsp is called 'sqlCmdArea'.

<textarea name=sqlCmdArea cols=80 rows=8>

So either change the name of the textarea field to 'sql' or change your setProperty as shown below.

<jsp:setProperty name="theBean" property="sql" value ="<%=request.getParameter("sqlCmdArea")"/>

cheers,

ram.

PS: nice tool, nice idea :), congrats.

Madathil_Prasada at 2007-7-13 8:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Also you can just rename your textarea to sqland trim the sql in your bean. Coz it will surely fail if there are leading spaces.if(sql.trim()toUpperCase().startsWith("SELECT")) {
masoodmjana at 2007-7-13 8:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

>

> PS: nice tool, nice idea :), congrats.

Hi friends,

Thanks for your reply. Madathil_Prasad, you've been always so helpfull. I just want to tell you the idea of making the tool is not mine, i've got that example in a book. Anyway now my program is working. Thanks again.

max25a at 2007-7-13 8:18:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...