Help convert some code

Here is a fragment of javabean code:


more code above....
public ResultSet execSQL() throws SQLException{ 
String sql;
sql = "SELECT ownerid, oname, ostreet FROM powners ORDER BY oname";

try
{
PreparedStatement os  = obCon.prepareStatement(sql);
ResultSet ownerr = os.executeQuery();

return (ownerr == null) ? null : ownerr;   
// PreparedStatement s = null;


}
  catch (SQLException se)
  {
    // log exception;
    throw se;
  }

}
more code below 
.
.

And a fragment of code currently working in a jsp file:


<%
.
.
ResultSet rs = null;
ob.connect();
String sql;
rs = ob.execSQL();

while (rs.next())
{
kount = kount + 1;
rowid = "a" + Integer.toString(kount);
out.println("<tr id=" + rowid + " onMouseover=this.bgColor='lightgrey' onMouseout=this.bgColor='#FFFFFF' onclick='cell(this.id)'>");
out.println("<td>" + rs.getString(1) + "</td>");
out.println("<td>" + rs.getString(2) + "</td>");
.
.
%>


The rs = ob.execSQL(); line communicates with bean, then I can loop through my resultset. All this works.

I am trying to convert and call the bean from the jsp expression language like:


.
.
.
<jsp:useBean id="cb" scope="application" class="Ownerbean.ownerbean" />
.
.
${cb.execSQL}


And then loop I guess something like

<c:forEach items="${rs}" var="item">
        <td>${item.ownerid}</td>
    </c:forEach>

I don’t know how to make the rs variable in expression language. And, I get an error that points to the line where I have ${cb.execSQL}.

How would I “call” the execSQL using the expression language?

You’re right, you should not be using the scriptlet tag.

I didn’t say build your entire view with servlets.

Here is a more concrete example:


public class ControllerServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response) {
    // Get a hold of your query executor here...
    // Fill you javabean here from your executed query...
    request.setAttribute("javabean", <your javabean object you filled from the query here>);
    
    // forward your to your jsp.
}

Now you would access your “JavaBean” through the “javabean” attribute with EL.

Notice I added the “JavaBean” into the request under the attribute name of “javabean” so therefore you would access your bean by using javabean in your EL expressions.


<h1>How are you today ${javabean.firstname}?</h1>

Executing code like you’re trying to do in a view (E.g.: JSP) is typically a bad idea. All code execution goes into a servlet that will pass a dumb object to your view.

The only thing I figured so far is:

<jsp:scriptlet>
String vs = "";
cb.connect();
vs = cb.execSQL();
out.println(vs);
cb.close();
</jsp:scriptlet>

And having the html in bean:

public String execSQL() throws SQLException{
String sql1 = "SELECT ownerid, oname, ostreet FROM powners WHERE ownerid = ?";

    try
{

        PreparedStatement os  = obCon.prepareStatement(sql1);
os.setString(1, this.p11);
ResultSet ownerr = os.executeQuery();
StringBuilder l_sb = new StringBuilder();
            // l_sb.append("<table>");

            l_sb.append("<html>");
            l_sb.append("<head>");
            l_sb.append("</head>");
            l_sb.append("<body>");
            l_sb.append("<table width=95% border=1 id=tbl>");

           while (ownerr.next())
            {


                l_sb.append("<tr>");
                l_sb.append("<td>").append(ownerr.getString(1)).append("</td>");
                l_sb.append("<td>").append(ownerr.getString(2)).append("</td>");
                l_sb.append("<td>").append(ownerr.getString(3)).append("</td>");


               l_sb.append("</tr>");
l_sb.append("</table>");
            }
            // l_sb.append("</table>");
            //System.out.println(l_sb.toString());
            ownerr.close();
            os.close();
            obCon.close();

            
return l_sb.toString(); // here returning this to jsp page
// And the jsp page is outputting it to webpage.
}
  catch (SQLException se)
  {
    // log exception;
    throw se;
  }

}

All of this works, but I can only perform such as this using the <jsp:scriptlet> tag.
It seems there should be a way using the expression language. Or is this a form of the EL?

P.s. I have used servlets to completely present pages. But I am trying to learn the EL, and how to communicate with a bean. I know how with getters/setters, but calling other functions and returning result to jsp page.

Your execSQL method is not JavaBean.

JavaBean is a very specific standard that people have morphed into describing any class. Which is wrong.

Therefore, you will not be able to call the execSQL method in your class from the EL Expressions like you’re trying to.

It would be better if you used a servlet to execute the query take the results and place them into a model object (that does follow the JavaBean standards) and then pass that object though the HttpServletRequest to your jsp page.