-1

I searched every possible solution but still no outcome of fix for this. It says that in line 4 of my jsp, there is an error that the bean is not found within scope.

Here is my jsp code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<jsp:useBean id="records" type="java.sql.ResultSet" scope="request"></jsp:useBean>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Gasoline eStore</title>
</head>
<body>
    <h1>Gasoline eStore</h1>
        <table class="table" align="center" border="1" cellpadding="2" cellspacing="2">
                <tr>
                <th>Name</th>
                <th>Sales Amount</th>
                <th>Sales Gross Commission</th>
                <th>Sales Commission</th>
                <th>Take Home Pay</th>

                </tr>


                <%
                boolean empty = true;
                while (records.next()) { 
                empty = false;
                %>

                    <tr class="success">
                        <td align="center"><%=records.getInt("id") %></td>
                        <td align="center"><%=records.getString("gasType") %></td>
                        <td align="center"><%=records.getDouble("litervalue") %></td>
                        <td align="center"><%=records.getDouble("initialAmount") %></td>
                        <td align="center"><%=records.getDouble("vat") %></td>
                        <td align="center"><%=records.getDouble("totalAmount") %></td>      
                    </tr>   
                <% 
                } %>

            </table>
                <% if(empty){ %>    
                    <div class="form-group has-error">
                        <label class="control-label" for="inputError1">No records found.</label>
                    </div>
                <% } %>

            <form action="index.jsp" method="post"> 
            <p><input class="btn btn-primary" type="submit" value="GO BACK">
        </form>

</body>
</html>

Here is my Servlet

package gas.store.controller;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import gas.store.model.GasBean;
import gas.store.utility.DBConnectionUtil;
import java.sql.*;

public class ListRecordServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private Connection connection = null;

    public void init() throws ServletException {
        connection = DBConnectionUtil.getDBConnection();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(connection != null) {
            ResultSet records = new GasBean().getAllRecordsTable(connection);

            request.setAttribute("records", records);

            request.getRequestDispatcher("listrecords.jsp").forward(request,response);
        } else {
            System.err.println("connection is NULL");
        }
    }

}``

1 Answers1

1

use JSTL

In your JSP get rid of Java code and use JSTL

In your servlet code create a ArrayList of Record Objects (A DTO for each record). The Record bean class would have proper setter/getters.

e.g.

<c:forEach items="${records}" var="rec" varStatus="loop">

   <td align="center">${rec.id}</td>
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64