本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.在上一学习笔记中,了解了MVC设计的思想,这一学习笔记,主要手动写一个MVC的查询程序,比较糙,重理解思想

2.需求:通过index.jsp 页面发过请求,查询学生的信息,将学生的信息输出到des.jsp页面上;可以删除学生的信息。

3.代码结构

  1)index.jsp  : 一个查询的超链接页面;

  2)des.jps     :  显示查询的页面;

  3)success.jsp :删除成功后跳转的页面

  4)ListAllStudentsServlet.java : 负责处理index 页面请求的servlet,同时与dao交互;

  5)DeletStudentServlet.java   :通过传入的flowId 进行删除;

  6)StudentDao.java : 定义方法getA() 用于与数据库交互,查询数据 ,返回结果;deleteByFlowId(int flowId)方法,按照flowId删除相应的学生信息;

  7)Student.java  :bean  同数据库表的字段一致,get set方法,带参,不带餐构造器,用于检查的 toString 方法;

4.具体代码

  1)index.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>index</title>
</head>
<body>
<a href="listAllStudents">List All Students</a> </body>
</html>

  

  2)des.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="com.jason.testMVC.Student"%>
<!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>des</title>
</head>
<body>
<%
List<Student> stus = (List<Student>) request
.getAttribute("students"); if (stus == null) {
System.out.println("stus is null");
} else {
%>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>FlowId</th>
<th>Type</th>
<th>IdCard</th>
<th>ExamCard</th>
<th>StudentName</th>
<th>Location</th>
<th>Grade</th>
<th>Delete</th>
</tr>
<%
for (Student stu : stus) {
%>
<tr>
<td><%=stu.getExamCard()%></td>
<td><%=stu.getType()%></td>
<td><%=stu.getIdCard()%></td>
<td><%=stu.getExamCard()%></td>
<td><%=stu.getStudentName()%></td>
<td><%=stu.getLocation()%></td>
<td><%=stu.getGrade()%></td>
<td><a href="deletStudent?flowId=<%=stu.getFlowId() %>"/>Delete</td> //通过这个种方式,向servlet传入flowId参数
</tr>
<%
}
}
%>
</table> </body>
</html>

  3)success.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> <h1>删除成功</h1>
<a href="listAllStudents">List All Students</a> </body>
</html>

  4)ListAllStudentsServlet.java

 package com.jason.testMVC;

 import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ListAllStudentsServlet
*/
@WebServlet("/listAllStudents")
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);
} }

  

  5)DeletStudentServlet.java

 package com.jason.testMVC;

 import java.io.IOException;

 import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; /**
* Servlet implementation class DeletStudentServlet
*/ @WebServlet("/deletStudent")
public class DeletStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String flowIdStr = request.getParameter("flowId");
// int flowId = Integer.parseInt(flowIdStr);
// System.out.println(flowIdStr); StudentDao studentDao = new StudentDao();
boolean flage = studentDao.deleteByFlowId(Integer.parseInt(flowIdStr)); if(flage){
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else{
System.out.println("删除失败");
} } }

  6)StudentDao.java

 package com.jason.testMVC;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; public class StudentDao { public List<Student> getAll() { List<Student> students = new ArrayList<Student>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null; try { String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/atguigu";
String user = "root";
String password = "zhangzhen";
// 加载驱动类
Class.forName(driverClass);
connection = DriverManager.getConnection(url, user, password); String sql = "SELECT * FROM student"; preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery(); while (resultSet.next()) {
int FlowId = resultSet.getInt(1);
int type = resultSet.getInt(2);
String idCard = resultSet.getString(3);
String examCard = resultSet.getString(4);
String studentName = resultSet.getString(5);
String locatoin = resultSet.getString(6);
int grade = resultSet.getInt(7); Student student = new Student(FlowId, type, idCard, examCard,
studentName, locatoin, grade); students.add(student); } } catch (Exception e) {
e.printStackTrace();
} finally { // 关闭资源
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
} try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
} try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} return students;
} public boolean deleteByFlowId(int flowId){ Connection connection = null;
PreparedStatement preparedStatement = null;
boolean flage = false; try { String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/atguigu";
String user = "root";
String password = "zhangzhen";
// 加载驱动类
Class.forName(driverClass);
connection = DriverManager.getConnection(url, user, password); String sql = "DELETE FROM student WHERE FlowID = ?"; preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, flowId);
int result = preparedStatement.executeUpdate();
if(result >= 0){
flage = true;
} } catch (Exception e) {
e.printStackTrace();
} finally { // 关闭资源 try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
} try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} return flage;
} }

  6)Student.java

 package com.jason.testMVC;

 /**
*
* @author: jason
* @time:2016年5月24日下午11:31:08
* @description:
*/
public class Student {
private int flowId; private int type; private String idCard; private String examCard; private String studentName; private String location; private int grade; public Integer getFlowId() {
return flowId;
} public void setFlowId(Integer flowId) {
this.flowId = flowId;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getIdCard() {
return idCard;
} public void setIdCard(String idCard) {
this.idCard = idCard;
} public String getExamCard() {
return examCard;
} public void setExamCard(String examCard) {
this.examCard = examCard;
} 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 int getGrade() {
return grade;
} public void setGrade(int grade) {
this.grade = grade;
} public Student(Integer flowId, int type, String idCard, String examCard,
String studentName, String location, int grade) {
super();
this.flowId = flowId;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
} //用于反射
public Student() { } @Override
public String toString() {
return "Student [flowId=" + flowId + ", type=" + type + ", idCard="
+ idCard + ", examCard=" + examCard + ", studentName="
+ studentName + ", location=" + location + ", grade=" + grade
+ "]";
} }

5.简单总结

 1)对于MVC设计模式的认识

  ① M: Model. Dao

  ② V: View. JSP, 在页面上填写 Java 代码实现显示

  ③ C: Controller. Serlvet:

      I. 受理请求

       II. 获取请求参数

III. 调用 DAO 方法

       IV. 可能会把 DAO 方法的返回值放入request 中

      V. 转发(或重定向)页面

  2)问题和足

  问题:什么时候转发,什么时候重定向 ? 若目标的响应页面不需要从 request 中读取任何值,则可以使用重定向。(还可以防止表单的重复提交)

  不足: I. 代码臃肿,结构不清楚。        解决方案:使用数据库连接池,DBUtils,JDBCUtils 工具类,DAO 基类;

        II. 一个请求一个 Serlvet 不好。    解决方案:一个模块使用一个 Serlvet,即多个请求可以使用一个 Servlet;

     III. 使用不友好。            解决方案:在页面上加入 jQuery 操作提示,如删除,保存等。

[原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想),小结 ,问题的更多相关文章

  1. [原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  2. [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  3. [原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  4. [原创]java WEB学习笔记24:MVC案例完整实践(part 5)---删除操作的设计与实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  5. [原创]java WEB学习笔记23:MVC案例完整实践(part 4)---模糊查询的设计与实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  6. [原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  7. [原创]java WEB学习笔记20:MVC案例完整实践(part 1)---MVC架构分析

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. [原创]java WEB学习笔记95:Hibernate 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

随机推荐

  1. 最常用的几个python库--学习引导

    核心库 1.NumPy 当我们用python来处理科学计算任务时,不可避免的要用到来自SciPy  Stack的帮助.SciPy Stack是一个专为python中科学计算而设计的软件包,注意不要将它 ...

  2. php里面用魔术方法和匿名函数闭包函数动态的给类里面添加方法

    1.认识  __set  (在给不可访问属性赋值时,__set() 会被调用) 也就是说你再访问一个类里面没有的属性,会出发这个方法 class A{ private $aa = '11'; publ ...

  3. java消息中间件之ActiveMQ初识

    目录 消息中间件简介 解耦合和异步 可靠性和高效性 JMS P2P Pub/Sub AMQP JMS和AMQP对比 常见消息中间件 ActiveMQ RabbitMQ Kafka 综合比较 标签(空格 ...

  4. ueditor的上传存储问题

    1.使用了 http://download.csdn.net/download/ouyhong123/8520689 下载的修改版jar包.主要修改是增加了一个地址属性,ActionEnter的参数. ...

  5. hiho1080 更为复杂的买卖房屋姿势

    题目链接: hihocoder1080 题解思路: 题目中对区间改动有两个操作: 0   区间全部点添加v 1   区间全部点改为v easy想到应该使用到两个懒惰标记  一个记录替换  一个记录增减 ...

  6. fastjson List<> 转Json , Json 转List<>

    SerializeWriter:相当于StringBuffer JSONArray:相当于List<Object> JSONObject:相当于Map<String, Object& ...

  7. codeforces #364c They Are Everywhere 尺取法

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. iOS collectionView添加类似tableView的tableHeaderView

    我们都知道UITableview有一个tableHeaderFooterView,这样我们在布局页面的时候,如果顶部有轮播图,可以直接把轮播图设置为tableView的HeaderFooterView ...

  9. HTML DOM节点的增删改查

    上篇博客中,我们已经初步接触了DOM基础,可是我们学习是为了可以更好地应用,今天我们就来看看DOM节点的增删改查. 无论在哪里,我们想要操作一个东西,总是应该先去获得它.那么我们怎么获得呢? HTML ...

  10. tcp 状态转移图详解

    首先看一张图片: 虚线表示服务端的状态转移,实现表示客户端的状态转移. 初始的close状态并不是真是的状态,只是为了方便描述开始和终止状态而构造出来的. 从服务端的状态转移开始说: 服务端打开后处于 ...