我正在尝试提出一个符合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);
}
}
}
Employee.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;
}
}
的index.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)
答案 0 :(得分:1)
在配置servlet和servlet映射时,您可能还想配置在jsp中的servlet-mapping中提到的url-pattern。
<强> JSP 强>
<form action="post" action="/dbconn">
...
</form>
<强>的web.xml 强>
<servlet>
<servlet-name>dbconn</servlet-name>
<servlet-class>com.xx.xx.DBConn</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dbconn</servlet-name>
<url-pattern>/dbconn</url-pattern>
</servlet-mapping>
此外,您可能需要考虑使用数据访问对象模式。 (如果你只是想了解servlet,请忽略)
答案 1 :(得分:0)
使用dispatcher.include(request, response)
方法。