0

I have a Search JSP page that will accept some input fields and send query to database with respective given fields . if there are 2000 relevant records in the database, the page will show the first 30 results and a next button to show succeeding results or previous button for previous results, just like when we search in Google. Now my problem is I don't have any idea how to do these things. I know basic of servlets, JSP, JDBC. is there anyone resolve this problem? suggestions and ideas are most appreciated. code will be like this

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "com.preva.vo.StoppageDetails"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<link href="css/cal.css" rel="stylesheet" type="text/css" />
<link href="css/sty.css" rel="stylesheet" type="text/css" />
<link href="css/tabborder.css" rel="stylesheet" type="text/css" />
</head>
<body>
                 <jsp:include page="Header.jsp" />

            <table align=center border=0 cellspacing=0 cellpadding=0>
            <tr ><td colSpan=5 align=center><b>Overspeed Details</b></td></tr>
            <tr >
      <td colspan=5 align=center>
      <b><%=request.getParameter("vehicleId") %></b></td></tr>
            <tr><td>From &nbsp;
    <%=session.getAttribute("fromdate") %>
     &nbsp;to&nbsp;       
     <%=session.getAttribute("startdate") %></td></tr>

            </table><br></br>
     <table class='rptTbl_sortable' width='80%' align=center cellspacing='2'  cellpadding='0' border='0'>

          <thead>
          <tr class="rptHdrRow">
         <th id="index" class="rptHdrCol_sort" nowrap>DeviceID</th>
         <th id="date" class="rptHdrCol_sort" nowrap>Date</th>
         <th id="time" class="rptHdrCol_sort" nowrap>Speed</th>
         <th id="statusdesc" class="rptHdrCol_sort" nowrap>Status</th>
                 <th id="address" class="rptHdrCol_sort" nowrap>Address</th>
</tr>
</thead>
<tbody>

<c:forEach items="${sessionScope.overspeeddetails}" var="overspeeddetailsvar">
<tr class="rptBodyRowOdd">
<td><c:out value="${overspeeddetailsvar.deviceID}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>   
<td><c:out value="${overspeeddetailsvar.speed}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.address}"></c:out></td>

</tr>
<tr class="rptBodyRowEven">
<td><c:out value="${overspeeddetailsvar.deviceID}"> </c:out>  </td>
<td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>   
<td> <c:out value="${overspeeddetailsvar.speed}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.address}"></c:out></td>

</tr>   </c:forEach>    
    </tbody>            


        </table> 


 </body>

  </html>

bean and Dao classes will be like this

public class OverspeedDetails {
private String  deviceID,timestamp,statuscode,address;
private double speed;
public double getSpeed() {
    return speed;
}

public void setSpeed(double speed) {
    this.speed = speed;
}

public String getDeviceID() {
    return deviceID;
}

public void setDeviceID(String deviceID) {
    this.deviceID = deviceID;
}

public String getTIMESTAMP() {
    return timestamp;
}

public void setTIMESTAMP(String TIMESTAMP) {
    this.timestamp = TIMESTAMP;
}

public String getStatuscode() {
    return statuscode;
}

public void setStatuscode(String statuscode) {
    this.statuscode = statuscode;
}

public String getAddress() {
    return address;
}

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

public List<OverspeedDetails> getosDetails(String accountID,String deviceID,String   Timestamp1,String Timestamp2,double speed) {
        Connection con=null;
        List<OverspeedDetails> overspeeddetail = new ArrayList<OverspeedDetails>();
        try{


            con= DBConnectionFactory.getDBConnection();

            String sql="SELECT deviceID,TIMESTAMP,speedKPH,statuscode,address  FROM eventdata WHERE (TIMESTAMP BETWEEN '"+Timestamp1+"' AND '"+Timestamp2+"') AND accountID='"+accountID+"' AND deviceID='"+deviceID+"'and speedKPH >"+speed+";";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(sql);
            System.out.println("Dao over"+rs.next());
            while (rs.next()){
                OverspeedDetails od=new OverspeedDetails();
                od.setDeviceID(rs.getString(1));
                String stringtimestamp=rs.getString(2);
                 long l=Long.parseLong(stringtimestamp);
                 long longtimestamp = l * 1000L;
                 String str = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(longtimestamp));

                    od.setTIMESTAMP(str);
                od.setSpeed(rs.getDouble(3));
                od.setStatuscode(rs.getString(4));
                String add=rs.getString(5);
                String add1[]=add.split(" \\d");
                //String add2=java.util.Arrays.toString(add.split(" "));
                od.setAddress(add1[0]);
                overspeeddetail.add(od);
              } 

            }catch (Exception e) {
                e.printStackTrace();

            }
        System.out.println(overspeeddetail);
            return overspeeddetail;


        }
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
hari
  • 33
  • 2
  • 3
  • 10
  • Why don't you consider going for external plugins like [jQgrid](http://trirand.com/blog/jqgrid/jqgrid.html) or [Data Table](http://datatables.net/) – Vinoth Krishnan Oct 30 '13 at 09:28

1 Answers1

2

You can use displaytag

The display tag library is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model. The library provides a significant amount of functionality while still being easy to use.

You need to download jar from this site displaytag-1.2.jar

Using display tag you can export the data in various formats e.g. Excel, PDF ect.
Column sorting is just on click on the column name link.

There are lot of examples on the site http://displaytag.org

JSP code :

<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

There are two ways to get the list in JSP
1) Using jstl: (Recommended)

Just use the sql:query tag and pass the result to the table tag in this way

    <sql:query var="results">
      select * from table
    </sql:query>

    <display:table name="${results.rows}" />

    (or
    <display:table name="pageScope.results.rows" />
    if not using the EL version)

2) Using dynabeans

<%
  Connection con = ...; // just open a connection

  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT * from table");
  RowSetDynaClass resultSet = new RowSetDynaClass(rs, false);
  stmt.close();
  con.close();
  request.setAttribute("results", resultSet);
%>

  <display:table name="requestScope.results.rows" />  

You can use either way but first JSTL is recommended.

You can use sessionScope

  <display:table id="deviceDetailsID" name="sessionScope.overspeeddetails" pagesize="10" style="width:99%;">

    <display:column property="deviceID" title="Device ID" sortable="true" headerClass="sortable" style="width: 1%"/>
    <display:column property="TIMESTAMP" title="TIMESTAMP" sortable="true" headerClass="sortable" format="{0,date,dd-MM-yyyy}"/>
    <display:column property="speed" title="Speed" sortable="true"/>
    <display:column property="statuscode" title="Status Code"/>
    <display:column property="address" title="Address" sortable="true" headerClass="sortable" />
</display:table>  

To use <sql:query> add JSTL 1.2 jar in classpath and on JSP

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

You need to add Commons Lang 2.6 jar in your class path to avoid this

java.lang.ClassNotFoundException: org.apache.commons.lang.UnhandledException

See also

  1. How to avoid Java Code in JSP-Files?
  2. JSTL sql
  3. Tag query
  4. display tag install guide
  5. displaytag-export-poi 1.2 jar to use export option
  6. displaytag-portlet 1.2 jar
Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • Thanks for reply.I am not understanding can u clearly specify – hari Oct 30 '13 at 09:35
  • I updated my code as per ur code but it is showing error like "The tag handler class for "display:setProperty" (org.displaytag.tags.SetPropertyTag) was not found on the Java Build Path".can u please help – hari Oct 30 '13 at 10:33
  • @hari : Sorry! Basically you don't need this `` just remove. it was needed in my project. See my updated post. Thank you. – Aniket Kulkarni Oct 30 '13 at 10:53
  • It is giving error once again like this java.lang.ClassNotFoundException: org.apache.commons.lang.UnhandledException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) – hari Oct 30 '13 at 11:12
  • @hari What is error? Please tell me. Please post in question. It is hard to read – Aniket Kulkarni Oct 30 '13 at 11:14
  • Oct 30, 2013 4:48:16 PM org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet jsp threw exception java.lang.ClassNotFoundException: org.apache.commons.lang.UnhandledException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) at java.lang.Class.getDeclaredConstructors0(Native Method) – hari Oct 30 '13 at 11:24
  • @hari : Please post error in question. You can edit your question. It is hard to read. – Aniket Kulkarni Oct 30 '13 at 11:51
  • OP has posted subsequent questions [first](http://stackoverflow.com/q/19681168/1031945) and [second](http://stackoverflow.com/q/20263513/1031945) – Aniket Kulkarni Jan 16 '14 at 05:10