1. 程式人生 > >javaWEB的第一次MVC之旅

javaWEB的第一次MVC之旅

設置 img 數據信息 del ttr ima 響應 add pan

這裏做一個很簡單的實例,只有幾個簡單的請求,首先先上一下數據表:

數據表結構:

技術分享圖片

程序目錄結構:

技術分享圖片

簡單說一下,各個包裏邊程序的主要作用:

bean: 裏面定義一個學生類,以及對應的屬性,並且還有有參和無參的構造方法,並且還有對應屬性的get() 和 set() 方法

dao: 連接數據庫,並且根據實際業務需求定義的增刪改查的方法

servlet: 實現和JSP頁面的交互,從而動態在JSP頁面顯示數據信息

整個程序的大體流程:

要在jsp頁面上展示所有的數據,以表格的形式,因此 發出展示數據的請求-----到響應這個請求的servlet----調用響應的業務邏輯處理方法----返回給jsp頁面

因此:testStu.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
> 7 <title>獲取學生請求頁面</title> 8 </head> 9 <body> 10 <a href="ShowStudent">學生信息顯示</a> 11 </body> 12 </html>

這裏請求的連接直接就是一個servlet

定義的Servlet,ShowStudent.java

 1 package com.mvc.servlet;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import
javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.mvc.bean.Student; 13 import com.mvc.dao.StudentDao; 14 15 @WebServlet("/ShowStudent") 16 public class ShowStudent extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 protected void doGet(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 StudentDao studentDao = new StudentDao();
//調用獲取學生信息的方法
22 List<Student> students = studentDao.getAllStu(); 23 //設置屬性 24 request.setAttribute("students", students); 25 //轉發請求,到展示頁面 26 request.getRequestDispatcher("/showStu.jsp").forward(request, response); 27 } 28 29 }

StudentDao看看這裏邊的getAllStu()方法,都做了哪些事
 1 // 顯示學生信息的方法
 2     public List<Student> getAllStu() {
 3         List<Student> students = new ArrayList<>();
 4         // 操作連接數據庫,這裏使用Oracle數據庫
 5         Connection conn = null;
 6         PreparedStatement ps = null;
 7         ResultSet res = null;
 8         String driverClass = "oracle.jdbc.driver.OracleDriver";
 9         String url = "jdbc:oracle:thin:@localhost:1521:orcl";
10         try {
11             Class.forName(driverClass);
12             conn = DriverManager.getConnection(url, "scott", "yao");
13             String sql = "SELECT * from STUDENTINFO";
14             ps = conn.prepareStatement(sql);
15             res = ps.executeQuery();
16             while (res.next()) {
17                 int id = res.getInt(1);
18                 String sno = res.getString(2);
19                 String sname = res.getString(3);
20                 String sclass = res.getString(4);
21 
22                 Student student = new Student(id, sno, sname, sclass);
23                 students.add(student);
24             }
25         } catch (Exception e) {
26             // TODO: handle exception
27         } finally {
28             try {
29                 if (res != null) {
30                     res.close();
31                 }
32             } catch (SQLException e) {
33                 // TODO Auto-generated catch block
34                 e.printStackTrace();
35             }
36             try {
37                 if (ps != null) {
38                     ps.close();
39                 }
40             } catch (SQLException e) {
41                 // TODO Auto-generated catch block
42                 e.printStackTrace();
43             }
44             try {
45                 if (conn != null) {
46                     conn.close();
47                 }
48             } catch (SQLException e) {
49                 // TODO Auto-generated catch block
50                 e.printStackTrace();
51             }
52         }
53         return students;
54     }

在ShowStudent的servlet裏面我們看到了request對象幹了兩件事情,設置了一個屬性,並且轉發請求到了showStu.jsp的頁面,因此,看看showStu.jsp是怎麽獲取這些信息的

 1 <%@page import="com.mvc.bean.Student"%>
 2 <%@page import="java.util.List"%>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>學生信息顯示</title>
10 </head>
11 <body>
12 <%
13 List<Student>  stu = (List<Student>)request.getAttribute("students");
14 %>
15 <table>
16 <tr>
17     <th>ID</th>
18     <th>學號</th>
19     <th>姓名</th>
20     <th>班級</th>
21     <th>刪除</th>
22 </tr>
23 <%
//進行遍歷
24 for(Student student:stu){ 25 %> 26 <tr> 27 <td><%= student.getID() %></td> 28 <td><%= student.getSno() %></td> 29 <td><%= student.getName()%></td> 30 <td><%= student.getSclass() %></td> 31 <td><a href="DeleteStudent?id=<%=student.getID() %>">刪除</a></td> 32 </tr> 33 <% 34 } 35 %> 36 </table> 37 </body> 38 </html>

最終的運行結果如下:

技術分享圖片

在點擊這個超鏈接之後,會轉發到這個請求到showStu.jsp來完成最終數據的展示

技術分享圖片

javaWEB的第一次MVC之旅