簡體   English   中英

為什么JSP無法從Servlet中找到屬性

[英]Why jsp can't find attribute from servlet

我無法將屬性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> 

但它顯示Cannot resolve variable 'list'在search2jsp.jsp中Cannot resolve variable 'list'

我已經解決了我的問題。 方式如下:

  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文件的開頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM