i am developing online bit by bit exam for that i retrieved questions from an xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one question in one page for the next question user need to click on next buttun and also there is a timer for each question after time has completed system automatically moves to next question with out clicking on next buttun?for that i tried different coding but i didn't get what i actually want?can some one help me in this?urgent?
1)Servlet.java
import java.io.*; import java.util.*; import form.Student; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.servlet.*; import javax.servlet.http.*; public class Servlet extends HttpServlet { int offset; int length; List list; protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { int maxEntriesPerPage = 1; 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(); try{ File file = new File("c:\\file.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(file); document.getDocumentElement().normalize(); System.out.println("Root element "+ document.getDocumentElement().getNodeName()); NodeList node = document.getElementsByTagName("student"); System.out.println("Information of the students"); for(int i = 0; i < node.getLength(); i++){ Node firstNode = node.item(i); if(firstNode.getNodeType()== Node.ELEMENT_NODE){ Element element = (Element) firstNode; NodeList firstNameElemntList = element.getElementsByTagName("name"); Element firstNameElement = (Element) firstNameElemntList.item(0); NodeList firstName = firstNameElement.getChildNodes(); Node n1=firstName.item(0); String name=n1.getNodeValue(); NodeList lastNameElementList = element.getElementsByTagName("address"); Element lastNameElement = (Element)lastNameElementList.item(0); NodeList lastName = lastNameElement.getChildNodes(); Node n2=lastName.item(0); String address=n2.getNodeValue(); list.add(new Student(name,address)); } } for(Student st:list){ System.out.println(st.getName()+"\t"+st.getAddress()); } } catch (Exception e) { e.printStackTrace(); } } 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; public Student(String name, String address) { this.name = name; this.address = address; } 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; } }
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 border=1 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