• MVC第二篇


    MVC实例

    现在Javaweb开发第一阶段学习步入实践阶段,在这儿将自己做的项目记下来,也方便积累经验。

    下图是处理流程:

     

     

    MySQL数据库表如下所示:

     

    1.在web项目webContent中新建test.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" 
        pageEncoding="UTF-8"%> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Insert title here</title> 
    </head> 
    <body>

        <a href="listAllStudentsServlet">List All Students</a>

    </body> 
    </html>

    2在Java-src中新建ListAllStudentServlet.java,并对其进行配置

    package com.javaweb.mvc;

    import java.io.IOException; 
    import java.util.Arrays; 
    import java.util.List;

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


    public class ListAllStudentsServlet extends HttpServlet { 
        private static final long serialVersionUID = 1L;

        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
            
            StudentDao studentDao = new StudentDao(); 
            List<Student> students = studentDao.getAll(); 
            
            request.setAttribute("students", students); 
            request.getRequestDispatcher("/students.jsp").forward(request, response); 
            
            
        }

    }

    3.继续创建Student.java

    package com.javaweb.mvc;

    public class Student { 
        private Integer id; 
        private String studentName; 
        private String location; 
        private String telephone; 
        
        public Integer getId() { 
            return id; 
        } 
        public void setId(Integer id) { 
            this.id = id; 
        } 
        public String getStudentName() { 
            return studentName; 
        } 
        public void setStudentName(String studentName) { 
            this.studentName = studentName; 
        } 
        public String getLocation() { 
            return location; 
        } 
        public void setLocation(String location) { 
            this.location = location; 
        } 
        public String getTelephone() { 
            return telephone; 
        } 
        public void setTelephone(String telephone) { 
            this.telephone = telephone; 
        } 
        public Student(Integer id, String studentName, String location, String telephone) { 
            super(); 
            this.id = id; 
            this.studentName = studentName; 
            this.location = location; 
            this.telephone = telephone; 
        } 
        
       

    }

    4.创建StudentDao.java

    package com.javaweb.mvc;

    import java.util.ArrayList; 
    import java.util.List; 
    import java.sql.*;

    public class StudentDao { 
        
        public List<Student> getAll(){ 
            List<Student> students = new ArrayList<Student>(); 
            Connection conn = null; 
            PreparedStatement ps = null; 
            ResultSet rs = null; 
            
            try { 
                
                    Class.forName("com.mysql.jdbc.Driver"); 
                
                String url = "jdbc:mysql://localhost:3306/student?user=root&password=1234"; 
                String user = "root"; 
                String password = "1234"; 
                String sql = "select * from stu"; 
                conn = DriverManager.getConnection(url, user, password); 
                ps = conn.prepareStatement(sql); 
                rs = ps.executeQuery(); 
                while(rs.next()){ 
                    int id = rs.getInt(1); 
                    String studentName = rs.getString(2); 
                    String location = rs.getString(3); 
                    String telephone = rs.getString(4); 
                    
                    Student student = new Student(id, studentName, location, telephone); 
                    
                    students.add(student); 
                } 
                
            }catch (ClassNotFoundException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            catch (SQLException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            }finally{ 
                try { 
                    if(rs != null) 
                        rs.close(); 
                    if(ps != null) 
                        ps.close(); 
                    if(conn != null) 
                        conn.close(); 
                } catch (SQLException e) { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                } 
            } 
            
            return students; 
        }

    }

    5.在webContent中新建students.jsp

    <%-- 显示学生信息 --%> 
    <%@page import="java.util.List"%> 
    <%@page import="com.javaweb.mvc.Student" %> 
    <%@ page language="java" contentType="text/html; charset=UTF-8" 
        pageEncoding="UTF-8"%> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Insert title here</title> 
    </head> 
    <body>

        
        <% 
            List<Student> students = (List<Student>)request.getAttribute("students"); 
            
        %> 
        <table> 
            <tr> 
                <th>id</th> 
                <th>studentName</th> 
                <th>location</th> 
                <th>telephone</th> 
            </tr> 
            
            <% 
                for(Student student:students){ 
            %> 
                <tr> 
                    <td><%= student.getId() %></td> 
                    <td><%= student.getStudentName() %></td> 
                    <td><%= student.getLocation() %></td> 
                    <td><%= student.getTelephone() %></td> 
                </tr> 
            <% 
                } 
            %> 
            
        </table> 
        
       

    </body> 
    </html>

    6.添加JDBC驱动

    添加在lib文件夹,因为lib文件夹中存放支持web应用运行的JAR文件

     

    7.接下来就可以运行了

    这是运行结果:

     

     

  • 相关阅读:
    Matlab绘图基础——利用axes(坐标系图形对象)绘制重叠图像 及 一图多轴(一幅图绘制多个坐标轴)
    [学习笔记]Javaweb开发视频教程之Tomcat9配置
    Matlab绘图基础——axis设置坐标轴取值范围
    Cauchy-Binet公式的证明 及 对《来自特征值的特征向量》的理解
    [问题解决]win10误删启动项(BCD)(HP电脑亲测,无需启动盘,并非重装系统)
    [经验分享]用自相似的思想来理解二叉树的三种遍历方法
    [参考]用递归的方法获取 字符 对应的 二进制字符串 (C/C++)
    [经验分享]SecureCRT导出操作日志 + Notepad自定义语言格式高亮日志文件
    [公式推导]一般线性秩统计量的方差函数 及其 极限分布
    [问题解决]RedHat7更换CentOS7的yum源时踩过的坑
  • 原文地址:https://www.cnblogs.com/jianglingjun/p/6815339.html
Copyright © 2020-2023  润新知