Java Server Pages[jsp] — java服务器页面 【包含有java程序的运行在服务器上的页面{HTML}】 页面 —【HTML】— 包含有java程序代码 — 在服务器上运行。 例如:
<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E"); String datetime=sdf.format(new Date()); %> <h1>当前系统时间:<% out.write(datetime); %></h1> </body> </html>JSP实际上就是html中包含java代码。用html标记表示静态效果,包含的java代码则处理动态数据。
格式:<%@指令名称 属性1=”属性值1”…%> 作用:在jsp页面中声明的一些由浏览器执行的附加信息. 常用指令:page指令 / include指令 / taglib指令
1. language=“java” — 指定语言 2. contentType=“text/html; charset=utf-8”:设置当前jsp页面的内容类型 3. pageEncoding=“utf-8”:设置当前jsp页面的字符编码 4. import=“java.util.Date”:导入java类库的依赖包【可以出现多个】 5. isELIgnored=“false” — 是否启用EL表达式语言 “${表达式}”,false可用,true不可以 6. isErrorPage=“false” — 是否是一个错误页面,如果是那么就可以使用exception的JSP内置对象 例如:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>page指令--常见属性</h1> <h2>1.language="java"---指定语言</h2> <h2>2.contentType="text/html; charset=utf-8":设置当前jsp页面的内容类型</h2> <h2>3.pageEncoding="utf-8":设置当前jsp页面的字符编码</h2> <h2>4.import="java.util.Date":导入java类库的依赖包【可以出现多个】</h2> <h3><%@ page import="java.text.SimpleDateFormat"<br> import="java.util.Date"%></h3> <hr> <h3><%@ page import="java.text.SimpleDateFormat"%></h3> <h3><%@ page import="java.util.Date"%></h3> <h2>5.isELIgnored="false"---是否启用EL表达式语言 “${表达式}”,false可用,true不可以</h2> <h3>123+234=${123+234}</h3> <h2>6.isErrorPage="false"---是否是一个错误页面,如果是那么就可以使用exception的JSP内置对象</h2> <% exception.printStackTrace(); %> </body> </html>例如:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>include指令--常见属性</h1> <h2>include指令--包含其他的资源进入本jsp页面</h2> <h2>file="test.txt":指定被包含的其他的资源的路径</h2> 包含文本信息:<%@include file="test.txt" %><br> 包含html信息:<%@include file="mytest.html" %> 包含jsp信息:<%@include file="test1.jsp" %> </body> </html>格式:<% java程序 %> 作用:嵌套在html中的java程序【提供动态数据】 例如:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.text.SimpleDateFormat" import="java.util.Date"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E"); String datetime=sdf.format(new Date()); %> <h1>当前系统时间:<% out.write(datetime); %></h1> </body> </html>格式:<%= 表达式/变量/方法 %> 作用:计算某种表达式的结果,或者变量/方法的结果。
格式:<%! 类/变量/方法 %> 作用:在jsp页面中定义一些自己需要的java变量/方法/类 例如:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>jsp表达式</h1> <h2>计算某种表达式的结果,或者变量/方法的结果。</h2> <h2>算术表达式 123+234=<%= 123+234 %></h2> <h2>关系表达式 123>234=<%= 123>234 %></h2> <h2>逻辑表达式 (123>234) || (123 < 234)===<%= (123 > 234) || (123 < 234) %></h2> <h1>jsp声明</h1> <h2>在jsp页面中定义一些自己需要的java变量/方法/类</h2> <%! String name="zhangsan"; public String getInfo(){ return "hello,wangxing"; } %> <h2>声明中的变量name==<%= name %></h2> <h2>声明中的变量getInfo()==<%= getInfo() %></h2> </body> </html>格式:<jsp:动作名称 动作属性…></jsp:动作名称> 作用:将一些常用的java程序封装,方便调用
include的jsp动作
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>jsp动作</h1> <h2>将一些常用的java程序封装,方便调用</h2> <h2>include的jsp动作--包含其他的资源进入本jsp页面</h2> 包含文本信息:<jsp:include page="test.txt"></jsp:include><br> 包含html信息:<jsp:include page="mytest.html"></jsp:include> 包含jsp信息:<jsp:include page="test1.jsp"></jsp:include> </body> </html>forward的jsp动作
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>jsp动作</h1> <h2>forward的jsp动作--跳转到其他的资源</h2> <jsp:forward page="mytest.html"></jsp:forward> </body> </html>include指令与include动作有什么区别?
include指令jsp:include动作语法格式<%@ include file=”…”%><jsp:include page=”…”>发生作用的时间页面转换期间请求期间包含的内容文件的实际内容页面的输出转换成的Servlet主页面和包含页面转换为一个Servlet主页面和包含页面转换为独立的Servlet影响主页面可以不可以发生更改时是否需要显式更改主页面需要不需要编译时间较慢-资源必须被解析较快执行时间稍快较慢-每次资源必须被解析灵活性较差-页面名称固定更好-页面可以动态指定jsp本质上就是一个Servlet程序。 Jsp在执行的时候会被转换成Servlet程序。 保存位置 F:\20200728\javawebworkspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\TestJSP1\org\apache\jsp
public final class test5_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { org.apache.jasper.runtime.HttpJspBase继承过javax.servlet.http.HttpServlet当我们请求一个jsp页面的时候,服务器收到jsp页面的请求以后,会先将被访问的jsp文件转换成Servlet文件,继续编译转换以后的Servlet文件得到Servlet文件对应的字节码文件,接着执行这个编译以后的字节码文件,将运行结果封装到响应对象中,返回给客户端浏览器。 缺点:第一次访问jsp页面会比较慢一些。
