动态网页技术--JSP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态网页技术--JSP相关的知识,希望对你有一定的参考价值。

分页查询

1.创建web工程

2.连接数据库引入jar包 classes12.jar

                                ojdbc14.jar

   放入lib中

3.指定一个jsp页面中的一个链接,跳转到一个Servlet上

4.创建一个Servlet,勾选上doget,dopost方法,Next后,Servlet/JSP Mapping URL:要从根目录跳转,删去前面一部分,留下/PageServlet

5.web.xml中配置会自动生成

6.因为要跳转到servlet上,所以index.jsp中a标签href路径是PageServlet

7.链接默认请求方式是get,只有表单请求方式是post,所以PageServlet在doget中写,dopost中流拼的网页都删了,请求方式改为this.doGet(request,response);

8.doget中调整字符集

         request.setCharacterEncoding("utf-8");
         response.setContentType("text/html;charset=utf-8");

9.引入DB类,负责连接关闭数据库(DB中用static修饰,背的类直接引用,不用实例化了)

10.doget中接着写调用JDBC连接数据库 ResultSet  rs=DB.getCon().createStatement().executeQuery(sql);  (执行完sql返回一个ResultSet值)

      分页查询sql语句:

            int cpage=1;

            String sql="select * from " +
                            "(select t.*, rownum r from " +
                            "(select * from t_client order by client_no) t)" +
                            " where r between "+(cpage*3-2)+" and "+(cpage*3)+"";

11.循环rs,封装成对象,放入List集合中

   创建实体类Client,提供set,get,构造方法

   声明集合 List<Client>  list=new ArrayList<Client>();

  循环rs取值:

while(rs.next()){
String clientNo=rs.getString("client_no");
String clientName=rs.getString("client_name");
String sex=rs.getString("sex");
String birthday=rs.getString("birthday");
String certificateNO=rs.getString("CERTIFICATE_NO");
String telNO=rs.getString("tel_no");
String clientType=rs.getString("client_type");
String cardNo=rs.getString("card_no");

每一条记录封装成一个对象

Client  client=new Client(clientNo, clientName, sex, birthday, certificateNO, telNO, clientType, cardNo);

循环出来的对象存入集合中

list.add(client);

12.list存入作用域中

request.setAttribute("list", list);

13.转发跳转页面

request.getRequestDispatcher("b.jsp").forward(request, response);

14.创建b.jsp显示页面,在这里取List

     ${requestScope.list }

   循环取出

<c:forEach items="${requestScope.list }" var="client">
<tr>
<td>
<c:out value="${client.clientNo}"></c:out>
</td>
<td>
<c:out value="${client.clientName}"></c:out>
</td>
<td>
<c:out value="${client.sex}"></c:out>
</td>
</tr>
</c:forEach>

15.b.jsp上点下一页按钮再跳回PageServlet上

<a href="PageServlet?cpage=${requestScope.cpage+1 }">下一页</a>

16.PageServlet上接收,再跳到b.jsp上

if(request.getParameter("cpage")!=null){
cpage=Integer.parseInt(request.getParameter("cpage"));
}       // 因为前面声明首页数  int cpage=1;所以进行判断,不然页码数永远是1

 设置最新的cpage页码数:   request.setAttribute("cpage", Integer.valueOf(cpage)); 

 

 

技术分享

主页index.jsp

 1 index.jsp主页
 2 
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8 
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11   <head>
12     <base href="<%=basePath%>">
13     
14     <title>My JSP ‘index.jsp‘ starting page</title>
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23   </head>
24   
25   <body>
26       <a  href="PageServlet">分页查询</a>
27   </body>
28 </html>

引入DB类连接关闭数据库

 1 DB
 2 
 3 package com.neusoft.db;
 4 
 5 import java.sql.Connection;
 6 import java.sql.DriverManager;
 7 import java.sql.SQLException;
 8 
 9 public class DB {    //负责数据库的连接和关闭
10     
11     static   Connection   con;
12     //连接
13     
14     public   static   Connection         getCon(){
15         try {
16             Class.forName("oracle.jdbc.driver.OracleDriver");
17             con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","lihengyu","lihengyu");
18             System.out.println("数据库已连接");  
19             
20         } catch (ClassNotFoundException e) {
21             // TODO Auto-generated catch block
22             e.printStackTrace();
23         } catch (SQLException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         }finally{
27             
28             return   con;
29         }
30 
31     }
32 
33     //关闭
34     public   static  void   conClose(){
35         try {
36             con.close();  
37         } catch (SQLException e) {
38             // TODO Auto-generated catch block
39             e.printStackTrace();
40         }
41         
42     }
43 
44 }

实体类

 1 实体类
 2 
 3 package com.neusoft.pojo;
 4 
 5 public class Client {  //客户对象  ---对应客户表       对象的属性---表中的字段        一行记录---一个对象
 6     
 7       String  clientNo;
 8       String  clientName;
 9       String  sex;
10       String  birthday;
11       String  certificateNO;
12       String  telNO;
13       String  clientType;
14       String  cardNo;
15       
16     public String getClientNo() {
17         return clientNo;
18     }
19     public void setClientNo(String clientNo) {
20         this.clientNo = clientNo;
21     }
22     public String getClientName() {
23         return clientName;
24     }
25     public void setClientName(String clientName) {
26         this.clientName = clientName;
27     }
28     public String getSex() {
29         return sex;
30     }
31     public void setSex(String sex) {
32         this.sex = sex;
33     }
34     public String getBirthday() {
35         return birthday;
36     }
37     public void setBirthday(String birthday) {
38         this.birthday = birthday;
39     }
40     public String getCertificateNO() {
41         return certificateNO;
42     }
43     public void setCertificateNO(String certificateNO) {
44         this.certificateNO = certificateNO;
45     }
46     public String getTelNO() {
47         return telNO;
48     }
49     public void setTelNO(String telNO) {
50         this.telNO = telNO;
51     }
52     public String getClientType() {
53         return clientType;
54     }
55     public void setClientType(String clientType) {
56         this.clientType = clientType;
57     }
58     public String getCardNo() {
59         return cardNo;
60     }
61     public void setCardNo(String cardNo) {
62         this.cardNo = cardNo;
63     }
64     public Client() {
65         
66     }
67     public Client(String clientNo, String clientName, String sex,
68             String birthday, String certificateNO, String telNO,
69             String clientType, String cardNo) {
70         
71         this.clientNo = clientNo;
72         this.clientName = clientName;
73         this.sex = sex;
74         this.birthday = birthday;
75         this.certificateNO = certificateNO;
76         this.telNO = telNO;
77         this.clientType = clientType;
78         this.cardNo = cardNo;
79     }
80       
81 }

servlet

 1 servlet
 2 
 3 package com.neusoft.servlet;
 4 
 5 import java.io.IOException;
 6 import java.io.PrintWriter;
 7 import java.sql.ResultSet;
 8 import java.sql.SQLException;
 9 import java.util.ArrayList;
10 import java.util.List;
11 
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 
17 import com.neusoft.db.DB;
18 import com.neusoft.pojo.Client;
19 
20 public class PageServlet extends HttpServlet {
21 
22     
23     public void doGet(HttpServletRequest request, HttpServletResponse response)
24             throws ServletException, IOException {
25         request.setCharacterEncoding("utf-8");
26         response.setContentType("text/html;charset=utf-8");
27         
28         List<Client>  list=new ArrayList<Client>();
29         //调用JDBC连接数据库
30         ResultSet  rs;
31         int  cpage=1;
32         
33         if(request.getParameter("cpage")!=null){
34             cpage=Integer.parseInt(request.getParameter("cpage"));
35             
36         }
37     
38         String  sql="select * from " +
39                 "(select t.*, rownum  r  from " +
40                 "(select * from t_client order by client_no) t)" +
41                 " where r between "+(cpage*3-2)+" and "+(cpage*3)+"";
42         
43         
44         try {
45            rs=DB.getCon().createStatement().executeQuery(sql);
46            while(rs.next()){
47                String   clientNo=rs.getString("client_no");
48                 String   clientName=rs.getString("client_name");
49                 String   sex=rs.getString("sex");
50                 String   birthday=rs.getString("birthday");
51                 String   certificateNO=rs.getString("CERTIFICATE_NO");
52                 String   telNO=rs.getString("tel_no");
53                 String   clientType=rs.getString("client_type");
54                 String   cardNo=rs.getString("card_no");
55                 
56                 //封装客户对象
57                 Client  client=new Client(clientNo, clientName, sex, birthday, certificateNO, telNO, clientType, cardNo);
58                 
59                 //将客户对象存入集合中
60                 list.add(client);
61                
62            }
63            
64               request.setAttribute("list", list);
65               request.setAttribute("cpage", Integer.valueOf(cpage));   
66               request.getRequestDispatcher("b.jsp").forward(request, response);
67            
68         } catch (SQLException e) {
69             // TODO Auto-generated catch block
70             e.printStackTrace();
71         }
72         
73     }
74 
75     
76     public void doPost(HttpServletRequest request, HttpServletResponse response)
77             throws ServletException, IOException {
78 
79              this.doGet(request, response);
80     }
81 
82 }

显示页面

 1 b.jsp显示页面
 2 
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 5 <%
 6     String path = request.getContextPath();
 7     String basePath = request.getScheme() + "://"
 8             + request.getServerName() + ":" + request.getServerPort()
 9             + path + "/";
10 %>
11 
12 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
13 <html>
14     <head>
15         <base href="<%=basePath%>">
16 
17         <title>My JSP ‘b.jsp‘ starting page</title>
18 
19         <meta http-equiv="pragma" content="no-cache">
20         <meta http-equiv="cache-control" content="no-cache">
21         <meta http-equiv="expires" content="0">
22         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
23         <meta http-equiv="description" content="This is my page">
24         <!--
25     <link rel="stylesheet" type="text/css" href="styles.css">
26     -->
27 
28     </head>
29 
30     <body>
31 
32         <table border="1"  width="800">
33             <tr>
34                 <td>
35                     客户编号
36                 </td>
37                 <td>
38                     客户姓名
39                 </td>
40                 <td>
41                     客户性别
42                 </td>
43                 <td>
44                     生日
45                 </td>
46                 <td>
47                     证书编号
48                 </td>
49                 <td>
50                     电话号
51                 </td>
52                 <td>
53                     类型
54                 </td>
55                 <td>
56                     卡号
57                 </td>
58             </tr>
59             <c:forEach items="${requestScope.list }" var="client">
60                 <tr>
61                     <td>
62                         <c:out value="${client.clientNo}"></c:out>
63                     </td>
64                     <td>
65                         <c:out value="${client.clientName}"></c:out>
66                     </td>
67                     <td>
68                         <c:out value="${client.sex}"></c:out>
69                     </td>
70                     <td>
71                         <c:out value="${client. birthday}"></c:out>
72                     </td>
73                     <td>
74                         <c:out value="${client.certificateNO}"></c:out>
75                     </td>
76                     <td>
77                         <c:out value="${client.telNO}"></c:out>
78                     </td>
79                     <td>
80                         <c:out value="${client.clientType}"></c:out>
81                     </td>
82                     <td>
83                         <c:out value="${client.cardNo}"></c:out>
84                     </td>
85                 </tr>
86             </c:forEach>
87 
88         </table>
89         
90             <a href="PageServlet?cpage=${requestScope.cpage+1 }">下一页</a>
91     </body>
92 </html>

执行结果

技术分享技术分享

 



























以上是关于动态网页技术--JSP的主要内容,如果未能解决你的问题,请参考以下文章

jsp简介

JSP编程专题1之JSP基础

JSP--指令元素

Java基础——JSP

JSP

Jsp第六课 Jsp简介和初步使用