大家好我是寻觅:
        最近过年,不知道大家多得如何,硬件搞少了点,书看多了些,时间也就多了些,写了些东西,就发上来,同大家一起探讨。
下面是一个servlet+bean分页的程序。
为什么第一个是servlet?
         首先,必须清楚的是,不管是web还是桌面系统其原理都是非常之相近的。你可以看看数据库连接,其实代码是一样的,只是在数据源上可能有所不同,WEB平台上可以将数据管理委托我们的webserver替我们管理,当然在app程序中你也可以写一个数据管理的独立组件。同理,web平台上的分页,其实和APP十分之相似。目前分页有两种方法:第一,使用sql查询部分的数据,如在SQL语句中使用limit / between...and...;第二,通过控制ResultSet指针实现。二者比较,前者相对简单,但从节省资源和速度没有后者优秀。
         其次,抛开一切的框架最快的web程序,莫过于servlet。
         本人实在不喜欢说这些,感觉这些东西,代码写多了,自然会知道,所以,你会发现我的文章里很少写这些废话,偶尔写多了些,居然发现点击多了不少,呵呵,看来大家很少看书和写代码。这也是我喜欢发资源型文章的原因之一。希望大家,在新的一年,拥有新气象,多看书,多写代码,尽情感受学习和写代码的乐趣。 运行结果:
运行结果:
访问路径
http://127.0.0.1:8080/webTest/
ServletTest.lusm
控制台:
########只查询一次数据库哦#########
web界面:
 
       
      代码:
代码:
web.xml
 <?xml version="1.0" encoding="gbk"?>
<?xml version="1.0" encoding="gbk"?>
 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <servlet>
    <servlet>
 <servlet-name>ServletTest</servlet-name>
        <servlet-name>ServletTest</servlet-name>
 <servlet-class>lusm.servlet.ServletTest</servlet-class>
        <servlet-class>lusm.servlet.ServletTest</servlet-class>
 <init-param>
        <init-param>
 <param-name>dbname</param-name>
            <param-name>dbname</param-name>
 <param-value>myTest</param-value>
            <param-value>myTest</param-value>
 </init-param>
        </init-param>
 <init-param>
        <init-param>
 <param-name>table</param-name>
            <param-name>table</param-name>
 <param-value>userinfo</param-value>
            <param-value>userinfo</param-value>
 </init-param>
        </init-param>
 <init-param>
        <init-param>
 <param-name>dba</param-name>
            <param-name>dba</param-name>
 <param-value>root</param-value>
            <param-value>root</param-value>
 </init-param>
        </init-param>
 <init-param>
        <init-param>
 <param-name>passwd</param-name>
            <param-name>passwd</param-name>
 <param-value>password</param-value>
            <param-value>password</param-value>
 </init-param>
        </init-param>
 </servlet>
    </servlet>
 
    
 <servlet-mapping>
    <servlet-mapping>
 <servlet-name>ServletTest</servlet-name>
        <servlet-name>ServletTest</servlet-name>
 <url-pattern>/ServletTest.lusm</url-pattern>
        <url-pattern>/ServletTest.lusm</url-pattern>
 </servlet-mapping>
    </servlet-mapping>
 </web-app>
</web-app>
ServletTest.java
 package lusm.servlet;
package lusm.servlet;

 import java.io.IOException;
import java.io.IOException;
 import java.io.PrintWriter;
import java.io.PrintWriter;
 import java.sql.ResultSet;
import java.sql.ResultSet;
 import java.sql.SQLException;
import java.sql.SQLException;

 import javax.servlet.ServletConfig;
import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;

 import lusm.db.inte.DbInf;
import lusm.db.inte.DbInf;
 import lusm.db.inte.ViewInf;
import lusm.db.inte.ViewInf;
 import lusm.db.oa.Db;
import lusm.db.oa.Db;
 import lusm.db.oa.View;
import lusm.db.oa.View;

 //前台显示
//前台显示

 public class ServletTest extends HttpServlet
public class ServletTest extends HttpServlet  {
{
 private static final long serialVersionUID = 5435345395438959L;
    private static final long serialVersionUID = 5435345395438959L;
 private String dbname;
    private String dbname;
 private String table;
    private String table;
 private String dba;
    private String dba;
 private String passwd;
    private String passwd;
 private String sql;
    private String sql;
 
    
 private DbInf di = null;
    private DbInf di = null;
 private ViewInf v = null;
    private ViewInf v = null;
 private ResultSet rs = null;
    private ResultSet rs = null;


 public ServletTest()
    public ServletTest()  {
{
 super();
        super();
 }
    }


 public void destroy()
    public void destroy()  {
{
 super.destroy();
        super.destroy();
 }
    }
 
    

 public void init(ServletConfig config) throws ServletException
    public void init(ServletConfig config) throws ServletException {
{
 
        
 //**********************用户输入*******************************
        //**********************用户输入*******************************
 dbname =  config.getInitParameter("dbname");
        dbname =  config.getInitParameter("dbname");
 table  =  config.getInitParameter("table");
        table  =  config.getInitParameter("table");
 dba    =  config.getInitParameter("dba");
        dba    =  config.getInitParameter("dba");
 passwd =  config.getInitParameter("passwd");
        passwd =  config.getInitParameter("passwd");
 sql = "select * from "+table;
        sql = "select * from "+table;
 
        
 di = new Db();
        di = new Db();
 v = new View();
        v = new View();

 try
        try  {
{
 rs = di.init(dbname,dba,passwd,sql).getRs();
            rs = di.init(dbname,dba,passwd,sql).getRs();

 } catch (SQLException e)
        } catch (SQLException e)  {
{
 e.printStackTrace();
            e.printStackTrace();
 System.out.println("查询错误");
            System.out.println("查询错误");
 }
        }
 //**********************结束输入********************************
        //**********************结束输入********************************
 }
    }
 
    
 public void doGet(HttpServletRequest request, HttpServletResponse response)
    public void doGet(HttpServletRequest request, HttpServletResponse response)

 throws ServletException, IOException
            throws ServletException, IOException  {
{
 response.setContentType("text/html;charset=gbk");
        response.setContentType("text/html;charset=gbk");
 response.setCharacterEncoding("GBK");
        response.setCharacterEncoding("GBK");
 PrintWriter out = response.getWriter();
        PrintWriter out = response.getWriter();

 //**********************用户输入*******************************
        //**********************用户输入*******************************
 //goto是分页的参数
        //goto是分页的参数 
 int go ;
        int go ;

 if(request.getParameter("goto") == null)
        if(request.getParameter("goto") == null) {
{
 go = 1;
            go = 1;
 }
        }
 else
        else

 
         {
{                                                                   
 go = Integer.parseInt(request.getParameter("goto"));
            go = Integer.parseInt(request.getParameter("goto"));
 }
        }

 try
        try  {
{
 v.setView(rs, "table",go,5);//这里用于测试,实际中使用须避免像5这样的魔数
            v.setView(rs, "table",go,5);//这里用于测试,实际中使用须避免像5这样的魔数
 out.println(v.getView());
            out.println(v.getView());

 } catch (Exception e)
        } catch (Exception e)  {
{
 e.printStackTrace();
            e.printStackTrace();
 out.println("打印失败");
            out.println("打印失败");
 }
        }
 //**********************结束输入********************************
        //**********************结束输入********************************
 
        
 out.flush();
        out.flush();
 out.close();
        out.close();
 }
    }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
    public void doPost(HttpServletRequest request, HttpServletResponse response)

 throws ServletException, IOException
            throws ServletException, IOException  {
{
 this.doGet(request, response);
        this.doGet(request, response);
 }
    }
 }
}                                              
ViewInf.java接口
 package lusm.db.inte;
package lusm.db.inte;

 import java.sql.ResultSet;
import java.sql.ResultSet;
 import java.sql.SQLException;
import java.sql.SQLException;


 public interface ViewInf
public interface ViewInf  {
{
 String ERROR = "错误";
    String ERROR = "错误"; 
 String NOSTYLE ="对不起! 没有该样式";
    String NOSTYLE ="对不起! 没有该样式";
 
    
 public void  setView(ResultSet rs,String style,int go,int pagesize) throws SQLException ;
    public void  setView(ResultSet rs,String style,int go,int pagesize) throws SQLException ;
 
    
 public String getView();
    public String getView();
 
    
 public int getRow(ResultSet rs)throws SQLException;
    public int getRow(ResultSet rs)throws SQLException;
 
    
 }
}
 View.java
 package lusm.db.oa;
package lusm.db.oa;
 
                                       
 import java.sql.ResultSet;
import java.sql.ResultSet;
 import java.sql.SQLException;
import java.sql.SQLException;

 //分页代码
//分页代码

 public class View implements lusm.db.inte.ViewInf
public class View implements lusm.db.inte.ViewInf  {
{
 private String view ;
    private String view ;
 
    

 public String getView()
      public String getView()  {
{
 return view;
        return view;
 }
    }


 public void setView(ResultSet rs,String style,int go,int pagesize) throws SQLException
    public void setView(ResultSet rs,String style,int go,int pagesize) throws SQLException  {
{
 
         

 if(style.equals("table"))
        if(style.equals("table")) {
{
 this.view = "<table border='2' bgcolor='#ff8000' width='240px' align='center'>";
           this.view = "<table border='2' bgcolor='#ff8000' width='240px' align='center'>";
 int i = 0;
          int i = 0;

 /**//*
          /**//*
 * 注意:我们不能用
           * 注意:我们不能用
 * rs.rs.absolute(0);
           * rs.rs.absolute(0);
 * 定位到第一条数据,这里我使用
           * 定位到第一条数据,这里我使用 
 * rs.first();
           * rs.first();
 * rs.previous();
           * rs.previous();
 * 解决了这个问题
           * 解决了这个问题
 * */
           * */
 if(go != 1)
          if(go != 1)
 rs.absolute(go-1);
             rs.absolute(go-1);

 else
          else {
{
 rs.first();
              rs.first();
 rs.previous();
              rs.previous();
 }
          }

 while (rs.next())
          while (rs.next())  {
{ 

 if(++i <= pagesize)
              if(++i <= pagesize) {
{
 this.view= this.view +"<tr>";
                   this.view= this.view +"<tr>";
 this.view= this.view +"<td>"+rs.getString(1) + "</td>" +
                   this.view= this.view +"<td>"+rs.getString(1) + "</td>" +
 "<td>" + rs.getString(2)+"</td>";
                                            "<td>" + rs.getString(2)+"</td>";
 this.view= this.view +"</tr>";
                   this.view= this.view +"</tr>";

 }else
               }else {
{
 break;
                   break;
 }
               }
 }
          }
 //最后一页
          //最后一页

 if((go+pagesize) > this.getRow(rs))
          if((go+pagesize) > this.getRow(rs)) {
{
 this.view = this.view +"<tr><td><a href ='/webTest/ServletTest.lusm?goto="+(go-pagesize)+"'>上一页</a></td><td>末 页</td></tr>";
              this.view = this.view +"<tr><td><a href ='/webTest/ServletTest.lusm?goto="+(go-pagesize)+"'>上一页</a></td><td>末 页</td></tr>";
 }//第一页
          }//第一页

 else if(go == 1)
          else if(go == 1) {
{
 this.view = this.view +"<tr><td>首 页</td><td><a href ='/webTest/ServletTest.lusm?goto="+(go+pagesize)+"'>下一页</a></td></tr>";
              this.view = this.view +"<tr><td>首 页</td><td><a href ='/webTest/ServletTest.lusm?goto="+(go+pagesize)+"'>下一页</a></td></tr>";
 }
          }

 else
          else {
{
 this.view = this.view +"<tr><td><a href ='/webTest/ServletTest.lusm?goto="+(go-pagesize)+"'>上一页</a></td><td><a href ='/webTest/ServletTest.lusm?goto="+(go+pagesize)+"'>下一页</a></td></tr>";
              this.view = this.view +"<tr><td><a href ='/webTest/ServletTest.lusm?goto="+(go-pagesize)+"'>上一页</a></td><td><a href ='/webTest/ServletTest.lusm?goto="+(go+pagesize)+"'>下一页</a></td></tr>";
 }
          }
 this.view= this.view +"</table>";
          this.view= this.view +"</table>";

 }else
        }else {
{                                               
 this.view = NOSTYLE;
         this.view = NOSTYLE;
 }
        }
 }
    }
 
    

 public int getRow(ResultSet rs) throws SQLException
    public int getRow(ResultSet rs) throws SQLException  {
{
 ResultSet rsr = rs;
        ResultSet rsr = rs;
 rsr.last();
        rsr.last();
 return rsr.getRow();
        return rsr.getRow();
 }
    }
 
    
 }
} 
DbInf.java接口
 package lusm.db.inte;
package lusm.db.inte;

 import java.sql.ResultSet;
import java.sql.ResultSet;
 import java.sql.SQLException;
import java.sql.SQLException;

 import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Connection;

 import lusm.db.oa.Db;
import lusm.db.oa.Db;


 public interface DbInf
public interface DbInf  {
{
 public Db init(String dbname,String dba,String passwd, String sql);
    public Db init(String dbname,String dba,String passwd, String sql);
 
    
 public Connection getConn() ;
    public Connection getConn() ;
 
    
 public void setConn(String dbname,String dba,String passwd) throws Exception;
    public void setConn(String dbname,String dba,String passwd) throws Exception;

 public ResultSet getRs() throws SQLException ;
    public ResultSet getRs() throws SQLException ;

 public void setRs(String sql) throws SQLException ;
    public void setRs(String sql) throws SQLException ;
 
    
 }
}
Db.java
 package lusm.db.oa;
package lusm.db.oa;

 import java.sql.DriverManager;
import java.sql.DriverManager;
 import java.sql.ResultSet;
import java.sql.ResultSet;
 import java.sql.SQLException;
import java.sql.SQLException;

 import lusm.db.inte.DbInf;
import lusm.db.inte.DbInf;

 import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Connection;

 //数据库
//数据库

 public class Db implements DbInf
public class Db implements DbInf  {
{
 private Connection conn;
    private Connection conn;
 private ResultSet rs;
    private ResultSet rs;


 public Db init(String dbname,String dba,String passwd, String sql)
    public Db init(String dbname,String dba,String passwd, String sql) {
{

 try
        try  {
{
 this.setConn(dbname, dba, passwd);
            this.setConn(dbname, dba, passwd);
 System.out.println("########只查询一次数据库哦#########");
            System.out.println("########只查询一次数据库哦#########");
 this.setRs(sql);
            this.setRs(sql);

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


 public Connection getConn()
    public Connection getConn()  {
{
 return conn;
        return conn;
 }
    }
 
    

 public void setConn(String dbname,String dba,String passwd) throws Exception
    public void setConn(String dbname,String dba,String passwd) throws Exception  {
{
 Class.forName("com.mysql.jdbc.Driver").newInstance();
        Class.forName("com.mysql.jdbc.Driver").newInstance();
 this.conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost" +
        this.conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost" +
 ":3306/"+dbname+"?user="+dba+"&password="+passwd+"&useUnicode=true&characterEncoding=GBK");
            ":3306/"+dbname+"?user="+dba+"&password="+passwd+"&useUnicode=true&characterEncoding=GBK");
 }
    }


 public ResultSet getRs() throws SQLException
    public ResultSet getRs() throws SQLException  {
{
 return rs;
        return rs;
 }
    }


 public void setRs(String sql) throws SQLException
    public void setRs(String sql) throws SQLException  {
{
 this.rs = this.getConn().createStatement().executeQuery(sql);
        this.rs = this.getConn().createStatement().executeQuery(sql);
 }
    }

 }
}
 
 
	posted on 2008-02-08 00:39 
小寻 阅读(3289) 
评论(5)  编辑  收藏  所属分类: 
j2se/j2ee/j2me