0
我有一個Servlet查詢數據庫並創建ArrayList(aka對象)。如何從包含在JSP中的Servlet獲取對象?
該Servlet包含在JSP頁面中,用於打印陣列列表的內容,但我不知道如何將它們從Servlet傳遞到JSP。
我怎樣才能讓JSP從Servlet中獲取這些對象?
這裏是我的servlet代碼:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//CREATING CONTAINERS THAT WILL BE FORWARDED
ArrayList<Volet> voletList = new ArrayList<Volet>();
ArrayList<Critere> critereList = new ArrayList<Critere>();
PrintWriter writer = response.getWriter();
try {
//CONNEXION TO DATABASE:
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "OCP", "oracle11g");
Statement st = con.createStatement();
//SQL REQUEST
String sqlVolet = "select * from ocp.volet";
ResultSet rsVolet = st.executeQuery(sqlVolet);
//WRITING FROM SQL RESULT INTO CONTAINER LIST:
while(rsVolet.next())
{
Volet volet = new Volet();
volet.setId(rsVolet.getInt(1));
volet.setDescription(rsVolet.getString(2));
voletList.add(volet);
}
String sqlCritere = "select * from ocp.critere";
ResultSet rsCritere = st.executeQuery(sqlCritere);
while(rsCritere.next())
{
Critere critere = new Critere();
critere.setId(rsCritere.getInt(1));
critere.setDescription(rsCritere.getString(2));
critere.setVoletid(rsCritere.getInt(3));
critereList.add(critere);
}
//CLOSING DATABASE CONNEXION:
con.close();
//DISPATCH THE CONTAINER THROUGH FORWARD
}
catch(Exception e)
{
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/View/ErrorPage.jsp");
request.setAttribute("e", e);
requestDispatcher.forward(request, response);
}
}
附:我試圖將對象轉發給JSP,但它不起作用。
通過使用HttpServletRequest和-Response。 – Stultuske
請告訴我們你試過了什麼。特別是servlet代碼。 – Rajesh
你能明確一點的方法嗎? – Mikahel