Friday, January 18, 2013

Pagination test code

check.jsp
<%@ page 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>Insert title here</title>
</head>
<body>
<form action="./CSVLoginServlet" method="post">
<input type="submit" value="Submit">
</form>
</body>
</html>


result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.*"%>
<!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>Insert title here</title>
</head>
<body>
    <%
        ArrayList array = (ArrayList) session.getAttribute("array");
        String pages = (String) session.getAttribute("pages");
        int i = 0;
        while (i < array.size()) {

            out.print(array.get(i));
            i++;
        }
        int j = 1;

        while (j <= Integer.parseInt(pages) + 1) {

            out.println("<a href=result.jsp?id=" + j + ">" + j
                    + "</a>");

            j++;
        }
    %>
</body>
</html>


csvfile.java
/**
 *
 */
package doa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Boomiraj
 *
 */
public class CSVFile {
    public static ArrayList<String> getArrayList(ArrayList list) throws IOException{
        //FileWriter fw = new FileWriter("C:/Users/Boomiraj/Desktop/Boomiraj.csv");
       //fw.close();
       File file = new File("C:/Users/Boomiraj/Desktop/test.csv");
       FileReader fr = new FileReader(file);
       BufferedReader pr =new BufferedReader(fr);
       String dataRow = pr.readLine();
      //System.out.println(dataRow);
      // ArrayList array = new ArrayList();
       if(file.isFile()){
       System.out.println(dataRow);
       while (dataRow != null){
        String[] dataArray = dataRow.split(",");
        for (String item:dataArray) {
            //System.out.println(item);
          // pr.re(item+",");
            list.add(item);
           // System.out.println(list);
        }
       // System.out.println("test"); // Print the data line.
        dataRow = pr.readLine();
       fr.read();
     //  System.out.println(list);
       }}
       return list;
   }
    public static void main(String[] args) throws IOException {
        ArrayList list = new ArrayList();
        new CSVFile().getArrayList(list);
        /*FileWriter fw = new FileWriter("C:/Users/Boomiraj/Desktop/Boomiraj.csv");
           fw.close();*/
         /*  File file = new File("C:/Users/Boomiraj/Desktop/test.csv");
           FileReader fr = new FileReader(file);
           BufferedReader pr =new BufferedReader(fr);
           String dataRow = pr.readLine();
          // System.out.println(dataRow);
          ArrayList array = new ArrayList();
           if(file.isFile()){
           //System.out.println(dataRow);
           while (dataRow != null){
            String[] dataArray = dataRow.split(",");
            for (String item:dataArray) {
                //System.out.println(item);
              // pr.re(item+",");
                array.add(item);
               // System.out.println(array);
            }
           // System.out.println();
            //System.out.println("test"); // Print the data line.
            dataRow = pr.readLine();
           fr.read();
           System.out.println(array);
           }}*/
    }

}



csvservlet.java
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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

import dao.CSVBean;
import doa.CSVFile;
import doa.CSVFileReadWrite;

/**
 * Servlet implementation class CSVLoginServlet
 */
public class CSVLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CSVLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        /*PrintWriter out = response.getWriter();
        CSVBean csvBean = new CSVBean();
        csvBean.setFirstName(request.getParameter("firstname"));
        csvBean.setLastName(request.getParameter("lastname"));
        csvBean.setPhoneNumber(request.getParameter("phonenumber"));
       
        CSVFileReadWrite.writeFile(csvBean);
        out.println("Write Finished");
        */
       
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        CSVFile csv = new CSVFile();
        //csv.getArrayList();
        ArrayList array = new ArrayList();
        HttpSession session = request.getSession(true);
        String pageNo = request.getParameter("id");
        //System.out.println(csv.getArrayList(array));
        if(pageNo==null){
            pageNo="1";
        }
        final int offSet = 2;

        int pageNumber = Integer.parseInt(pageNo);
        if(pageNumber != 0){
        //System.out.println("67"+array.add(csv.getArrayList(array)));
        int numberOfData = array.size();
        //System.out.println(array);
        int totalPages = numberOfData/offSet;
        String noPages = Integer.toString(totalPages);
        System.out.println(noPages + 1);
        int nodata = offSet*pageNumber;

        int fistNo = nodata - offSet;
        //System.out.println("NO Of Data"+nodata);
        //System.out.println("From"+fistNo+"To"+nodata);

        if(nodata>array.size()){
            nodata = array.size();
        }
        ArrayList onlyData = new ArrayList();
        while (fistNo<nodata ){
            //StudentBean student = (StudentBean) arrayList.get(fistNo);
            //System.out.println(student.getName_Strudet());

            array.get(fistNo);
           
            fistNo ++;
        }
        }
        System.out.println("servlet 88"+array);
        session.setAttribute("array", array);
        session.setAttribute("pages", pageNo);
        response.sendRedirect("result.jsp");
    }

}

No comments:

Post a Comment