Results 1 to 3 of 3
  1. #1
    Payatola2287 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Listing problems

    Im having a problem on listing the result of a query.
    im kinda out of ideas. in my sample website, i want to pass the values from the servlet to the jsp file. i used ArrayList to display the data. i successfully display the data. but now i want it to be in a table so table it would be organized. i want to display one column from the database in one <td> tag. here's my codes

    List.jsp
    <%@ page import = "java.util.* , db.Show" language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!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">
    <title>List</title>
    </head>
    <body>
    <table>
    <tr>
    <%

    Show li = new Show();
    ArrayList al = li.getList();
    Iterator i = al.iterator();
    while(i.hasNext()){

    %>

    <td>
    <%= i.next() %>
    </td>

    <%
    }

    %>
    </tr>
    </table>
    </body>
    </html>

    Show.java

    Java Code:
    package db;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class List
     */
    @WebServlet("/Show")
    public class Show extends HttpServlet {
        
        public Show() {
           
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        }
    
        public ArrayList<String> getList(){
            String driver = "org.sqlite.JDBC";
            String url = "jdbc:sqlite:D:\\Java\\workspace\\Softdev\\SampleWebProj\\WebContent\\Customer.db";
            String sql = "";
            PreparedStatement ps = null;
            ResultSet r = null;
            Connection con = null;
            ArrayList<String> l = new ArrayList<String>();
            try{
                Class.forName(driver).newInstance();
                con = DriverManager.getConnection(url);
                sql = "SELECT * FROM Account";
                ps = con.prepareStatement(sql);
                r = ps.executeQuery();
                
                while(r.next()){
                    String username = r.getString("username");
                    String password = r.getString("password");
                    l.add(username);
                    l.add(password);
                    
                }
            }catch(Exception s){
                
                s.printStackTrace();
            }
            return l;
        }
    }
    Last edited by Eranga; 03-04-2012 at 08:14 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: Listing problems

    Please use code tags when you are posting code segments in the forum. Unformatted codes are really hard to read.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,446
    Rep Power
    18

    Default Re: Listing problems

    First, that servlet is seriously wrong...especially the instantiation of it in the JSP.

    The servlet is constructed by your server.
    It is called (doGet or doPost) when the client makes a request that maps to that servlet (in the web.xml).
    You would then do your business logic (generally not in the servlet, but in a layer that is called by the servlet, in your case a DataAccessObject possibly, but we'll skip that bit) and place the List into the request (setAttribute). It would then forward to your JSP page.
    The JSP would retrieve the List from the request (getAttribute) and display it.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. listing files at the FTP
    By oulutas in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 11:30 AM
  2. Listing Installed DSA
    By anoopasta in forum XML
    Replies: 1
    Last Post: 05-06-2010, 04:10 PM
  3. Listing the alphabet (beginner)
    By SuperOnion in forum New To Java
    Replies: 3
    Last Post: 12-22-2008, 03:23 AM
  4. Listing Logical Drives
    By Juggler in forum New To Java
    Replies: 3
    Last Post: 08-10-2008, 07:08 AM
  5. Listing all available Locales
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •