Self-written pages

Source: Internet
Author: User

JSP code:


<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">

<Title> page </title>
 
<SCRIPT type = "text/JavaScript">
Function Sub (){
VaR size = Document. getelementbyid ("XX"). value;
Window. Location = "<% = PATH %>/userservlet? Size = "+ size;
}
Function (){
VaR P = Document. getelementbyid ("toindex"). value;
Window. Location = "<% = PATH %>/userservlet? P = "+ P;
}
</SCRIPT>
</Head>

<Body>
<A href = "userservlet"> display by page </a>
<Center>
<Table border = "1" cellpadding = "10" cellspacing = "0">
<Tr> <TD> id </TD> <TD> name </TD> </tr>
<C: foreach items = "$ {page. pagedate}" Var = "PG">
<Tr> <TD >$ {pg. ID} </TD> <TD >$ {pg. Username} </TD> </tr>
</C: foreach>
</Table>
$ {Page. pagecount} pages & nbsp; Page $ {page. pagenow} & nbsp; $ {page. pagesize} records are displayed on each page.
<C: If test = "$ {page. pagenow ne 1}">
<A href = "userservlet? P = $ {page. pageNow-1} "> previous page </a>
</C: If>
<C: If test = "$ {page. pagenow ne page. pagecount}">
<A href = "userservlet? P =$ {page. pagenow + 1} "> next page </a>
</C: If>
<Input type = "text" size = "3" id = "XX"/> entries per page
<Input type = "button" value = "go" onclick = "sub ()"/>
Go to the <input type = text id = "toindex" size = 2/> page <input type = "button" value = "go" onclick = "to ()"/>
</Center>
</Body>
</Html>

Servlet code:

Response. setcontenttype ("text/html; charset = GBK ");
Userservice US = new userservice ();
// Current page
String page = request. getparameter ("p ");
If (page = NULL ){
Page = "1 ";
}
Int P = integer. parseint (PAGE );
// The number of records per page
String SZ = request. getparameter ("size ");
If (SZ = NULL ){
SZ = "10 ";
}
Int size = integer. parseint (sz );

Page pages = us. findall (p, size );
Request. setattribute ("page", pages );
Request. getrequestdispatcher ("/index. jsp"). Forward (request, response );

Service Code:

Public page findall (INT pagenow, int size ){
Page page = new page ();
Userdao = new userdao ();

Int begin = (pageNow-1) * size;
Int end = begin + size;
Int COUNT = userdao. pagecount ();
Int pagecount = count % size = 0? Count/size: Count/size + 1;

Page. setpagenow (pagenow );
Page. setpagecount (pagecount );
Page. setpagesize (size );
Page. setpagedate (userdao. findall (begin, end ));
Return page;
}

Dao (JDBC) code:

Public list findall (INT begin, int end ){
List list = new arraylist ();
Conn = dbutil. getconnection ();
Try {
Pst = conn. preparestatement ("select * from (select ID, username, rownum R from userinfo) Where R>? And r <=? ");
Pst. setint (1, begin );
Pst. setint (2, end );
Rs = pst.exe cutequery ();
While (Rs. Next ()){
User user = new user ();
User. setid (Rs. getint ("ID "));
User. setusername (Rs. getstring ("username "));
List. Add (User );
}
} Catch (sqlexception e ){
E. printstacktrace ();
} Finally {
Dbutil. getclose (RS );
Dbutil. getclose (PST );
Dbutil. getclose (conn );
}
Return list;
}
 
Public int pagecount (){
Int I = 0;
Conn = dbutil. getconnection ();
Try {
St = conn. createstatement ();
Rs = st.exe cutequery ("select count (*) from userinfo ");
If (Rs. Next ()){
I = Rs. getint (1 );
}
} Catch (sqlexception e ){
E. printstacktrace ();
} Finally {
Dbutil. getclose (RS );
Dbutil. getclose (PST );
Dbutil. getclose (conn );
}
Return I;
}

Encapsulation class Page code:

Private int pagenow; // current page
Private int pagesize; // number of entries per page
Private int pagecount; // the total number of pages.
Private list pagedate; // data

You can use the constructor to initialize

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.