一、JSP是什么?
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代码则处理动态数据。
二、JSP页面中的元素?
1. 指令
格式:<%@指令名称 属性1=”属性值1”…%> 作用:在jsp页面中声明的一些由浏览器执行的附加信息. 常用指令:page指令 / include指令 / taglib指令
page指令—常见属性
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> <h2>5.isELIgnored="false"---是否启用EL表达式语言 “${表达式}”,false可用,true不可以</h2> <h3>450+500=${450+500}</h3> <h2>6.isErrorPage = "false"---是否是一个错误页面,如果是那么就可以使用exception的内置对象</h2> <% //exception.printStackTrace(); %> </body> </html>执行结果:
include指令[包含其他的资源进入本jsp页面]—常见属性
例如:
<%@ 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> <h1>include指令--常见属性</h1> <h2>include指令--包含其他的资源进入本jsp页面</h2> <h2>file="test.txt":指定被包含的其他的资源的路径</h2> 包含文本信息:<%@include file="test.txt" %><br> 包含html信息:<%@include file="mytest.html" %> </body> </html>执行结果:
taglib指令–在jsp页面中使用JSTL【jsp的标准标签库】标签的时候,用来导入标签库 【这个指令暂时不处理,我们在后面学习JSTL时候,会详细介绍】 2.Java代码片段
格式:<% 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>执行结果:
3.jsp表达式
格式:<%= 表达式/变量/方法 %> 作用:计算某种表达式的结果,或者变量/方法的结果。
4.jsp声明
格式:<%! 类/变量/方法 %> 作用:在jsp页面中定义一些自己需要的java变量/方法/类
例如:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false"%> <!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>算术表达式 456+236=<%=456+236 %></h2> <h2>关系表达式 553>356=<%=553>356 %></h2> <h2>逻辑表达式(523>256) || (652<236) =<%=(523>256) || (652<236) %></h2> <h1>jsp声明</h1> <h2>在jsp页面中定义一些自己需要的java变量/方法/类</h2> <%! String name = "李四"; public String getInfo(){ return "Hello world"; } %> <h2>声明中的变量name=<%=name %></h2> <h2>声明中的方法getInfo()=<%=getInfo() %></h2> </body> </html>5.jsp动作 格式:<jsp:动作名称 动作属性…></jsp:动作名称> 作用:将一些常用的java程序封装,方便调用
<%@ 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> <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> <h1>forward的jsp动作</h1> <h2>forward的jsp动作--跳转到其他的资源</h2> <jsp:forward page="mytest.html"></jsp:forward> </body> </html>执行结果: forward的jsp动作 三、include指令[<%@include file=”” %>]与include动作[<jsp:include page=""></jsp:include>]有什么区别?
include指令include动作[<%@include file=”” %><jsp:include page=""></jsp:include>页面转换编译时就已经被激活在执行时请求了才被激活静态包含动态包含能在两个文件之间传递参数不能在两个文件之间传递参数执行时不需编译,速度快需要加载执行,速度慢通过file属性指定被包含的文件,放在页面的顶部,file属性不支持任何的表达式通过page属性来指定被包含的文件的,page属性支持jsp表达式四、JSP页面的执行过程? 1.jsp本质上就是一个Servlet程序。 Jsp在执行的时候会被转换成Servlet程序。 保存位置 E:\网星\java\eclipse javaweb_workspace.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页面会比较慢一些。
四、Servlet与JSP的区别?
ServletJSP控制逻辑侧重视图没有内置对象有内置对象直接运行jsp经编译后就变成了Servlet才可运行