Start New Discussion within our Web Development Community

I have this Code...

package source;

import java.sql.*;
import java.sql.SQLException;
import java.util.*;

public class ifsDAO {

    private Database database;

    public ifsDAO(Database database) {
        this.database = database;


    public List<ifsBean> list() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<ifsBean> ifs_array = new ArrayList<ifsBean>();

        try {

            connection = database.getConnection();
            statement = connection.prepareStatement("select * from emp_details");
            resultSet = statement.executeQuery();
            while ( {
                ifsBean arraylist = new ifsBean();
        } finally {
            if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
            if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
            if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}

        return ifs_array;

package source;

public class ifsBean {

    private String fname;
    private String sname;
    private String town;

    public String getFname() { return fname; }
    public String getSname() { return sname; }
    public String getTown() { return town; }

    public void setFname(String fname) { this.fname = fname; }
    public void setSname(String sname) { this.sname = sname; }
    public void setTown(String town) { = town; }

package source;

import java.sql.*;

public class Database {
    private String url;
    private String username;
    private String password;

    public Database(String driver, String url, String username, String password) {
        try {
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Driver class is missing in classpath", e);
        this.url = url;
        this.username = username;
        this.password = password;

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);

package source;

import java.sql.SQLException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class ifsServlet extends HttpServlet {

    private ifsDAO ifsDAO;

    public void init() throws ServletException {
        String driver = "org.postgresql.Driver";
        String url = "jdbc:postgresql://localhost/mydb";
        String username = "user";
        String password = "pass";

        Database database = new Database(driver, url, username, password);
        this.ifsDAO = new ifsDAO(database);

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

        try {
            List<ifsBean> ifs_array = ifsDAO.list();

            request.setAttribute("ifs_array", ifs_array);
            request.getRequestDispatcher("ifslist.jsp").forward(request, response);
        } catch (SQLException e) {
          throw new ServletException("Cannot retrieve areas", e);

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri=""%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    <c:forEach items="${ifs_array}" var="array">




I can list all the results...

however how can I filter the results here using a filter or a link and parameters...

However.. in I'll create

You can filter the results at your query by adding a WHERE. Look for some tutorials on how to write sql statements with WHERE:

select * from emp_details WHERE fname='something'

Then at the jsp page that submits to the ifsServlet you can pass the name or whatever you want as parameter and then pass that as argument to your dao, so you can get the filtered list.

Of course it is assumed that you how to write forms that submit values in jsp.

This article has been dead for over six months. Start a new discussion instead.