Manish Grover
DataGrid without DataBase
2 Answer(s)      4 years and 5 months ago
Posted in : EJB

I want display list of data without using database. Ex- Employee have some elements like- name, sex, place, designation. I have to display the content of these elements but without having this information in database, but this can be present in java file.


NAME SEX PLACE DEGREE
Manish Male Delhi CA Aman Male Delhi MBA

above you can see, it can show like this. The Data of manish and aman will be in java file not in Database. Using JSP for frontend and java file for backend, no database.

Advertisement
View Answers

April 28, 2011 at 12:53 PM


1)PaginationServlet.java:

import java.io.*;
import java.util.*;
import form.Student;
import javax.servlet.*;
import javax.servlet.http.*;

public class PaginationServlet extends HttpServlet {
    int offset;
    int length;
    List list;

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

        int maxEntriesPerPage = 2;
        int page = 1;
        String pageNumberValue = request.getParameter("pageNumber");

        if (pageNumberValue != null) {
            try {
                page = Integer.parseInt(pageNumberValue);
                System.out.println("Page Number:" + page);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        int offset = maxEntriesPerPage * (page - 1);
        TestList(offset, maxEntriesPerPage);

        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("pages", getPages());
        httpSession.setAttribute("studentDetails", getListByLength());

        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/paging.jsp");
        dispatcher.forward(request, response);
    }
    public void fillList() {
        list = new ArrayList();
        list.add(new Student("A", "Delhi", 25, "Btech"));
        list.add(new Student("B", "Mumbai", 26, "Mtech"));
        list.add(new Student("C", "Kolkata", 27, "MSC"));
        list.add(new Student("D", "Chennai", 28, "MBA"));
        list.add(new Student("E", "Hyderabad", 23, "MCA"));
        list.add(new Student("F", "Agra", 24, "MBBS"));
        list.add(new Student("G", "Shimla", 26, "BDS"));
        list.add(new Student("H", "Lucknow",25,"MCom"));
    }
    public void TestList(int offset, int length) {
        this.offset = offset;
        this.length = length;
        fillList();
    }
    public ArrayList getListByLength() {
        ArrayList arrayList = new ArrayList();
        int to = this.offset + this.length;
        if (this.offset > list.size())
            this.offset = list.size();
        if (to > list.size())
            to = list.size();
        for (int i = this.offset; i < to; i++) {
            arrayList.add(list.get(i));
        }
        return arrayList;
    }
    public List getPages() {
        List pageNumbers = new ArrayList();
        int pages = list.size() / this.length;
        if (list.size() % this.length != 0) {
            pages = pages + 1;
        }
        for (int i = 1; i <= pages; i++) {
            pageNumbers.add(new Integer(i));
        }
        return pageNumbers;
    }
}


April 28, 2011 at 12:55 PM


continue..

2)Student.java

package form;
import java.io.*;

public class Student implements Serializable {

    private String name;
    private String address;
    private int age;
    private String degree;

    public Student(String name, String address, int age,String degree) {
        this.name = name;
        this.address = address;
        this.age = age;
        this.degree = degree;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

}

The above java class is a bean and should be located inside /classes/form/Student.

3)paging.jsp:

<%@page import="java.util.List"%>
<%@page import="form.Student"%>
<html>
<h1>Pagination</h1>
<%
    List list = (List) session.getAttribute("studentDetails");
    List pageNumbers = (List) session.getAttribute("pages");
%>
<table border="1">
    <tr><th>Name</th><th>Address</th><th>Age</th><th>Degree</th></tr>
    <%
        for (int i = 0; i < list.size(); i++) {
        Student st = (Student) list.get(i);
        %>
                <tr>
                <td><%=st.getName()%></td>
                <td><%=st.getAddress()%></td>
                <td><%=st.getAge()%></td>
                <td><%=st.getDegree()%></td></tr>
        <%
        }
        %>
        <td colspan="4" align="right">
        <form method="get" action="../PaginationServlet">
        <table>
            <tr>
                <%
                    for (int i = 0; i < pageNumbers.size(); i++) {
                %>
                <td><a href="/examples/PaginationServlet?pageNumber=<%=pageNumbers.get(i)%>"><%=pageNumbers.get(i)%></a></td>
                <%
                    }
                %>
            </tr>
        </table>
        </form>
        </td>
    </tr>
</table>
</html>









Related Tutorials/Questions & Answers:
DataGrid without DataBase
DataGrid without DataBase  I want display list of data without using database. Ex- Employee have some elements like- name, sex, place, designation. I have to display the content of these elements but without having
datagrid
datagrid   please send datagrid in jsp lot fields including date
Advertisements
I want to Populate ComBobox value from database that combox is Itemrenderer of DataGrid.
I want to Populate ComBobox value from database that combox is Itemrenderer of DataGrid.  I want to Populate ComBobox value from database that combox is Itemrenderer of DataGrid
Pagination without using database in php
Pagination without using database in php  How can I show multiple images in multiple rows with pagination in php
Designing a Database with a junction table or without it?
Designing a Database with a junction table or without it?  Designing a group of table with, where many tables reference single parent table. In that case is it a better option to create a junction table to maintain
Draw graph using jsp without database connection
Draw graph using jsp without database connection  Draw graph using jsp code without database connection
connect database without specifying dsn name in java
connect database without specifying dsn name in java  How can i connect the database with my application without specifying the dsn name by using jdbc & odbc..... i have saw this code on the web-site try
jsp-datagrid
jsp-datagrid  hello sir.....i have 20 records in my database and i am showing those data in datagrid view but now i want to show 10 data in one page... option in my datagrid view with 1,2 pages....plz help me...awaiting for your
jsp-datagrid
jsp-datagrid  hello sir.....i have 20 records in my database and i am showing those data in datagrid view but now i want to show 10 data in one page... option in my datagrid view with 1,2 pages....plz help me...awaiting for your
DataGrid in flex
DataGrid in flex  Hi.... Please tell me What is the data type of the dataprovider property in DataGrid? please give an small example for that.... Thanks
DataGrid in flex
DataGrid in flex  Hi..... Please tell me about DataGrid extends which class? please give me the syntax......... Thanks
datagrid including combobox
datagrid including combobox  please send datagrid in jsp using mysql database based on id in the datagrid select each record and also update record with available to old values updation is done in form wise. using combo box
editable datagrid
editable datagrid  How to create a editable datagrid in jsp,struts1.3,glassfish v2.x application server and database oracle using netbeans6.8 ide?   Hi Friend, Please visit the following link: http://www.roseindia.net
retrive image from database using jsp without stream
retrive image from database using jsp without stream  How to retrive image from database using jsp without stream like (inputStream
DataGrid in flex
DataGrid in flex  Hi.... What kind of data grids you can create through Flex applications? please tell me about the datagrid so i can implement it in my application .... Thanks
DataGrid in flex
DataGrid in flex  Hi....... I want to know about How do you access methods in DataGrid from external itemRenderers? please give an example so i can clearly understand.... Thanks
without ;
without ;  can u give me an example of a program without
without ;
without ;  can u give me an example of a program without
How to save form fields into the MySql Database without submit button in jsp?
How to save form fields into the MySql Database without submit button in jsp?  I want to store user inputs into the database using javasccript or ajax or jqury but without submit button. Form Contains three fields
Datagrid not working
Datagrid not working  The code here is working fine, apart from... the datagrid tag gives an error saying that the items attribute does not accept ant... This works fine for datagrid but then the javascript for drop down menu stops
ArrayCollection in DataGrid
ArrayCollection in DataGrid  Hi.... I want to know about What are the advantages of using arraycollection as a dataprovider instead of array in DataGrid? please give the ans ASAP....... Thanks
DataGrid in flex
DataGrid in flex  Hi.... please provide the solution of that problem When I have only a single record, why doesn't it appear in my DataGrid? please give an example Thanks  Ans: This is a caused in flex
DataGrid in flex
DataGrid in flex  Hi........ Please provide the solution of that problem When I add or modify an item in my dataProvider, why doesn't it show up in my DataGrid? Thanks in advance  Ans: When you work
dyanamic upload of images using struts without database connection
dyanamic upload of images using struts without database connection  I want to upload images dynamically with out using data base..and want to create image for every user in my db
DataGrid ItemRenderer
DataGrid ItemRenderer  Hi... I have a datagrid which contains a Spark dropdownlist that needs to obtain dynamic data. The datagrid uses a separate..." backgroundColor="#000000"> <mx:DataGrid id="myDG" dataProvider="{myDP
datagrid search - Hibernate
datagrid search  Hi friend, i have displayed a datagrid using jsp but i need a search option in that which is used to display and sort...;Hi Friend, Do you want to get sorted data from the database along
jsp-datagrid
jsp-datagrid  hello sir...i have one view page in which edit and delete option are there in a datagrid view and i want to show data in page wise... code retrieves all the data from the database table and stored it into html table
jsp-datagrid
jsp-datagrid  hello sir...i have one view page in which edit and delete option are there in a datagrid view and i want to show data in page wise(pagination)...with previous and next links....i am also attaching my code for your
jsp-datagrid
jsp-datagrid  hello sir...i have one view page in which edit and delete option are there in a datagrid view and i want to show data in page wise(pagination)...with previous and next links....i am also attaching my code for your
How can i write a datagrid coding - Java Beginners
How can i write a datagrid coding  Hi, I want make a datagrid..plz send me datagrid coding using mysql database and jsp..plz tell me brief description about grid how can i connect datagrid with database. Thanks
How sql Queries possible in DAO without creating Database connections - Java Beginners
How sql Queries possible in DAO without creating Database connections  In DAO we are writting sql queries , how it is possible without creating and closing database connections