Jsp&Servlet入门级项目全程实录第3讲

惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!

1、建立数据表及数据(略)

2、装载驱动,建立数据表
          <link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/default/easyui.css">
          <link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/icon.css">
          <script type="text/javascript" src="jquery-easyui-1.3.3/jquery.min.js"></script>
          <script type="text/javascript" src="jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
          <script type="text/javascript" src="jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

<table id="dg" title="班级信息" class="easyui-datagrid" fitColumns="true" pagination="true" rownumbers="true" fit="true" url="gradeList">
          <thead>
               <tr>
                    <th field="id" width="50" >编号</th>
                    <th field="gradeName" width="100" >班级名称</th>
                    <th field="gradeDesc" width="250" >班级描述</th>
               </tr>
          </thead>
</table>
          field:与数据库名称相同
          fitColumns:自适应    
          pagination:分页      
          rownumbers:行号
          fit:最大化
          url:请求的地址

3、model
     package com.java1234.model;

     public class Grade {
     private int id;
     private String gradeName;
     private String gradeDesc;
    
     public int getId() {
          return id;
     }
     public void setId(int id) {
          this.id = id;
     }
     public String getGradeName() {
          return gradeName;
     }
     public void setGradeName(String gradeName) {
          this.gradeName = gradeName;
     }
     public String getGradeDesc() {
          return gradeDesc;
     }
     public void setGradeDesc(String gradeDesc) {
          this.gradeDesc = gradeDesc;
     }    
     }

4、创建GradeListServlet及配置web.xml、填写url请求
package com.java1234.web;

import java.io.IOException;
import java.sql.Connection;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.java1234.dao.GradeDao;
import com.java1234.model.PageBean;
import com.java1234.util.DbUtil;
import com.java1234.util.JsonUtil;
import com.java1234.util.ResponseUtil;

public class GradeListServlet extends HttpServlet{
   
     @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException {
          //转换doPost
          this.doPost(request, response);
     }

     @Override
     protected void doPost(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException {
         
        
     }     
}

   <servlet>
    <servlet-name>gradeListServlet</servlet-name>
    <servlet-class>com.java1234.web.GradeListServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>gradeListServlet</servlet-name>
    <url-pattern>/gradeList</url-pattern>
  </servlet-mapping>  

<table id="dg" title="班级信息" class="easyui-datagrid" fitColumns="true" pagination="true" rownumbers="true"  url="gradeList">

5、分页
String * from table limie start,size
start起始页
size每页大小

算法
start=(page-1)*rows;

封装page对象
package com.java1234.model;

public class PageBean {

     private int page;//第几页
     private int rows;//每页记录数
     private int start;//起始页
    
     public PageBean(int page, int rows) {
          super();
          this.page = page;
          this.rows = rows;
     }
    
     public int getPage() {
          return page;
     }
     public void setPage(int page) {
          this.page = page;
     }
     public int getRows() {
          return rows;
     }
     public void setRows(int rows) {
          this.rows = rows;
     }
     public int getStart() {
          return (page-1)*rows;
     }
     
}

6、取数据
    public ResultSet gradeList(Connection con,PageBean pageBean)throws Exception{
          StringBuffer sb=new StringBuffer("select * from t_grade");
          if(pageBean!=null){
               sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());              
          }
          PreparedStatement pstmt=con.prepareStatement(sb.toString());
          return pstmt.executeQuery();
     }

7、创建jsonUtil并导入json-lib包(将rs转换成List)
      public static JSONArray formatRsToJsonArray(ResultSet rs)throws Exception{
          ResultSetMetaData md=rs.getMetaData();
          int num=md.getColumnCount();
          JSONArray array=new JSONArray();
          while(rs.next()){
               JSONObject mapOfColValues=new JSONObject();
               for(int i=1;i<=num;i++){
                    mapOfColValues.put(md.getColumnName(i),rs.getObject(i));
               }
               array.add(mapOfColValues);
          }
          return null;
     }
     ResultSetMetaData得到结果集(rs)的结构信息,比如字段数、字段名等。
     int num=md.getColumnCount();取得列数

8、封闭PageBean加构造方法
          //封装
          PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));

public PageBean(int page, int rows) {
          super();
          this.page = page;
          this.rows = rows;
     }

9、获取总记录数
      public int gradeCount(Connection con)throws Exception{
          String sql="select count(*) as total from t_grade";
          PreparedStatement pstmt=con.prepareStatement(sql);
          ResultSet rs=pstmt.executeQuery();
          if(rs.next()){
               return rs.getInt("total");
          }else{
               return 0;
          }
     }

10、封装向页面输出数据
    public static void write(HttpServletResponse response,JSONObject jsonObject)throws Exception{
          response.setContentType("text/html;charset=utf-8");
          PrintWriter out=response.getWriter();
          out.println(jsonObject.toString());
          out.flush();
          out.close();
     }

 

 

你可能感兴趣的:(servlet)