Java开发工程师(Web方向) - 02.Servlet技术 - 第4章.JSP
第4章--JSP
JSP
JSP(Java Server Pages) - 中文名:Java服务器页面
动态网页技术标准
JSP = Html + Java + JSP tags
在服务器端执行,返回给客户端一个html文本
可以看作简化的Servlet
JSP vs Servlet:
侧重点:JSP页面表示--视图;Servlet逻辑控制
内置对象:JSP有内置对象供选择;Servlet无
本质:JSP只需完成输出到客户端的内容
JSP处理流程:
浏览器向服务器发送JSP请求,请求JSP文件,
JSP容器(常用tomcat)从磁盘中载入对应JSP文件,并转化成对应的Servlet文件
(简单将所有模板文件改用print语句输出,将所有JSP元素转换成Java代码)
JSP容器将Servlet编译成.class文件
将原始请求传递给Servlet容器
web组件调用Servlet容器执行Servlet实例
Servlet输出HTML页面,并将其嵌入到HttpResponse交给web服务器
web服务器以静态资源的方式将HttpResponse返回浏览器
JSP基本语法:
主要包含JSP元素(被JSP引擎处理的部分)和模板数据(JSP引擎不处理的部分,如html内容):
模板元素:静态内容
JSP元素:指令、注释、表达式、声明、脚本
JSP声明:参变量的声明--一个声明语句可以声明一个或多个变量或方法,供后面的java代码使用
<%!declaration; [declaration;] + ... %>
i.e. <%! int a, b, c; %>
JSP表达式:可以包含任何符合Java语言规范的表达式,但是不能使用分号来结束表达式
<% = 表达式 %>
i.e. <p>Today's date: <%= (new java.util.Date()).toLocaleString() %> </p>
JSP脚本:可以包含任意量的Java语句、变量、方法或表达式
<% 代码片段 %>
i.e. <% out.println("Your IP address is " + request.getRemoteAddr()); %>
JSP注释:代码注释/将某段代码注释掉
<%-- 注释在这里 --%>
JSP指令:用于设置页面相关的属性,并不直接产生输出;告诉JSP容器如何处理其余的JSP页面
page指令:设置静态属性--定义页面的依赖属性,比如脚本语言、error页面、缓存需求等。
include指令:向当前页面插入一个静态文件(如JSP/html/文本/java程序)的内容。
用于通知JSP容器将另外一个文件包含到当前文件中,效果为内容的复制粘贴
taglib指令:引入标签库的定义
JSP内置对象:JSP容器为每个页面提供的java对象,开发者可以直接使用这些对象而不用显式声明(也成为预定义变量)
目前支持9个内置对象


i.e.
new - Other - JSP File - webapp下hello.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>
</body>
</html>
第一行page指令:language/contentType/pageEncoding...
尝试加入:
注释:<!-- this is jsp comment -->
声明:<%!String name; %>
表达式:request uri is <%=request.getRequestURI() %>
脚本:
<%
name = "abc";
out.println("name is " + name);
%>
完整代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- this is jsp comment -->
<%!String name; %>
request uri is <%=request.getRequestURI() %>
<br/>
<%
name = "abc";
out.println("name is " + name); %>
</body>
</html>
之前提到JSP容器会将JSP转换成Servlet文件:
路径:Tomcat目录/work/Catalina/localhost/项目文件夹
在JSP执行之前,该文件夹下为空;
执行之后:/org/apache/jsp/路径下产生两个文件 hello_jsp.class & hello_jsp.class
hello_jsp.java
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/9.0.0.M22
* Generated at: 2017-08-10 06:48:36 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.*; public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports { String name; ......
......
...... 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=ISO-8859-1");
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("<!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=ISO-8859-1\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("<!-- this is jsp comment -->\r\n");
out.write("\r\n");
out.write("request uri is ");
out.print(request.getRequestURI() );
out.write("\r\n");
out.write("<br/>\r\n"); name = "abc";
out.println("name is " + name); 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);
}
}
}
_jspService() 相当于Servlet中的service()
Java开发工程师(Web方向) - 02.Servlet技术 - 第4章.JSP的更多相关文章
- Java开发工程师(Web方向) - 02.Servlet技术 - 第3章.Servlet应用
第3章.Servlet应用 转发与重定向 转发:浏览器发送资源请求到ServletA后,ServletA传递请求给ServletB,ServletB生成响应后返回给浏览器. 请求转发:forward: ...
- Java开发工程师(Web方向) - 02.Servlet技术 - 第1章.Servlet
第1章--Servlet Servlet简介 Servlet应用于? 浏览器发出HTTP请求,服务器接收请求后返回响应给浏览器. 接收请求后到返回响应之间: 服务器将请求对象转交给Servlet容器 ...
- Java开发工程师(Web方向) - 02.Servlet技术 - 第2章.Cookie与Session
第2章--Cookie与Session Cookie与Session 浏览器输入地址--HTTP请求--Servlet--HTTP响应--浏览器接收 会话(session):打开浏览器,打开一系列页面 ...
- Java开发工程师(Web方向) - 02.Servlet技术 - 期末考试
Servlet课程考试 Servlet课程考试 Servlet课程考试 总分:55分 限定时间:120分钟 进入考试 答案已成功提交!请耐心等待成绩公布 Servlet课程考试: 1(12分) 简单谈 ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第3章.AOP技术
第3章--AOP技术 Spring框架 - AOP概述 笔记https://my.oschina.net/hava/blog/758873Spring框架 - AOP使用 笔记https://my.o ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第2章.IoC容器
第2章.IoC容器 IoC容器概述 abstract: 介绍IoC和bean的用处和使用 IoC容器处于整个Spring框架中比较核心的位置:Core Container: Beans, Core, ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第1章.Spring概述
第1章.Spring概述 Spring概述 The Spring Framework is a lightweight solution and a potential one-stop-shop f ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第5章.Web框架
第5章--Web框架 Web框架概述 Web框架单元测验 本次得分为:13.50/15.00, 本次测试的提交时间为:2017-09-25 1单选(2分) 关于Spring MVC中Dispatche ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第4章.数据访问
第4章--数据访问 Spring JDBC DAO (Data Access Object) 实现数据访问相关接口(接口和实现分离) ORM (Object Relation Mapping) 对象关 ...
随机推荐
- Mongoose 对象的特殊性
一.偶遇难题 在最近使用Mongoose的时候,遇到这样一个问题: 我从DB中查询出来一个对象,比如是Book,这个对象我想在返回时,给他附加一个字段,比如是字段A,正常来说,JS你只需要Book.A ...
- 修改office文档修改日期
修改“创建日期”可采用如下方法: 首先把系统日期调整到您所希望的时间,然后到MS-DOS方式下,对该文件输入如下命令:COPY /B filename +,, (一个加号.两个逗号),当询问您是否确认 ...
- Razor
什么是Razor 1.Razor是一种将基于服务器的代码添加到网页中的标记语法 2.Razor具有传统ASP.NET标记功能 3.Razor是一种服务器端的标记语法 4.Razor同时支持C#和VB ...
- 使用RMAN增量备份处理Dataguard因归档丢失造成的gap
场景: 备库执行日志应用出现如下报错: Thu Mar 29 11:21:45 2018FAL[client]: Failed to request gap sequence GAP - thread ...
- 学习笔记 - Manacher算法
Manacher算法 - 学习笔记 是从最近Codeforces的一场比赛了解到这个算法的~ 非常新奇,毕竟是第一次听说 \(O(n)\) 的回文串算法 我在 vjudge 上开了一个[练习],有兴趣 ...
- 【补】英语对IT工作者的重要性
浅谈程序员的英语学习 作为在中国工作的程序员,不懂得英语似乎也不妨碍找到好工作,升职加薪.但程序员这个工种则稍有不同,因为程序,尤其是高级语言,基本上都是由英语和数字表达式构成的.英语对于程序员十 ...
- 谈个人对avascript面向对象的理解
javascript,不但是javascript或者是别的语音,大多数都有一句经典的话:一切皆对象. 下面谈谈我个人对面向对象的理解,为什么要用面向对象来写js,这话我思考了很久,最后得出的结论就是: ...
- js数组去重(多种写法)
最基本的写法 使用indexOf() var arr = [1,1,5,77,32,54,2,4,5,2,2,4,52,2,2,2,2,2] //比较常规的语法使用indexOf来判断是否已经存在 g ...
- linux操作之软件安装(二)(源码安装)
源码安装 linux上的软件大部分都是c语言开发的 , 那么安装需要gcc编译程序才可以进行源码安装. yum install -y gcc #先安装gcc 安装源码需要三个步骤 1) ./confi ...
- Flume:source和sink
Flume – 初识flume.source和sink 目录基本概念常用源 Source常用sink 基本概念 什么叫flume? 分布式,可靠的大量日志收集.聚合和移动工具. events ...