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) 对象关 ...
随机推荐
- OpenCV 中CV_IMAGE_ELEM 的使用
CV_IMAGE_ELEM 是一个宏函数,基本形式: CV_IMAGE_ELEM(image,elemtype,row,col) 其中,image为指针数组,elemtype为数据的存取类型,row为 ...
- 基于vue脚手架的项目打包上线(发布)方法和误区
最近要把vue脚手架开发的一个项目上线,只知道vue脚手架是基于node的服务端项目,那么只需要 npm run dev 就可以轻松启动整个项目,当我想当然的给服务器配置合适的node环境(这里也遇到 ...
- [开源]JSON文本格式化工具(简码万能助手开源扩展程序)
现在的网站大多都是使用json进行API式前后端数据交互, 有时抓包得到的是一串没格式化的JSON文本, 不太方便分析, 所以我自行写了个开源扩展程序, 可以方便地格式化JSON文本. 当然,你也 ...
- 闲谈Hybrid
前言 当经常需要更换样式,产品迭代,那么我们应该考虑hybrid混合开发,上层使用Html&Css&JS做业务开发,底层透明化.上层多多样化,这种场景非常有利于前端介入,非常适合业务快 ...
- 500. Keyboard Row (5月26日)
解答 class Solution { public: vector<string> findWords(vector<string>& words) { vector ...
- 竞赛题解 - NOIP2018 旅行
\(\mathcal {NOIP2018} 旅行 - 竞赛题解\) 坑还得一层一层的填 填到Day2T1了 洛谷 P5022 题目 (以下copy自洛谷,有删减/修改 (●ˇ∀ˇ●)) 题目描述 小 ...
- Mybartis逆向工程
Mybartis逆向工程 0.创建工程项目,切记莫用中文,亲测在运行时报错 1.Pom文件,使用mybatis-generator插件 <?xml version="1.0" ...
- jar包导入本地maven库的操作
pom文件配置格式: <dependency> <groupId>A</groupId> <artifactId>B</artifactId> ...
- vue-cli3 vue.config.js 配置
// cli_api配置地址 https://cli.vuejs.org/zh/config/ module.exports = { baseUrl: './', // 部署应用包时的基本 URL o ...
- 集合,ArrayList练习
import java.util.ArrayList; import java.util.Iterator; public class ArrayListTest { public static vo ...