javaWEB的第一次MVC之旅

这里做一个很简单的实例,只有几个简单的请求,首先先上一下数据表:

数据表结构:

程序目录结构:

 

简单说一下,各个包里边程序的主要作用:

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来完成最终数据的展示

猜你喜欢

转载自www.cnblogs.com/yaoruozi/p/8971056.html
今日推荐