package com.myapp.beans;
public class Student {
private String sno;
private String sname;
private Integer sage;
private String ssex;
private String deptno;
public Student(String sno, String sname, Integer sage, String ssex, String deptno) { this.sno = sno; this.sname = sname; this.sage = sage; this.ssex = ssex; this.deptno = deptno; }
public String getDeptno() { return deptno; }
public void setDeptno(String deptno) { this.deptno = deptno; }
public Integer getSage() { return sage; }
public void setSage(Integer sage) { this.sage = sage; }
public String getSname() { return sname; }
public void setSname(String sname) { this.sname = sname; }
public String getSno() { return sno; }
public void setSno(String sno) { this.sno = sno; }
public String getSsex() { return ssex; }
public void setSsex(String ssex) { this.ssex = ssex; }
} |
ShowServlet.java:
package com.myapp.servlets;
import java.io.IOException; import java.util.*; import javax.servlet.ServletException; import javax.servlet.http.*; import com.myapp.beans.Student;
public class ShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //定义10个学生对象 Student s[] = new Student[10]; s[0] = new Student("s001","张三",15,"女","d002"); s[1] = new Student("s002","李四",15,"男","d002"); s[2] = new Student("s003","王五",16,"女",null); s[3] = new Student("s004","赵六",15,"男","d004"); s[4] = new Student("s005","田七",17,"女","d002"); s[5] = new Student("s006","周八",15,"男",null); s[6] = new Student("s007","付九",19,"女","d003"); s[7] = new Student("s008","王莹",16,"男",null); s[8] = new Student("s009","罗剑",15,"男","d002"); s[9] = new Student("s010","宇飞",15,"男","d002"); //将数据装入List List l = new ArrayList(); for(int i=0;i<s.length;i++){ l.add(s[i]); } //将List放入session request.getSession().setAttribute("allstudents", l); //跳转到要显示的页面 response.sendRedirect("index.jsp"); }
} |
web.xml:
<servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>com.myapp.servlets.ShowServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> |
我们通过以上代码,虚拟出一些数据,供页面的标签调用,在实际项目中,可改为由数据库读取。
<c:out>
index.jsp:
<%@page contentType="text/html;charset=gb2312"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <HTML> <HEAD> <TITLE>JSTL之C标签</TITLE> </HEAD> <BODY> <table border="1"> <c:forEach var="student" items="${allstudents}"> <tr> <td><c:out value="${student.sno}"/></td> <td><c:out value="${student.sname}"/></td> <td><c:out value="${student.ssex}"/></td> <td><c:out value="${student.deptno}" default="no deptno specified"/></td> </tr> </c:forEach> </table> </BODY> </HTML> |
<c:set>
index.jsp:
<%@page contentType="text/html;charset=gb2312"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <HTML> <HEAD> <TITLE>JSTL之C标签</TITLE> </HEAD> <BODY> <c:set var="studentTable" scope="session"> <table border="1"> <c:forEach var="student" items="${allstudents}"> <tr> <td><c:out value="${student.sno}"/></td> <td><c:out value="${student.sname}"/></td> <td><c:out value="${student.ssex}"/></td> <td><c:out value="${student.deptno}" default="no deptno specified"/></td> </tr> </c:forEach> </table> </c:set> <A HREF="show.jsp">前进</A> </BODY> </HTML> |
我们通过<c:set>标签,把一个可以显示的表格,保存到session对象的属性中,到下一页显示。
show.jsp:
<%@page contentType="text/html;charset=gb2312"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <HTML> <HEAD> <TITLE>JSTL之C标签</TITLE> </HEAD> <BODY> <c:out value="${studentTable}" escapeXml="false"/> </BODY> </HTML> |
注意这里的escapeXml="false"属性,如果为true,则会把HTML标签显示在网页里。
<c:if>
index.jsp:
<%@page contentType="text/html;charset=gb2312"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <HTML> <HEAD> <TITLE>JSTL之C标签</TITLE> </HEAD> <BODY> <c:forEach var="student" items="${allstudents}"> <c:if test="${student.deptno == 'd002'}"> ${student.sname}<br> </c:if> </c:forEach> </BODY> </HTML> |
<c:choose>
index.jsp:
<%@page contentType="text/html;charset=gb2312"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <HTML> <HEAD> <TITLE>JSTL之C标签</TITLE> </HEAD> <BODY> <c:forEach var="student" items="${allstudents}"> <c:choose> <c:when test="${student.deptno == 'd003'}"> <font color="blue"> </c:when> <c:when test="${student.deptno == 'd004'}"> <font color="red"> </c:when> <c:otherwise> <font color="green"> </c:otherwise> </c:choose> ${student.sname}</font><br> </c:forEach> </BODY> </HTML> |
<c:forEach>
index.jsp:
<%@page contentType="text/html;charset=UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <HTML> <HEAD> <TITLE>JSTL之C标签</TITLE> </HEAD> <BODY> <c:forEach var="i" begin="1" end="10"> ${i} • </c:forEach> </BODY> </HTML> |
以上是简单的定义变量,指定其变化范围,循环显示。
index.jsp:
<%@page contentType="text/html;charset=gb2312"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <HTML> <HEAD> <TITLE>JSTL之C标签</TITLE> </HEAD> <BODY> <c:forEach var="student" items="${allstudents}" begin="2" end="9" step="2"> <c:out value="${student.sno}"/><br> </c:forEach> </BODY> </HTML> |
这个例子与<c:out>的类似,只是在循环时可以指定起始位子和步长。