注:本文内容是建立在佟刚老师的JavaWeb教程上写的,如有侵犯,联系删除
MVC是Model-View-Controller的简称,即模型-视图-控制器。
MVC是一种设计模式,它把应用程序分成三个核心模块:模型、视图、控制器,它们各自处理自己的任务。
1.受理请求
2.获取请求参数
3.调用DAO方法
4.可能会把DA0方法的返回值放入request中
5.转发(或重定向)页面
用户在视图提供的界面( JSP )上发出请求,视图把请求转发给控制器(Servlet),控制器调用相应的模型来处理用户请求,模型进行相应的业务逻辑处理(Mode.Dao 方法),并返回数据。最后控制器调用相应的视图来显示模型返回的数据。
即:点击List al students 就展示所有的students,再有一个delete的功能,delete成功后转到一个success页面。
public class JDBCUtils {
static {
try {
System.out.println("链接成功?");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection conn = null;
String url = "jdbc:mysql://127.0.0.1:3306/data?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
System.out.println("链接成功啦");
return conn;
}
public static void close(Connection conn, Statement stm, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stm != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public class StudentDao {
public List getAll() throws SQLException {
List students=new ArrayList<>();
Connection connection=null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql="SELECT flow_id,type,id_card,exam_card,student_name,location,grade from student";
connection= JDBCUtils.getConnection();
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 location=resultSet.getString(6);
int grade=resultSet.getInt(7);
Student student=new Student(flowId,type,idCard,examCard,studentName,location,grade);
students.add(student);
}
for(Student st:students)
System.out.println(st.toString());
return students;
}
public void deleteByFlowId(int flowId) throws SQLException {
Connection connection=null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql="DELETE from student WHERE flow_id = ?";
connection= JDBCUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,flowId);
preparedStatement.executeUpdate();//这里是更新,不然删除了没有效果
}
}
//@WebServlet("/listAllStudents")
public class ListAllStudentsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StudentDao studentDao=new StudentDao();
List students=null;
try {
students = studentDao.getAll();
} catch (SQLException e) {
e.printStackTrace();
}
req.setCharacterEncoding("utf-8");
req.setAttribute("students", students);
req.getRequestDispatcher("/students.jsp").forward(req,resp);
}
}
@WebServlet("/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String flowId = req.getParameter("flowId");
StudentDao studentDao = new StudentDao();
try {
studentDao.deleteByFlowId(Integer.parseInt(flowId));
req.getRequestDispatcher("/success.jsp").forward(req,resp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
设置Servlet映射有两个方法
1、在Servlet类的上面添加@WebServlet("/listAllStudents")
2、在web.xml配置Servlet映射
listallstudents
cqy.mvc.ListAllStudentsServlet
listallstudents
/listAllStudents
若目标的响应页面不需要从 request 中读取任何值,则可以使用重定向(还可以防止表单的重复提交)。
1、没有使用数据库连接池,DBUtils,JDBCUtils 工具类,DAO 基类等,上面代码过于臃肿;
2、一个请求一个 Serlvet 不好!一个模块使用一个 Serlvet,该如何实现多个请求可以使用一个 Servlet ;
3、页面体验不够友好,在页面上加入 jQuery 提示。