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.
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.
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; } }
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>
Ads