我无法将属性list
从servlet转移到jsp。这是我的代码:
searchInfo.java:
public class searchInfo extends HttpServlet {
static final String DB_URL = "jdbc:mysql://localhost/students" + "?serverTimezone=GMT%2B8" + "&useSSL=false";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
Connection conn = null;
PreparedStatement pstmt = null;
String sql;
ResultSet rs = null;
List<Map> list = new ArrayList<>();
try {
Class.forName(context.getInitParameter("JDBC_DRIVER"));
conn = DriverManager.getConnection(DB_URL, context.getInitParameter("USER"), context.getInitParameter("PASS"));
sql = "SELECT * FROM INFORMATION WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, req.getParameter("id")); //get id from form
rs = pstmt.executeQuery();
toList(rs, list);
req.setAttribute("list", list);
req.getRequestDispatcher("/search2jsp.jsp").forward(req, resp);
rs.close();
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
static void toList(ResultSet rs, List<Map> list) throws SQLException {
while(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String college =rs.getString("college");
String major = rs.getString("major");
String phone = rs.getString("phone");
Map map = new HashMap();
map.put("id", id);
map.put("name", name);
map.put("sex", sex);
map.put("age", age);
map.put("college", college);
map.put("major",major);
map.put("phone", phone);
list.add(map);
for(Map map1 : list) {
System.out.println(map1);
}
}
}
}
search2jsp.jsp
<html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Search Students Information</title>
</head>
<body>
<h1 align="center">Search Students Information</h1>
<table align="center" width="100%" border="1">
<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>age</th>
<th>college</th>
<th>major</th>
<th>phone</th>
</tr>
<c:forEach items="${list}" var="usr">
<tr>
<td>${usr.id}</td>
<td>${usr.name}</td>
<td>${usr.sex}</td>
<td>${usr.age}</td>
<td>${usr.college}</td>
<td>${usr.major}</td>
<td>${usr.phone}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
但它在search2jsp.jsp中显示Cannot resolve variable 'list'
答案 0 :(得分:0)
我已经解决了我的问题。方法如下:
将standard.jar
和<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
添加到项目依赖项
添加
ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base
在jsp文件的开头。