el表达式,c标签的使用

el表达式的使用

  • 学生类
public class Student {
	
	int id;
	String name;
	String sex;
	String birth;
	
	public Student() {}
	
	public Student(int id, String name, String sex, String birth) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.birth = birth;
	}
	
	// el 表达式取实体类的属性,需要设置get方法
	public int getId(){
		return id;
	}
	
	public String getName(){
		return name;
	}
	
	public String getSex() {
		return sex;
	}
	
	public String getBirth() {
		return birth;
	}
		
}

  • 取域对象request,session,application的值

  • 取学生对象的值

  • 取集合对象的值

  • servlet doService方法如下

	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 请求编码
		req.setCharacterEncoding("UTF-8");
		// 响应编码
		resp.setContentType("text/html;charset=UTF-8");
		
		// 设置request, session, application域对象的值
		req.setAttribute("req", "req1");
		req.getSession().setAttribute("session", "session1");
		req.getServletContext().setAttribute("app", "application1");
		
		// 设置学生对象
		req.setAttribute("stu", new Student(1, "jack", "男", "2020-2-2"));
		
		// 设置集合对象
		List<Student> list = new ArrayList<Student>();
		list.add(new Student(2, "r", "女", "2020-3-4"));
		list.add(new Student(3, "y", "女", "2020-3-4"));
		list.add(new Student(4, "e", "女", "2020-3-4"));

		req.setAttribute("list", list);
		// 请求转发到index.jsp页面
		req.getRequestDispatcher("index.jsp").forward(req, resp);
	}
  • jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- jsp/jstl/core --%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <h1>req, session, application取值</h1>
    <%-- 取域对象的值 --%>
    ${ req }
    
    ${ session }
    
    ${ app }
    <hr/>
    
    <%-- 判断空 --%>
    ${ empty 1 }
    
    <c:if test="true">
    	<br/>i am true!
    </c:if>
    <br/>
    学生:
    ${ stu } <%-- 学生对象 --%>
   	${ stu.name } 
   	${ stu.sex } 
   	${ stu.birth } 

   
    <br/>
     <%-- 取list的值 --%>
    <c:forEach items="${ list }" var="stu">
    	${ stu.id }
    	${ stu.name }
    	${ stu.sex } 
     	${ stu.birth } <br/>
    </c:forEach>
    
        
     </body>
</html>

el表达式,c标签的使用_第1张图片

  • 超链接参数取值
<a href="index.jsp?row_id=1">c标签取get</a>
// index.jsp页面 el表达式取值
${ param.row_id }

c标签的使用,取的别名c

  • 导入jstl库,别导错了,还有一个标签库跟下面的很像。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <c:if test="${ 1==1 }">
    	1=1
    </c:if>
    
    <c:choose>
    	<c:when test="true">true</c:when>
    	<c:when test="false">false</c:when>
    	<c:otherwise>false</c:otherwise>
    </c:choose>
    
    <c:forEach begin="1" end="5" step="1" var="i">
    	${ i }
    </c:forEach>

el表达式,c标签的使用_第2张图片

你可能感兴趣的:(#,servlet)