Paging in Jsp

hi
this is my jsp code:



<%@ 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>

    <%@page import = "policeLibrary.*,java.sql.ResultSet;" %>
<%
int uid = 5;//get current user from session
%>
<form>
<div class="midleft">

  <div class="importantlinks">

  <h3 align="center"><b>List Of Past Complaints</b></h3></div>

<div class="message">
<table border="1" width = "100%">
      <tbody>
            <tr>
            <td width = "13%">Date</td>
                  <td width = "5%">Complaint Id </td>
                  <td width = "30%">Subject</td>
            
            </tr>
            
<%

Complaints m = new Complaints();
ResultSet result = m.getComplaints(uid);
int i=1;
while(result.next())
{
int cid = result.getInt("Cid");
String subject = result.getString("subject");
subject = subject.length()>21?subject.substring(0,19)+"...":subject;

String date = result.getString("Incidentdate");

%>
            <tr>
            <td><%= date %></td>
                  <td><%= cid%></td>
                  
                  <td><%= subject%></td>
            </tr>
<%
}
%>
      </tbody>
</table>
</div>
</div>
</form>

</body>
</html>



in the above jsp code the cid,subject and incident is obtained from the database(DB2) using classes in the Package called Police Library.

Please give the code for inserting paging. i need 10 entries.


manikandan689Asked:
Who is Participating?
 
rrzConnect With a Mentor Commented:
Were any of the debug messages, that I inserted into the code,  printed in your console ???
I put those messages into the code so that we could see where the execution stops.  So if you can't see any of the messages then we aren't getting anywhere.  
The latest error message  probably is telling us that your query is not constructed properly. But I am guessing on this.  Maybe you should open another question where you could get the help of a database expert.  Did you  try  using a simple query ?   Maybe
String query = "select * from complaint";  
or something similar.

If you can't configure your present development environment to give proper error messages and stacktraces, then maybe you should try something else. Eclipse and Netbeans IDEs are free.    Tomcat is free also.  I use Tomcat standing alone on my desktop. It gives proper error messages that allows me to track down problems easily.  

0
 
Sathish David Kumar NArchitectCommented:
manikandan can you tell some what breif ? i am not getting ur question clearly ??
0
 
manikandan689Author Commented:
hello,

   i actually have a list of complaints stored for different   persons in the database.

when a person requests to see all his complaints. i have to display it dynamically. Now if his list of complaints exceeds 10 in number then i have to display in subsequent pages.
In this fuctionality i don't know how to display the list of complaints in subsequent pages(paging).

i.e implementing next , back and page numbers.

in the above table i am creating an object 'm' of class complaints.
assuming an arbitrary value of user id  i.e uid=5 in the above code and retrieving the complaint id(cid), subject of the complaint(subject), incident date(Incident date).


after retrieving the details i am creating a table in which i am displaying these fields (cid,subject,incident date).

Now i just need the code for implementing next and back buttons.


Please help.
i'm an engineering student with no much knowledge of jsp as i do not have it in my syllabus.

 
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
chaitu chaituCommented:
0
 
manikandan689Author Commented:


Since i am doing it as my project work. they expect everything to be own codes.

so i cannot use any of the codes specified in the links that you gave
0
 
Sathish David Kumar NArchitectCommented:
dont use the same one??

customize what u need from them?
0
 
rrzCommented:
Can you use JSTL ?  If yes, then see my solution at
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_24321162.html 
If you want to use just JSP, then we could help you transform it.
0
 
manikandan689Author Commented:
No i am not supposed to use jstl.

it must be of Jsp. i hope you saw my jsp code.

it would be of great helpful for me if you could add paging for my code.



0
 
rrzCommented:
I am going to bed soon. I could help you  some more in the morning.
Tonight, we could decide on a data structure that we could set as a session-scoped object.  
>retrieving the complaint id(cid), subject of the complaint(subject), incident date(Incident date).
Maybe we could use  a  ArrayList of HashMaps.  Something like  

while(result.next())
{  
...
map.put("cid", cid);
map.put("subject", subject);
map.put("date", date);  
list.add(map);
}
session.setAttribute("complaints", list);

Will this be good ?
0
 
manikandan689Author Commented:
ya fine.

after how long can i expect you to be back.

within 8hrs from now?
0
 
rrzCommented:
I'll be back in 10-12 hours. You could post some dummy data that we could use for testing. We just need to populate the list with some test data and then proceed with the display code.  That way we can all test the JSP part and not worry about the database code.
0
 
manikandan689Author Commented:
Fine.
 
one more thing


how do i pass parameter from one jsp file to another on clicking a link.

is this correct:
<a href="complaints-view.jsp?param1=uid&param2=cid">
                          
                          <%= subject %></a>

here on clicking on the subject which is dynamically generated i call complaints-view.jsp passing two parameters.


thsi is the code in complaints-view.jsp
<%= request.getParameter("param1") %>
<%= request.getParameter("param2")%>

Am i right?
0
 
Sathish David Kumar NArchitectCommented:
its some what correct

u want to pass only uid and cid means then urs correct but value means pas below like

<a href="complaints-view.jsp?param1='sathish'&param2=cid">
                         
0
 
Sathish David Kumar NArchitectCommented:
ya correct ! why your not using JSTL??

if the hyper link is form submit means then no need to pass the data u can  directly get that !!
0
 
rrzCommented:
>how do i pass parameter from one jsp file to another on clicking a link.  
I thought that we would put  uid  and  the complaints list  into session-scoped objects.
0
 
manikandan689Author Commented:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page 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>

    <%@page import = "policeLibrary.*,java.sql.ResultSet;" %>
<%
int uid=6;
int cid=3;
 
%>

<div class="midleft">

  <div class="importantlinks">

  <h3 align="center"><b> Complaint Details</b></h3></div>

<div class="message">

<%

Complaints m = new Complaints();
ResultSet result = m.getComplaintDetails(uid,cid);
 cid = result.getInt("Cid");
 int pid = result.getInt("Pid");
 int psid = result.getInt("Psid");
String subject = result.getString("Csubject");
String description = result.getString("Cdescription");
String date = result.getString("Incidentdate");
String suspect = result.getString("Suspect");
String placeofoccurance = result.getString("Placeofoccurane");
String status = result.getString("Iscomplaintclosed");
String createdon = result.getString("Creationtime");
%>

Date:<p><%= createdon %></p><br/><br/>
Complaint Id:<%= cid %><br/><br/>
Assigned Police:<%= pid %><br/><br/>
Police Station:<%= psid %><br/><br/>
Subject:<%= subject %><br/><br/>
Description:<%= description %><br/><br/>
Date on which Incident Occured:<%= date %><br/><br/>
Place of Occurance:<%= placeofoccurance %><br/><br/>
Any Suspect:<%= suspect %><br/><br/>
Complaint Status:<%= status %><br/><br/>


</div>
</div>
</body>
</html>



the page is not displaying.it is showing nullpointer exception.

please help
0
 
rrzCommented:
>the page is not displaying.it is showing nullpointer exception.
Try putting the lower part in
while(result.next()){}

We need to get on the same page. It looks like you changed the data structure.
First copy and paste the following  test JSP.  Please tell me if I am going in the right direction.
<%@ page import="java.util.*" %>
<%
  ArrayList<HashMap> testList = new ArrayList<HashMap>();
  for(int i = 0; i < 35; i++){
      HashMap<String,String> testMap = new HashMap<String,String>();
      testMap.put("date", "d" + i);
      testMap.put("cid", "cid" + i);
      testMap.put("subject", "sub" + i);
      testList.add(testMap);
      testMap = null;
  }
  session.setAttribute("complaints", testList);
  session.setAttribute("uid", "test123");
%>

<%
   int totalCount = 0;
   int perPage = 10;
   int pageStart = 0; 
   String start = request.getParameter("start");
   if(start != null)pageStart = Integer.parseInt(start);
   if(pageStart < 0)pageStart = 0;
   String uid = (String)session.getAttribute("uid");
   ArrayList<HashMap> complaints = (ArrayList)session.getAttribute("complaints");
   if(complaints != null){
          totalCount = complaints.size();
          if(pageStart >= totalCount)pageStart = pageStart - perPage;
   }  
%>
<a href="<%=request.getRequestURL()%>?start=<%=pageStart - 10 %>"><<</a>
<%=pageStart + 1 %> - <%=pageStart + 10 %> 
<a href="<%=request.getRequestURL()%>?start=<%=pageStart + 10 %>">>></a>
<h1>Complaints</h1>
<%
   if(uid != null && totalCount != 0){
       out.print("User: " + uid + "<br/>");
       HashMap<String,String> map = new HashMap<String,String>();
       for(int x = pageStart; x < pageStart + perPage && x < complaints.size(); x++){
           map = complaints.get(x);
           out.print(map.get("date") + " " + map.get("cid") + " " + map.get("subject") + "<br/>");
       }
   }else {
          out.print("no data");
         }
%>

Open in new window

0
 
manikandan689Author Commented:
ya
the code works correctly.


Now my major concern is the above displayed code.

for the time being i just need soultion for that .


previously i had given you a jsp page which loads

date  cid  subject (displayed with paging)

now if the user clicks on any of the subject i have to give full details about the complaint like incident date,description,police assigned etc which is what i am trying to display taking an arbitrary value for the uid and cid but getting an Error.

i tried my sql statements in the java classes. it is working fine.

Now this is my primary concern.
 
please help
0
 
rrzCommented:
>the page is not displaying.it is showing nullpointer exception.  
Please tell us what line is causing the problem.
Is there anything in the ResultSet ?
I am just a JSP expert. I am not a database expert. But I will try to help. Try adding debug statements. Maybe use
 out.print("fetchSize is " + getFetchSize());
0
 
rrzCommented:
I meant  
out.print("fetchSize is " + result.getFetchSize());
0
 
manikandan689Author Commented:
null [Ljava.lang.StackTraceElement;@595e595e


i am getting the following error.

do you have any idea
0
 
rrzCommented:
What line is causing problem ?
0
 
rrzCommented:
Just try something like this to see if the query is working.
<%
Complaints m = new Complaints();
ResultSet result = m.getComplaintDetails(uid,cid);
out.print("fetchSize is " + result.getFetchSize());
%>
0
 
manikandan689Author Commented:
This Is my Code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page 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>

    <%@page import = "policeLibrary.*,java.sql.ResultSet;" %>
  <%
    try
{
   
int uid=6;
int cid=3;
 
%>

<div class="midleft">

  <div class="importantlinks">

  <h3 align="center"><b> Complaint Details</b></h3></div>

<div class="message">

<%

Complaints m = new Complaints();
ResultSet result = m.getComplaintDetails(uid,cid);
if(result.next())
{

 cid = result.getInt("Cid");
 int pid = result.getInt("Pid");
 int psid = result.getInt("Psid");
String subject = result.getString("Csubject");
String description = result.getString("Cdescription");
String date = result.getString("Incidentdate");
String suspect = result.getString("Suspect");
String placeofoccurance = result.getString("Placeofoccurane");
String status = result.getString("Iscomplaintclosed");
String createdon = result.getString("Creationtime");
result.next();


%>
      
Date:<p><%= createdon %></p><br/><br/>
Complaint Id:<%= cid %><br/><br/>
Assigned Police:<%= pid %><br/><br/>
Police Station:<%= psid %><br/><br/>
Subject:<%= subject %><br/><br/>
Description:<%= description %><br/><br/>
Date on which Incident Occured:<%= date %><br/><br/>
Place of Occurance:<%= placeofoccurance %><br/><br/>
Any Suspect:<%= suspect %><br/><br/>
Complaint Status:<%= status %><br/><br/>

<%
}
else

throw new Exception();
}
catch(Exception e)
{
      out.println("There were errors during processing: "+e.getMessage()+"\n"+e.getStackTrace());


      return;
}  
 %>


</div>
</div>
</body>
</html>






this is my java class:

public ResultSet getComplaintDetails(int uid, int cid)
            {
                  
                  try{
                        if(uid<1)
                        {
                        return null;
                        }
                        String query="SELECT complaint.Cid,complaint.Csubject,complaint.Incidentdate,complaint.Ctype,complaint.Pid,complaint.Psid,complaint.Cdescription,complaint.Suspect,complaint.Placeofoccurane,complaint.Iscomplaintclosed,complaint.Creationtime" +
                                    " FROM complaint"+
                                    "where complaint.Userid="+uid+"and complaint.Cid="+cid;
                        ResultSet result = conn.runResultSetQuery(query);
                        return result;
                        
                  }
                  catch (Exception e)
                  {
                        
                        return null;
                        
                  }
            }



This is the  database



CREATE TABLE complaint(
Cid INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY NOT NULL,
Ctype INTEGER NOT NULL,
Pid INTEGER NOT NULL,
Psid INTEGER NOT NULL,
Userid INTEGER NOT NULL,
Incidentdate TIMESTAMP DEFAULT CURRENT TIMESTAMP,
Csubject VARCHAR(20) ,
Cdescription VARCHAR(20) NOT NULL,
Suspect VARCHAR(20),
Placeofoccurane VARCHAR(20) ,
Iscomplaintclosed VARCHAR(1) ,
Creationtime TIMESTAMP DEFAULT CURRENT TIMESTAMP,
FOREIGN KEY(Pid) REFERENCES police(Uid) ON DELETE CASCADE,
FOREIGN KEY(Userid) REFERENCES user(Userid) ON DELETE CASCADE,
FOREIGN KEY(Psid) REFERENCES policestation(Psid) ON DELETE CASCADE,
FOREIGN KEY(Ctype) REFERENCES complainttype(Ctypeno) ON DELETE CASCADE
);


this is the value for uid=6 and cid=3 (expected output but not getting)

CID    :3
 CSUBJECT:   Missing car

  INCIDENTDATE  :  2011-03-20-03.33.04.554000
           
  CTYPE: 1
   
    PID:       8

  PSID:1
   
    CDESCRIPTION    :brand new mercedez

     SUSPECT :      mateenahamed

       PLACEOFOCCURANE:austin town
 
    ISCOMPLAINTCLOSED: n

 CREATIONTIME:              2011-03-20-03.33.04.554000



i have sent everything.





  .
0
 
manikandan689Author Commented:


i tried the code which you gave

i am getting the following error

Error 500: java.lang.NullPointerException
0
 
rrzCommented:
Let's try to get to a starting point. Please copy and paste the following test.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="java.util.*,policeLibrary.*,java.sql.ResultSet" %>
<!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>Testing</title>
</head>
<body>
<%
  int uid=6;
  int cid=3;
  Complaints m = new Complaints();
  ResultSet result = m.getComplaintDetails(uid,cid);
  out.print("---------------------fetchSize is " + result.getFetchSize()); 
%>
</body>
</html>

Open in new window

0
 
manikandan689Author Commented:
I am getting the following error

Error 500: java.lang.NullPointerException
0
 
rrzCommented:
What line is causing problem ?
Are you using Tomcat ?
Please show the whole error message.
0
 
manikandan689Author Commented:
i'm using


IBM RAD 7.0
0
 
manikandan689Author Commented:
i'm unable to debug in this RAD to find the exact line
0
 
rrzCommented:
>IBM RAD 7.0
I haven't used that.  
Try changing the method in your java class to
            public ResultSet getComplaintDetails(int uid, int cid)
            {  
                        String query="SELECT complaint.Cid,complaint.Csubject,complaint.Incidentdate,complaint.Ctype,complaint.Pid,complaint.Psid,complaint.Cdescription,complaint.Suspect,complaint.Placeofoccurane,complaint.Iscomplaintclosed,complaint.Creationtime" +
                                    " FROM complaint"+
                                    "where complaint.Userid="+uid+"and complaint.Cid="+cid;
                        ResultSet result = conn.runResultSetQuery(query);
                        return result;
                        
                  }
                  catch (Exception e) 
                  {
                        e.printStackTrace();
                  }
            }

Open in new window

0
 
manikandan689Author Commented:
i am getting same null pointer exception
0
 
rrzCommented:
I meant to post
          public ResultSet getComplaintDetails(int uid, int cid)
            {  
               try{
                        String query="SELECT complaint.Cid,complaint.Csubject,complaint.Incidentdate,complaint.Ctype,complaint.Pid,complaint.Psid,complaint.Cdescription,complaint.Suspect,complaint.Placeofoccurane,complaint.Iscomplaintclosed,complaint.Creationtime" +
                                    " FROM complaint"+
                                    "where complaint.Userid="+uid+"and complaint.Cid="+cid;
                        ResultSet result = conn.runResultSetQuery(query);
                        return result;
                        
                  }
                  catch (Exception e) 
                  {
                        e.printStackTrace();
                  }
            }

Open in new window

0
 
manikandan689Author Commented:
i am getting same null pointer exception
0
 
rrzCommented:
Try this
            public ResultSet getComplaintDetails(int uid, int cid)
            {  
                  try{
                         Statement stmt = connection.createStatement();
                         String query="SELECT complaint.Cid,complaint.Csubject,complaint.Incidentdate,complaint.Ctype,complaint.Pid,complaint.Psid,complaint.Cdescription,complaint.Suspect,complaint.Placeofoccurane,complaint.Iscomplaintclosed,complaint.Creationtime" +
                                    " FROM complaint"+
                                    "where complaint.Userid="+uid+"and complaint.Cid="+cid;
                        ResultSet rs = stmt.executeQuery(query);
                        return result;
                        
                  }
                  catch (Exception e) 
                  {
                        e.printStackTrace();
                  }
            }

Open in new window

0
 
manikandan689Author Commented:
it says connection cannot be resolved to a type;
0
 
rrzCommented:
change
Statement stmt = connection.createStatement();  
to
Statement stmt = conn.createStatement();
0
 
manikandan689Author Commented:
the method createStatement()  is undefined for the type database
0
 
rrzCommented:
I had assumed that conn was a java.sql.Connection.  
Where is conn defined ?
Where is your method runResultSetQuery defined ?
0
 
rrzCommented:
Are you still working ?
0
 
manikandan689Author Commented:
hi
is anybody there?
0
 
rrzCommented:
Yes.
Where is your method runResultSetQuery defined ?
0
 
manikandan689Author Commented:
     
      public ResultSet runResultSetQuery(PreparedStatement statement)
      {
            ResultSet r=null;
            try
            {
                  curQuery = null;
                  curQueryStatement = statement;
                  r = curQueryStatement.executeQuery();
            }
            catch(Exception e)
            {
                  System.out.println(e.getMessage());
            }
            return r;
      }

        
      public ResultSet runResultSetQuery(String query){
            ResultSet r=null;
            try
            {
                  curQuery = query;
                  curQueryStatement = c.prepareStatement(curQuery);
                  r = curQueryStatement.executeQuery();
            }
            catch(Exception e)
            {
                  System.out.println(e.getMessage());
            }
            return r;
      }
      
these are my connections in database class belonging to the same package.

0
 
rrzCommented:
Since you don't know where the null pointer exception is coming from,
you will have to add debug code. I assume that you will get these messages on your console.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="java.util.*,policeLibrary.*,java.sql.ResultSet" %>
<!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>Testing</title>
</head>
<body>
<%
  int uid=6;
  int cid=3;
  System.out.println("before creating Complaints object m");
  Complaints m = new Complaints();
  System.out.println("m obect is " + m.toString());
  ResultSet result = m.getComplaintDetails(uid,cid);
  out.print("testing"); 
%>
</body>
</html>

Open in new window

In your  method  
public ResultSet runResultSetQuery(String query){
            System.out.println("inside method runResultSetQuery");
            System.out.println("query is " + query);
            ResultSet r=null;
            try
            {
                  curQuery = query;
                  curQueryStatement = c.prepareStatement(curQuery);
                  System.out.println("before executeQuery");
                  r = curQueryStatement.executeQuery();
                  System.out.println("after executeQuery");
                  System.out.println("r type is" + r.getType());
            }
             catch(NullPointerException){ System.out.println("caught NPE in runResultSetQuery"); }
            catch(Exception e)
            {
                  System.out.println(e.getMessage());
            }
            return r;
      }

Open in new window

0
 
rrzCommented:
I saw your other question;
http://www.experts-exchange.com/Database/DB2/Q_26898817.html 
You have three database experts commenting  there. Why don't you respond ? You will probably need their help later.  Just ignoring them is not nice. Tell them what you are doing.
0
 
manikandan689Author Commented:
i am getting the following error

Error 500: com.ibm.db2.jcc.a.SqlException: [jcc][t4][10120][10898][3.52.90] Invalid operation: result set is closed. ERRORCODE=-4470, SQLSTATE=null
0
 
manikandan689Author Commented:
hi

ya i  checked . it is working.

now i have no issues with it.


you have helped me a lot.

THANKS A LOT!!!!!!!!!!!!
0
 
rrzCommented:
I noticed that you have started new questions. If you are done here then please close this out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.