我正在尝试提出一个符合mvc架构的代码并在jsp页面中显示数据库的内容。数据的连接和处理要在java文件中并且只有数据的显示会包含在jsp页面。
我正在使用tomcat服务器。我的 WEB-INF/lib 文件夹中有 ojdbc6.jar 和 jstl-1.2.jar。
(更新)将我的 web.xml 更改为指向索引后,我得到了 java.lang.StackOverflowError 错误。
代码有什么遗漏/错误吗?另外,如果我不遵守 MVC 设计,请告诉我。任何想法将不胜感激。谢谢你。
这是我要运行的代码。
DBConn.java
public class DBConn extends HttpServlet{
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Connection connection = null;
Statement stmt=null;
ResultSet rs=null;
List<Employee> dataList = new ArrayList<Employee>();
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String portNumber = "1521";
String sid = "xe";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "hr";
String password = "hr";
connection = DriverManager.getConnection(url, username, password);
stmt = connection.createStatement();
rs = stmt.executeQuery("select employee_id, first_name from employees");
while (rs.next()) {
dataList.add(new Employee(rs.getInt("employee_id"),
rs.getString("first_name")));
}
} catch (ClassNotFoundException e) {
// Could not find the database driver
e.printStackTrace();
} catch (SQLException e) {
// Could not connect to the database
e.printStackTrace();
} finally{
if(rs!=null){
try{
rs.close();
}catch(Exception ex) { /* */ ex.printStackTrace();}
}
if(stmt!=null){
try{
stmt.close();
}catch(Exception ex) { /* */ ex.printStackTrace();}
}
if(connection !=null){
try{
connection.close();
}catch(Exception ex) { /* */ ex.printStackTrace();}
}
}
request.setAttribute("data", dataList);
String strViewPage = "index.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(strViewPage);
if (dispatcher != null) {
dispatcher.forward(request, response);
}
}
}
雇员.java
public class Employee {
private Integer id;
private String name;
//public constructors and
//setter/getter
public Employee(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
索引.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<table border="1" width="303">
<tr>
<td width="119"><b>Emp ID</b></td>
<td width="168"><b>First Name</b></td>
<tr
<form action="post">
<c:forEach var="employee" items="${data}">
<br/> ${employee.id} ${employee.name}
</c:forEach>
</form>
</tr>
</table>
</body>
</html>
web.xml
<servlet>
<servlet-name>DBConn</servlet-name>
<servlet-class>DB.DBConn</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DBConn</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
错误
09 6, 12 10:09:00 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet DBConn threw exception
java.lang.StackOverflowError
at java.util.HashMap$KeySet.<init>(HashMap.java:872)
at java.util.HashMap$KeySet.<init>(HashMap.java:872)
at java.util.HashMap.keySet(HashMap.java:869)
at java.util.HashSet.iterator(HashSet.java:153)
at java.util.Collections$1.<init>(Collections.java:3382)
at java.util.Collections.enumeration(Collections.java:3381)
at org.apache.catalina.connector.Request.getAttributeNames(Request.java:1027)
at org.apache.catalina.connector.RequestFacade.getAttributeNames(RequestFacade.java:300)
at org.apache.catalina.core.ApplicationHttpRequest$AttributeNamesEnumerator.<init>(ApplicationHttpRequest.java:927)
at org.apache.catalina.core.ApplicationHttpRequest.getAttributeNames(ApplicationHttpRequest.java:243)
at org.apache.catalina.core.ApplicationHttpRequest$AttributeNamesEnumerator.<init>(ApplicationHttpRequest.java:927)
at org.apache.catalina.core.ApplicationHttpRequest.getAttributeNames(ApplicationHttpRequest.java:243)