jstl是el表达式的扩展
使用jstl需要添加jar包
package com.test.servlet;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.test.pojo.Student;/*** Servlet implementation class TestJstlServlet*/
@WebServlet("/TestJstlServlet")
public class TestJstlServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public TestJstlServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//response.getWriter().append("Served at: ").append(request.getContextPath());request.setAttribute("store_num", 50);request.setAttribute("price", 500);List<Student> list=new ArrayList<Student>();list.add(new Student(1,"daimenglaoshi","jisuanji","0101"));list.add(new Student(2,"daimenglaoshi2","yingyu","0101"));list.add(new Student(3,"zhangsan","jisuanji","0102"));request.setAttribute("student_list", list);Map<String, String> map=new HashMap<String,String>();map.put("name", "zhangsan");map.put("age","23");request.setAttribute("map",map);request.getRequestDispatcher("TestJstl.jsp").forward(request, response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${store_num>10}">库存充足
</c:if><c:if test="${store_num<=10}">库存不足
</c:if><c:choose><c:when test="${price>1000}"> 太贵</c:when><c:when test="${price>500}"> 买得起</c:when><c:otherwise>太便宜了 不买了</c:otherwise>
</c:choose><c:forEach var="student" items="${student_list}" >${student.sname}<br/>
</c:forEach><c:forEach var="item" items="${map}">${item.key} ${item.value}<br/></c:forEach></body>
</html>
package com.test.pojo;//琛ㄦ槧灏勭被
public class Student {private int sid;public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getMajor() {return major;}public void setMajor(String major) {this.major = major;}public String getStu_class() {return stu_class;}public void setStu_class(String stu_class) {this.stu_class = stu_class;}private String sname;private String major;private String stu_class;public Student(){}public Student(int sid,String sname,String major,String stu_class){this.sid=sid;this.sname=sname;this.major=major;this.stu_class=stu_class;}}