MVC案例之查詢學習:HTTP狀態500 - java.lang.NullPointerException
阿新 • • 發佈:2018-11-17
這幾天都在看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. 轉發或(重定向)頁面
學習過程還是得認真細心,特別是搞技術的一定要嚴謹不能有絲毫的馬虎。。。要不然後果就是浪費你大把時間,而浪費時間就是浪費青春,浪費青春就是浪費生命!通俗點說就是浪費洗髮水!