有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么jsp无法从servlet中找到属性

我无法将属性list从servlet传输到jsp。这是我的密码:

搜索信息。爪哇:

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中显示了Cannot resolve variable 'list'。jsp


共 (1) 个答案

  1. # 1 楼答案

    我已经解决了我的问题。方法如下:

    1. jstl.jarstandard.jar添加到项目依赖项中

    2. 添加<%@ 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"%> 在jsp文件的开头