首页 > 代码库 > jsp
jsp
一、jsp脚本元素
标签 | 语法 | 用途 |
声明标签 | <%!变量或方法声明%> | 声明jsp内所所用的全局变量或方法 |
表达式 | <%=%> | 输出Java中的变量或者表达式的值到页面上 |
程序代码标签 | <% 程序代码 %> | 编写Java程序代码,但不可以定义方法 |
二、jsp页面注释
jsp注释:<%-- 注释 --%>,转换阶段消失(即在jsp转换为servlet阶段),只能被开发人员看到。
Java注释://、/*注释*/、/**注释*/,编译阶段消失(即jsp转换成的servlet,然后转换的servlet编译成class文件的时候消失)。
HTML注释:<!-- 注释 -->,不会消失,在页面中也能看到。
三、JSP指令
功能:
用于指定jsp执行某些步骤。
用于指示jsp表现特定的行为。
语法格式:
<%@ 指令 [属性="属性值"]%>
分类:
page指令
include指令
tablib指令
3.1 page指令
page属性包含在"<%@"和"%>"之间。
这些属性可以单独使用,也可以几个或多个同时使用。
page指令用来定义jsp文件的全局属性。
在jsp页面中,只有import可以出现多次,其他属性都只能出现一次。
3.2 include指令
include指令的语法格式如下:
<%@ include file="filename" %>
include指令的作用是在jsp页面中静态包含一个文件,同时由jsp解析包含的文件内容。
静态包含的含义:
file不能为一变量。
不可以在file所指定的文件后接收任何参数。
包含的是目标文件的源码;包含过来,一起翻译。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>头部</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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> 头部 </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>菜单</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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> 菜单 </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>脚本</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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> 脚本 </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>主体</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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> <%@ include file="/jsp/head.jsp" %> <%@ include file="/jsp/menu.jsp" %> 主体 <%@ include file="/jsp/foot.jsp" %> </body> </html>
访问之后,查看源代码就可以看到。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘head.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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> <script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=http://www.mamicode.com/c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head> <body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-7" data-genuitec-path="/day11/WebRoot/jsp/head.jsp"> 头部 </body> </html> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘menu.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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> <script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=http://www.mamicode.com/c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head> <body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-9" data-genuitec-path="/day11/WebRoot/jsp/menu.jsp"> 菜单 </body> </html> 主体 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘foot.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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> <script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=http://www.mamicode.com/c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head> <body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-10" data-genuitec-path="/day11/WebRoot/jsp/foot.jsp"> 脚本 </body> </html> </body> </html>
这样,就能体现上面的那句话了吧(包含的是目标文件的源码;包含过来,一起翻译)。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 头部
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 菜单
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 脚本
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>主体</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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> <%@ include file="/jsp/head.jsp" %> <%@ include file="/jsp/menu.jsp" %> 主体 <%@ include file="/jsp/foot.jsp" %> </body> </html>
访问之后,查看源代码就可以看到。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>主体</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"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> <script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"8123",secure:"8124"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=http://www.mamicode.com/c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head> <body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-8" data-genuitec-path="/day11/WebRoot/jsp/body.jsp"> 头部 菜单 主体 脚本 </body> </html>
3.3 taglib指令
taglib指令用于在jsp页面中导入标签库。
常用属性:
uri 标签文件的URL地址
prefix 标签组的命名空间前缀
四、jsp内置对象
内置对象 | 代表内容 | 范围 |
request | 触发服务应用的请求 | request |
response | 对请求的响应 | page |
sesson | 为请求的客户创建的session对象 | session |
application | 从servlet配置对象获得servlet上下文 | application |
out | 向输入流写入内容的对象 | page |
pageContext | 本jsp页面的上下文 | page |
page | 实现处理本也当前请求的类的实例 | page |
config | 本jsp的ServletConfig | page |
exception | 表示jsp页面运行时产生的异常 | page |
内置对象对应的类型
request | HttpServletRequest |
response | HttpServletResponse |
session | HttpSession |
application | ServletContext |
config | ServletConfig |
page | Object |
pageContext | PageContext |
exception | Throwable |
out | JspWriter |
五、jsp的四种数据范围
对象 | 范围 |
pageContext | page范围 |
request | 请求范围 |
session | 会话范围 |
application | 应用范围 |
六、jsp常用标签
<jsp:include>文件包含 动态包含
<jsp:forward>在jsp页面完成转发
<jsp:param> 在请求和转发之前携带参数
七、静态包含和动态包含的区别?
本文出自 “11831428” 博客,请务必保留此出处http://11841428.blog.51cto.com/11831428/1907747
jsp