include指令
在JSP中,可以使用include指令来包含其他jsp文件
include的指令的语法如下:<%@ include file="文件路径"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>这是一个表头</h1> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%@ include file="header.jsp" %> <h1>这是一个主体部分</h1> </body> </html>
page指令 主要用来设置页面的属性,比如导入包、指明输出的页面类型、控制session等
taglib指令 用来引用标签库并设置标签库的前缀
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% List<String> list = new ArrayList<>(); list.add("AA"); list.add("BB"); request.getSession().setAttribute("list", list); %> <c:forEach var="str" items="${list}"> <table> <tr> <td>${str}</td> </tr> </table> </c:forEach> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("pageContext", "pageContext作用域"); request.setAttribute("request", "request作用域"); session.setAttribute("session", "session作用域"); application.setAttribute("application", "application作用域"); request.getRequestDispatcher("target.jsp").forward(request, response); %> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% out.println(pageContext.getAttribute("pageContext")+"<br>"); out.println(request.getAttribute("request")+"<br>"); out.println(session.getAttribute("session")+"<br>"); out.println(application.getAttribute("application")); %> </body> </html>
JSP中的六个动作:
JSP共有以下9大内置对象:
原文:https://www.cnblogs.com/fengfuwanliu/p/10523416.html