这几天都在看servlet和jsp的视频,早上看完MVC案例的查询,没想到一个小小的错误搞了我差不多一个下午,还是得多点记录总结,以后绝不再犯同样的错误!
这个查询主要是通过点击 test.jsp 页面的超链接来查看数据库里面的数据信息。
1.首先有一个 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 Servlet</a>
</body>
</html>
2.创建一个学生类 Student.java,里面的信息要和数据库里面的一一对应。
package com.smc.javaweb.mvc;
public class Student {
private Integer id;
private String userName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return userName;
}
public void setUsername(String userName) {
this.userName = userName;
}
public Student(Integer id, String userName) {
super();
this.id = id;
this.userName = userName;
}
public Student() {}
}
3.然后创建一个 StudentDao 类,用来获取数据库信息并返回。
package com.smc.javaweb.mvc;
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<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql:///sys";
String user = "root";
String password = "123456";
Class.forName(driverClass);
connection = DriverManager.getConnection(url, user, password);
String sql = "SELECT id,username FROM test";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
int id = resultSet.getInt(1);
String userName = resultSet.getString(2);
Student student = new Student(id, userName);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
4.创建一个servlet类,叫做 ListAllStudentsServlet ,用来处理请求,获取请求参数,调用Dao方法转发页面。
package com.smc.javaweb.mvc;
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;
@WebServlet("/listAllStudentsServlet")
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.创建一个jsp页面 students.jsp 来显示点击链接后的页面。
<%@page import="com.smc.javaweb.mvc.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<!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> stus = (List<Student>)request.getAttribute("students");
%>
<table>
<tr>
<th>Id</th>
<th>UserName</th>
</tr>
<%
for(Student student:stus){
%>
<tr>
<td><%= student.getId() %></td>
<td><%= student.getUsername() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
到这里,运行:
没错,发生了500错误!后来通过查看错误日志和网上找解决方案,又改了一个数据库名字的错误和一个由于粗心的手误。发现还是不行,最后又重新看视频重新再写一遍...才发现是第三步中最后的 return null 没有改!!!应该是 return students 的。
改正之后:
总结一下:
M:model. Dao
V:view. JSP,在页面上填写Java代码实现显示
C:controller. servlet:
1. 受理请求
2. 获取请求参数
3. 调用Dao方法
4. 可能会把Dao方法的返回值放入request中
5. 转发或(重定向)页面
学习过程还是得认真细心,特别是搞技术的一定要严谨不能有丝毫的马虎。。。要不然后果就是浪费你大把时间,而浪费时间就是浪费青春,浪费青春就是浪费生命!通俗点说就是浪费洗发水!