使用下面這個jsp的前你需要寫一個Stu類生成set和get方法
Public Class Stu{
private int id;
private String name;
public void setId(int id){
thisid=id;
}
public int getId(){
return thisid;
}
public void setName(String name){
thisname=name;
}
public String getName(){
return thisname;
}
}
寫個簡單的servlet
public class gotoMall extends HttpServlet {
public void doGet(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
ArrayList<Stu> stus=new ArrayList<Stu>();
Stu stu=new Stu();
stusetId();
stusetName("aa");
stusetId();
stusetName("bb");
stusadd(stu);
stusadd(stu);
requestsetAttribute("stus" stus);
requestgetRequestDispatcher("/WEBINF/admin/myjsp")forward(request response);
}
public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
thisdoGet(request response);
}
}
myjsp
<%@ page language="java" import="javautil*" pageEncoding="utf"%>
<! 引入jstl標簽庫 >
<%@ taglib uri="%>
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html>
<head>
<script type="text/javascript" language="javascript" src="js/js_deletejs" charset="gbk"></script>
<link href="css/maincss" rel="stylesheet" type="text/css" />
<meta httpequiv="ContentType" content="text/html; charset=utf" />
<title>My JSP page</title>
</head>
<body>
LIST:<select>
<! 前提是已經把stus這個list放到了requestattribute中 在servlet或action中 requestsetAttribute(stus); >
<c:forEach items="${stus}" var="stu">
<! Stu類中必須有set和get方法 >
<option>${stuname}</option>
</c:forEach>
</select>
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19829.html