JSP 指令标记
常用的Java Server page语言指令有page 和 include指令,而JSP指令用于h转换阶段,主要提供整个JSP 页面的相关信息,影响由JSP生成的Servlet(应用连接器)整体结构。
page
page指令可以用于标记整个Java Server pages页面的一些属性,如:
1 2
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
| ID |
DA |
FA |
| language |
设置当前页面开发语言 |
目前仅仅可以使用java |
| contentType |
用于应用程序来打开的方式类型 |
contentType |
|
text/html |
HTML网页形式 |
|
text/plain |
普通文本 |
|
application/pdf |
PDF文档 |
|
application/msword |
Word 应用程序 |
|
application/vnd.ms-powerpowerpoint |
PowerPoint 应用程序 |
|
image/jpeg |
JPEG 图像 |
|
image/png |
PNG 图像 |
|
image/gif |
GIF 图像 |
| pageEncoding |
charset 服务器发送给客户浏览器时所见到的网页内容编码 |
pageEncoding是指JSP文件自身存储时的所用编码 |
| charset |
charset 和pageEncoding 一样,都是网页内容编码,如果pageEncoding不存在则使用charset |
如果都不存在这使用默认编码ISO-8859-1 |
include
include指令可以用于当一个网站中有多个Java Server pages页面需要显示同样的信息时,为了减少代码量和易于阅读可以使用include指令进行处理。Inculde指令的作用就是将一个jsp文件、Html网页文件或其他文本静态嵌入到当前jsp页面之中。
NewFile.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% out.println("NewFile.jsp"); %> <br> <%@ include file="NewFile1.jsp"%> </body> </html>
|
NewFile1.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% out.println("Hello,world!"); %> </body> </html>
|
JSP 动作标记
JSP 指令标记和动作标记的区别主要是指令标记以<%@开始,的指令。而JSP动作标记则是由Java Server pages引起在请求阶段时处理的,而不是和指令一样在翻译为Servlet(应用连接器)阶段处理的。
在通常的情况之下,动作标记主要有 include、forward、param、useBean、getProperty、setProperty、useBean、getProperty、setProperty等
include 动作标记
include动作标记的主要作用就是将JSP文件、HTML文件或其他文本文件动态嵌入到当前的Java Server Pages页面之中
1
| <jsp:include page="file"/>
|
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% out.println("NewFile.jsp"); %> <hr> <jsp:include page="NewFile1.jsp"/> </body> </html>
|
NewFile.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% out.println("NewFile.jsp"); %> <hr> <jsp:include page="NewFile1.jsp"/> </body> </html>
|
NewFile1.jsp
page language1 2 3 4 5 6 7 8 9 10 11 12 13
| pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% out.println("Hello,world!"); %> </body> </html>
|
forward 动作标记
forward 动作标记的作用是从该标记出现出停止当前的Java server pages 页面的继续执行并转向指定的forward动作标记中page属性值的指定JSP页面。
判断数值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% int min = 10; if (min == 10) { out.println("min = 10"); %> <jsp:forward page="NewFile1.jsp"/> <% } else { out.println("min != 10"); } %> <jsp:forward page="NewFile2.jsp"/> </body> </html>
|
⬅️ Go back