Implement a paging display of the retrieved data in a JSP page

Source: Internet
Author: User
Tags empty http post implement sql query stmt table name trim
js| Pagination | data | show | page


In a page, when there is a lot of data to retrieve, it is often necessary to display the data in pagination and to make the paging.

Here are some routines to illustrate the implementation of JSP page paging technology.

First, in the JSP, the Java servlet is used to retrieve the data, and the JSP to invoke the results to display.

As a result, this technique can be divided into two parts (dependencies):

1. Implementation in the server-side servlet

Points:

& Save Query condition to session, take query condition in session

& Set the MaxRows of the Statement object (determine how many rows of data is displayed on a page)

& sequentially queries the data by executing the SQL statement, pressing MaxRows to retrieve a maxrows data,

The next page retrieves the next maxrows data, and so on.

2. In the JSP display implementation

Points:

& Display MaxRows Data

& Invoke the next maxrows data from the servlet query again via the "next page" button or hyperlink

The process is as shown in the figure:

The following is a description (a servlet program and a JSP program) with an example:

L querymedicine Java (medicine querymedicine)

Parcel medicine;

Import javax.servlet.*;

Import javax.servlet.http.*;

Import java.io.*;

Import java.util.*;

Import java.sql.*;

Import medicine.medicinelist;

Import Medicine.searchdata;

////////////////////////////////////////////////////

Pick Up---mutiquery.jsp page and get 5 query criteria from Request///

Inquire the medicine according to the condition, and deposit the result into the "medicinelist" of the session. ///

Zhang Hu March 15, 2000///

////////////////////////////////////////////////////

The public class Querymedicine enlarged HttpServlet {

a private Dbconnectionmanager connmgr;

Initializing global variables

The Public empty init (ServletConfig setting) throws Servletexception {

Super.init (config);

Connmgr = Dbconnectionmanager.getinstance ();

}

======================== processing HTTP GET requests ============================

Public empty doget (HttpServletRequest request, httpservletresponse reaction)
Throw Servletexception, IOException {

Stating Stmt=null;

Connection against = Connmgr.getconnection ("medicine");

if (empty against = =) {

Response.sendredirect ("/medicine/con_error.html");

Return

}

==================== CREATE DATABASE Statement =============================

Give it a try {

Stmt=con.createstatement ();

}

catch (Exception e)

{

Connmgr.freeconnection ("medicine", con);

Response.sendredirect ("/medicine/stmt_error.html");

Return

}

//-------------------------------------------------------------

Long all_count=0;//The total number of medicines that meet the condition

=================== get session====================== from request

Give it a try {

HttpSession meeting = Request.getsession (false);

if (empty meeting = =) {

Connmgr.freeconnection ("medicine", con);

Response.sendredirect ("/medicine/session_error.html");

Return

}

//-----------------------------------------------------------

String Ll_pos=request.getparameter ("pos");
If Pos=-1 says to start from scratch,

//----------------------------------------------------------

SQL statement string for ===================== query ==========================

Rope sqlstatment=& quot; Select MEDICINES.YPBM, MEDSMALLTYPES.ZLMC, MEDSUPERTYPES.CLMC,
MEDTYPES.DLMC, MEDICINES.YPM, Medicines.zyyx, medicines.ypzy from Medicine,
Medsmalltypes, Medsupertypes, where's medtypes medicines.ypbm> "+ll_pos+"
and ((MEDSMALLTYPES.ZLBM = MEDICINES.ZLBM) as well as (MEDSUPERTYPES.CLBM = MEDICINES.CLBM)
and (MEDTYPES.DLBM = MEDICINES.DLBM) as well as (Medicines.del_flag =0) & quot;;

//-----------------------------------------------------

Rope ZLBM;

Rope DLBM;

Rope CLBM;

Rope Zyyx;

Rope YPM;

Searchdata slist= new Searchdata ();

Sync (meeting) {

Slist= (Searchdata) session.getattribute ("SearchList");

}

if (Ll_pos.equals ("-1")) {

= = = = = Not from "Next page" to look at the data from the beginning ==========

if (slist! =null) {

Sync (meeting) {

Session.removeattribute ("SearchList");

}

}

slist= new Searchdata ();

============= the parameters of the query condition from the setting of the query condition page ===================

Zlbm=request.getparameter ("ZLBM");

Dlbm=request.getparameter ("DLBM");

Clbm=request.getparameter ("CLBM");

Zyyx=request.getparameter ("Zyyx");

Ypm=request.getparameter ("YPM");

//------------------------------------------------------

================= the query criteria parameter to the session by Vector ==========

SLIST.SETZLBM (ZLBM);

SLIST.SETDLBM (DLBM);

SLIST.SETCLBM (CLBM);

Slist.setzyyx (ZYYX);

SLIST.SETYPM (YPM);

Sync (meeting) {

Session.setattribute (& quot; searchlist & quot;, slist);

}

//---------------------------------------------------------

}

//--------------------------------------------------------

Other than that

{

============================ fetch Query Condition parameter ====================

if (slist! =null) {

ZLBM=SLIST.GETZLBM ();

DLBM=SLIST.GETDLBM ();

CLBM=SLIST.GETCLBM ();

Zyyx=slist.getzyyx ();

YPM=SLIST.GETYPM ();

}

Another

if (stmt! =null) stmt.close ();

Stmt.setmaxrows (0);

Connmgr.freeconnection ("medicine", con);

Response.sendredirect ("/medicine/session_error.html");

Return

}

//-------------------------------------------------

}

Rope sql2=& quot; Select number (*) from where the medicine del_flag=0& quot;;

===================== sets the SQL statement based on the condition parameter =======================

if (!) ( Zlbm.trim (). Equals ("0"))) {

sqlstatment+= (& quot) and (medicines.zlbm= ' "+zlbm.trim () +" ") & quot;);

sql2+= (& quot) and zlbm= ' "+zlbm.trim () +" "");

}

if (!) ( Dlbm.trim (). Equals ("0"))) {

sqlstatment+= (& quot) and (medicines.dlbm= ' "+dlbm.trim () +" ") & quot;);

sql2+= (& quot) and dlbm= ' "+dlbm.trim () +" "");

}

if (!) ( Clbm.trim (). Equals ("0"))) {

sqlstatment+= (& quot) and (medicines.clbm= ' "+clbm.trim () +" ") & quot;);

sql2+= (& quot) and clbm= ' "+clbm.trim () +" "");

}

if (!) ( Zyyx.trim (). Length () ==0)) {

sqlstatment+= (& quot) and (like '% "+zyyx.trim () +"% "medicines.zyyx) & quot;);

sql2+= (like '% "+zyyx.trim () +"% "); the same & quot; Zyyx

}

if (!) ( Ypm.trim (). Length () ==0)) {

sqlstatment+= (& quot) and (like '% "+ypm.trim () +"% "medicines.ypm) & quot;);

sql2+= (like '% "+ypm.trim () +"% "); the same & quot; ypm

}

sqlstatment+=& quot;)) by medicines.ypbm ASC & quot;; The command

//-------------------------------------------------------------

Vector list = new vector ();

================= sets the number of data bars displayed on one page (the number of data bars retrieved at a time) ===========

Stmt.setmaxrows (25);

//----------------------------------------------------------------------

=================== execute the query to put the results in resultset ================

ResultSet rs = stmt.executequery (sqlstatment);

ResultSet rs2= stmt.executequery (SQL2);

//------------------------------------------------

if (rs==null) {//If no query result data

if (stmt! =null) stmt.close ();

Stmt.setmaxrows (0);

Connmgr.freeconnection ("medicine", con);

Response.sendredirect ("/medicine/no_medicine.html");//directed to a page

Return

}

==================== the drug information into the data object and depositing it in the vector ================

if (Rs2.next ()) {

All_count=rs2.getlong (1);//Get total number of strips

}

if (rs2!=null) rs2.close ();

while (Rs.next ()) {

Medicinelist m = new Medicinelist ();

M.SETYPBM (Rs.getint ("YPBM"));

M.SETZLMC (rs.getstring ("ZLMC"));

M.SETCLMC (rs.getstring ("CLMC"));

M.SETDLMC (rs.getstring ("DLMC"));

M.SETYPM (rs.getstring ("YPM"));

M.setzyyx (rs.getstring ("Zyyx"));

M.setypzy (rs.getstring ("Ypzy"));

M.SETCLBM (CLBM);

M.SETDLBM (DLBM);

M.SETZLBM (ZLBM);

List.addelement (m);

}

//----------------------------------------------------------

if (rs!=null) rs.close ();

Stmt.setmaxrows (0);

if (stmt!=null) stmt.close ();

Connmgr.freeconnection ("medicine", con);

======================== Deposit in Session ===========================

Sync (meeting) {

Session.setattribute ("medicinelist", list);

}

//-----------------------------------------------

Seize (SQLException e) {

Connmgr.freeconnection ("medicine", con);

Response.sendredirect ("/medicine/sql_error.html");

return;}

======================= Redirect to a JSP page ==========================

String Url= "/medicine/querymedicine.jsp?all_count=" +all_count;

ServletContext sc = Getservletcontext ();

RequestDispatcher rd = sc.getrequestdispatcher (URL);

Rd.forward (request, response);

//-------------------------------------------------------

}

Public empty break () {

=================== terminates the database connection when the servlet exits, canceling the statement object

if (connmgr! =null) connmgr.release ();

//------------------------------------------------------------------------

}

======================== processing HTTP POST requests ============================

Public empty doPost (HttpServletRequest request, httpservletresponse reaction)
Throw Servletexception, IOException {

Doget (request, reaction);

}

========================== Get Servlet Information ============================

Public Rope Getservletinfo () {

Back "medicine.querymedicine information & quot;;

}

}

L querymedicine.jsp

<%@ page Language =& quot; Java & quot;session=& quot; true & quot; Import= "java.util.*,
Medicine.method, medicine.medicinelist "contenttype=" TEXT/HTML;CHARSET=GBK "%>

< handwriting language =& quot; JavaScript & quot;>

Feature Next () {

var Ls_pos=document.form2.maxpos.value;

document.location= "/servlet/medicine.querymedicine?pos=" +LS_POS;

}

Feature Details (YPBM) {

document.location= "/servlet/medicine.detail?ypbm=" +YPBM;

}

</script>

<!--add Head-->

<div align= "center" >

<body topmargin= "0" leftmargin= "rightmargin=" bgcolor= "#FFFFFF" >
<div align= "center" >

< Center >< iframe width =760 height =130 noresize scrolling= No frameborder=0
marginheight=0marginwidth=0src=& quot; \medicinetitle.html "></iframe>

</div>

<!--complete-->

<title> Products List </title>

<%

Method MD = new method ();

Vector sklist;

Sync (meeting) {

Sklist = (vector) session.getattribute ("Medicinelist");

}

%>

<!-----------------------------display header-------------------------------->

<%

if (sklist = = Empty or sklist.size () <=0)

{

Response.sendredirect ("/medicine/no_medicine.html");

Return

}

Other than that

{%>

<center> Hello! The following is a list of items, together with <font color= #ff0000 >
<%=request.getparameter ("All_count")%></font> drug information in accordance with the conditions

</center>

<div align= "center" >

< Center >

< table border =& quot;1& quot;cellpadding=& quot;0& quot;cellspacing=& quot;0&; width quot =& ;840& quot;
bordercolorlight= "#FFFFFF" bordercolordark= "#000000" >

<tr bgcolor= "#339933" >

<TD width=42 align= "center" ><font color= "#339933" ><b> details </b></font></td>

<TD width=160 align= "center" ><font color= "#FFFFFF" ><b> Product name </b></font></td>

<TD width=52 align= "center" ><font color= "#FFFFFF" ><b> Super class </b></font></td>

<TD width=145 align= "center" ><font color= "#FFFFFF" ><b> Big class </b></font></td>

<TD width=145 align= "center" ><font color= "#FFFFFF" ><b> subclass </b></font></td>

<TD width=270 align= "center" ><font color= "#FFFFFF" ><b> description </b></font></td>

</tr>

<!---------------------------------------------------------------------------------------------------------- --------->

<%

Medicinelist d= new Medicinelist ();

Suitable for (int index = 0; index < sklist.size (); index++)

{

D = (medicinelist) sklist.elementat (index);

Rope Lshref;

if (D.getypzy () ==null or D.getypzy (). Equals ("http://") or D.getypzy (). Equals ("")

{

Lshref= "";

Another

lshref=& quot;< href= "+d.getypzy () +" > ";

}

Out.println ("<form name = table 1 method=post action=/servlet/medicine.detail?ypbm=" +D.GETYPBM () + ">");

Out.println ("<tr bgcolor= #F3F3E9 >< td align=center >< font color= #000000大小 =2>
<input type=submit name=submit1 value= detail ></FONT></TD><TD Align=center><font color=# 000000 size=2> ");

Out.println (LSHREF+D.GETYPM () + "</a></font></td><td align=center >< font color= #000000大小 =2 >& quot;);

Out.println (D.GETCLMC () + "</font></td><td align=center >< font color= #000000大小 =2>& quot;
+D.GETDLMC () + "</font></td><td align=center >< font color= #000000大小 =2>& quot;);

Out.println (D.GETZLMC () + "</font></td><td align=center >< font color= #000000大小 =2>& quot;
+md.notnull (D.getzyyx ()) + "</font></td></tr></form>");

Out.flush ();

}

%>

< table name = Table 2id= Table 2>

< input type =hidden named =maxpos id=maxpos value=<%=d.getypbm ()%>>

</Forms >

<%

}

%>

</table>

<p></p>

<%

String Ssll=request.getparameter ("All_count");

Long Long temp= new (SSLL);

Long cou= temp.longvalue ();

if (Sklist.size () ==25&&cou>25)

{%>

<input Type=button value= "Next page" >

<%}%>

<%

Sync (meeting) {

if (Session.getattribute ("Medicinelist")!=null) {

Session.removevalue ("Medicinelist");

}

}

%>

</center>

<p align= "center" ><input type= "button" value= "return" name= "B3" >

<br><br><br>

<!--add Bottom-->

<div align= "center" >

< Center >< iframe width =760 height =140 noresize scrolling= No frameborder=0
marginheight=0marginwidth=0src=& quot; \bottom.html "></iframe>

</div>

<!--complete-->




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.