我还在学习开发webapps。我遇到了这个教程。
EmpBean.java
package form;
import java.sql.*;
import java.util.*;
public class EmpBean {
public List dataList(){
ArrayList list=new ArrayList();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from employee");
while(rs.next()){
list.add(rs.getString("name"));
list.add(rs.getString("address"));
list.add(rs.getString("contactNo"));
list.add(rs.getString("email"));
}
}
catch(Exception e){}
return list;
}
}
BeanInServlet.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BeanInServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
form.EmpBean p = new form.EmpBean();
List list=p.dataList();
req.setAttribute("data", list);
RequestDispatcher rd = req.getRequestDispatcher("/jsp/beandata.jsp");
rd.forward(req, res);
}
}
beandata.jsp
<%@page language="java" import="java.util.*" %>
<html>
<body>
<table border="1" width="303">
<tr>
<td width="119"><b>Name</b></td>
<td width="168"><b>Address</b></td>
<td width="119"><b>Contact no</b></td>
<td width="168"><b>Email</b></td>
</tr>
<%Iterator itr;%>
<% List data= (List)request.getAttribute("data");
for (itr=data.iterator(); itr.hasNext(); ){
%>
<tr>
<td width="119"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
</tr>
<%}%>
</table>
</body>
</html>
我尝试使用netbeans并且它可以工作..但是我想知道JSTL是否可以用来呈现数据(View)。
答案 0 :(得分:1)
<table>
<c:foreach var="item" varStatus="status" items="${requestScope.data}">
<c:choose>
<c:when test="${status.index % 4 == 0}">
<tr>
<td width="119"><c:out value="${item}" /></td>
</c:when>
<c:otherwise>
<td width="168"><c:out value="${item}" /></td>
</c:otherwise>
</c:choose>
<c:if test="${status.index % 4 == 3}">
</tr>
</c:if>
</c:foreach>
</table>