jsp

1、jsp脚本元素javascript

标签 语法 用途
声明标签 <%!变量或方法声明%> 声明jsp内所所用的全局变量或方法
表达式 <%=%> 输出Java中的变量或者表达式的值到页面上
程序代码标签 <% 程序代码 %> 编写Java程序代码,但不能够定义方法


2、jsp页面注释css


jsp注释:<%-- 注释 --%>,转换阶段消失(即在jsp转换为servlet阶段),只能被开发人员看到。html


Java注释://、/*注释*/、/**注释*/,编译阶段消失(即jsp转换成的servlet,而后转换的servlet编译成class文件的时候消失)。java


HTML注释:<!-- 注释 -->,不会消失,在页面中也能看到。session


3、JSP指令
app

功能:jsp

    用于指定jsp执行某些步骤。
async

    用于指示jsp表现特定的行为。
ide

语法格式:ui

    <%@ 指令 [属性="属性值"]%>

分类:

    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="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="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="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="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="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=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="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=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="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=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="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="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=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>

wKiom1jL07nhQlk4AABcGEURo-o384.png

3.3    taglib指令

    taglib指令用于在jsp页面中导入标签库。

    经常使用属性:

        uri        标签文件的URL地址

        prefix   标签组的命名空间前缀


4、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


5、jsp的四种数据范围

对象 范围
pageContext page范围
request 请求范围
session 会话范围
application 应用范围


6、jsp经常使用标签

<jsp:include>文件包含 动态包含
<jsp:forward>在jsp页面完成转发
<jsp:param> 在请求和转发以前携带参数


7、静态包含和动态包含的区别?

wKioL1jL14SDgeceAAHgJz7ePeY663.png

相关文章
相关标签/搜索