Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i want to avoid scriptlets on my jsp pages. I have google it and search here too but i was not able to solve it

i have read many posts that scriptlets is a bad practice so finally i decided to avoid this too. I have developed a project using struts framework in which mostly jsp pages have java code. And now i wanted to remove that code from my all the pages.

<%@page import="java.sql.ResultSet"%>
<%@page import="com.pra.sql.SQLC"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<table align="left" width="346" border="0">
    <tr><td align="center" colspan="3" style="font-size:20px;">Tests We Provide</td></tr>
    <tr>
        <th width="80" height="38" nowrap>Test ID</th>
        <th width="200" nowrap>Test Name</th>
        <th width="144" nowrap>Test Fee</th>
    </tr>
    <%
        String sql = "Select TestID,tname,tfee from addtest order by tname";
        ResultSet rs = SQLC.getData(sql, null);
        while (rs.next()) {
            String testid = rs.getString("TestID");
            String testname = rs.getString("tname");
            String testfee = rs.getString("tfee");
    %>
    <tr>
        <td width="80" height="34" nowrap><%=testid%></td>
        <td width="200" nowrap><%=testname%></td>
        <td width="144" nowrap><%=testfee%></td>
    </tr>  
    <%
        }
    %>
</table>
like image 436
Maninder Avatar asked Feb 01 '26 06:02

Maninder


1 Answers

  1. Associate the JSP with an action and thus an Action class
  2. Execute the query in Java code - you could do it in the Action, though I'd recommend passing control to a separate data access layer
  3. Save the results of the query into a List of a bean type that holds each of the columns in your result
  4. Set the List as an attribute of the request
  5. Use a tag library like to loop over the the List, and print the output.
like image 117
Rajesh J Advani Avatar answered Feb 03 '26 18:02

Rajesh J Advani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!