1. JSP
- Java Server Pages
Sun公司倡导,其他公司参与制定的动态网页的技术标准
特点:跨平台、易维护、易管理
【实质】:嵌入Java代码的HTML页面
2. JSP 执行过程
- 翻译:JSP转换成Java,Tomcat中work目录下文件增加。又叫转换、转译
- 编译:由Java生成class。
- 运行:直接运行字节码文件(class文件)
3. JSP 生命周期
-
初始化
public void _jspInit() {}
-
服务
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {}
-
销毁
public void _jspDestroy() {}
源代码
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/8.0.47
* Generated at: 2018-06-12 07:43:24 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.Date;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = new java.util.HashSet<>();
_jspx_imports_classes.add("java.util.Date");
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
//初始化
public void _jspInit() {
}
//销毁
public void _jspDestroy() {
}
//服务
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t");
out.print(new Date() );
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
4. JSP元素
4.1 静态内容
4.2 注释
1. 浏览器查看源文件可见
<!-- 这是注释 -->
2. 浏览器查看源文件不可见
<%-- 这也是注释 --%>
4.3 指令
以<%@ 开始 , 以 %>结束
(1) Page指令
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
-
language=“java” –- 指明页面的脚本语言
-
import=“java.util.*” – 导入Java包
-
contentType=“text/html;charset=gbk” – 设置编码方式
-
pageEncoding=“utf-8”
-
errorPage = ‘错误页面的URL’
-
isErrorPage = ‘true’ 设置为true之后,可以在错误页面上输出异常信息
<%=exception%>
(2) Include指令
<%@include file="top.jsp" %>
-
在页面的任何地方引入其他页面
-
只有一个file属性
<body> <div id="container"> <%@include file="top.jsp" %> <hr/> <div><br>新闻列表. <br></div> <hr/> <%@include file="foot.jsp" %> </div> </body>
(3) Taglib指令
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- 在页面中引入标签库文件(JSTL、Structs标签、自定义标签)
prefix=“c” – 前缀(别名)
uri=http://java.sun.com/jsp/jstl/core – 标签库文件的引用路径
4.4 表达式
- 以<%= 开始 , 以 %>结束
- 负责在页面上输出内容
- 表达式中不能有 “;”
<%=al.get(i)%>
4.5 小脚本
- 以<% 开始 , 以 %>结束
- 在JSP页面中嵌入Java代码
- 每行语句以 “;” 结束,括号匹配 – 存在于方法中
<%
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(123);
al.add(654);
al.add(74398);
al.add(3);
al.add(5);
%>
4.6 声明
- 以<%! 开始 , 以 %>结束
- 可以设置全局变量和方法
<%!
int count = 0;
public int getCount(){
return count;
}
%>
4.7 标准动作
<jsp:useBean id=""></jsp:useBean>
<jsp:include page="URL"></jsp:include>