I'm using Eclipse to create a "Dynamic Web Project". And i write some simple code by MVC structure.
My LoaiBean class:
package bean;
public class LoaiBean {
private String maLoai;
private String tenLoai;
public String getMaLoai() {
return maLoai;
}
public void setMaLoai(String maLoai) {
this.maLoai = maLoai;
}
public String getTenLoai() {
return tenLoai;
}
public void setTenLoai(String tenLoai) {
this.tenLoai = tenLoai;
}
public LoaiBean(String maLoai, String tenLoai) {
super();
this.maLoai = maLoai;
this.tenLoai = tenLoai;
}
}
My LoaiDao class:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import bean.LoaiBean;
public class LoaiDao {
private DBConnection dbConnection = null;
public LoaiDao() {
super();
this.dbConnection = new DBConnection("bookdb");
}
public LoaiDao(DBConnection dbConnection) {
super();
this.dbConnection = dbConnection;
}
/**
* get list of categories
* @return {@link ArrayList}
*/
public ArrayList<LoaiBean> getLoai(){
Connection connection = this.dbConnection.getConnection(true);
ArrayList<LoaiBean> loais = new ArrayList<LoaiBean>();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from loai");
while(rs.next()) {
loais.add(new LoaiBean(
rs.getString("maloai"),
rs.getString("tenloai")
));
}
} catch (SQLException e) {
e.printStackTrace();
}
return loais;
}
}
My DBConnection class:
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private String server = "localhost";
private String port = "1433";
private String databaseName = null;
private String user = "sa";
private String password = "123";
private Connection connection = null;
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getDatabaseName() {
return databaseName;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Connection getConnection(boolean autoConnect) {
if(autoConnect && this.connection == null) {
this.connect();
}
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public DBConnection(String databaseName) {
super();
this.databaseName = databaseName;
}
public void connect() {
if(this.databaseName == null) {
return;
}
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String st = "jdbc:sqlserver://"+this.server+":"+this.port+";databaseName="+this.databaseName+";user="+this.user+";password="+this.password;
try {
this.connection = DriverManager.getConnection(st);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void reconnect() {
this.disconnect();
this.connect();
}
public void disconnect() {
if(this.connection != null) {
try {
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
My LoaiBo class:
package bo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.LoaiBean;
import dao.LoaiDao;
public class LoaiBo {
private LoaiDao dao = null;
public LoaiDao getDao() {
return dao;
}
public void setDao(LoaiDao dao) {
this.dao = dao;
}
public LoaiBo() {
super();
this.dao = new LoaiDao();
}
public LoaiBo(LoaiDao dao) {
super();
this.dao = dao;
}
public ArrayList<LoaiBean> getLoai(){
return this.dao.getLoai();
}
}
And My JSP file tc.jsp:
<%@page import="bean.LoaiBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="bo.LoaiBo"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<%
LoaiBo bo = new LoaiBo();
ArrayList<LoaiBean> loais = bo.getLoai(); // error is showed here
%>
<div class="row justify-content-center">
<div class="col-md-4 col-sm-6 col-xs-12">
<ul class="list-group">
<% for (LoaiBean loaiBean : loais) { %>
<li class="list-group-item"><%= loaiBean.getMaLoai() %></li>
<% } %>
</ul>
</div>
</div>
</body>
</html>
When i run file tc.jsp above, the error is appear: enter image description here
HTTP Status 500 – Internal Server Error
Type Exception Report
Message An exception occurred processing [/tc.jsp] at line [17]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing [/tc.jsp] at line [17]
14: <body>
15: <%
16: LoaiBo bo = new LoaiBo();
17: ArrayList<LoaiBean> loais = bo.getLoai();
18: %>
19: <div class="row justify-content-center">
20: <div class="col-md-4 col-sm-6 col-xs-12">
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:625)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:514)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException
dao.LoaiDao.getLoai(LoaiDao.java:39)
bo.LoaiBo.getLoai(LoaiBo.java:40)
org.apache.jsp.tc_jsp._jspService(tc_jsp.java:140)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
These function run normally when i test by a java core class with main function, but not run in jsp file.
I'm using eclipse, tomcat 9, java 8.
I don't know how to fix this problem. I hope people guys will help me. Thank you.