第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的更多相关文章

  1. Java开发工程师(Web方向) - 02.Servlet技术 - 第3章.Servlet应用

    第3章.Servlet应用 转发与重定向 转发:浏览器发送资源请求到ServletA后,ServletA传递请求给ServletB,ServletB生成响应后返回给浏览器. 请求转发:forward: ...

  2. Java开发工程师(Web方向) - 02.Servlet技术 - 第1章.Servlet

    第1章--Servlet Servlet简介 Servlet应用于? 浏览器发出HTTP请求,服务器接收请求后返回响应给浏览器. 接收请求后到返回响应之间: 服务器将请求对象转交给Servlet容器 ...

  3. Java开发工程师(Web方向) - 02.Servlet技术 - 第2章.Cookie与Session

    第2章--Cookie与Session Cookie与Session 浏览器输入地址--HTTP请求--Servlet--HTTP响应--浏览器接收 会话(session):打开浏览器,打开一系列页面 ...

  4. Java开发工程师(Web方向) - 02.Servlet技术 - 期末考试

    Servlet课程考试 Servlet课程考试 Servlet课程考试 总分:55分 限定时间:120分钟 进入考试 答案已成功提交!请耐心等待成绩公布 Servlet课程考试: 1(12分) 简单谈 ...

  5. Java开发工程师(Web方向) - 04.Spring框架 - 第3章.AOP技术

    第3章--AOP技术 Spring框架 - AOP概述 笔记https://my.oschina.net/hava/blog/758873Spring框架 - AOP使用 笔记https://my.o ...

  6. Java开发工程师(Web方向) - 04.Spring框架 - 第2章.IoC容器

    第2章.IoC容器 IoC容器概述 abstract: 介绍IoC和bean的用处和使用 IoC容器处于整个Spring框架中比较核心的位置:Core Container: Beans, Core, ...

  7. Java开发工程师(Web方向) - 04.Spring框架 - 第1章.Spring概述

    第1章.Spring概述 Spring概述 The Spring Framework is a lightweight solution and a potential one-stop-shop f ...

  8. Java开发工程师(Web方向) - 04.Spring框架 - 第5章.Web框架

    第5章--Web框架 Web框架概述 Web框架单元测验 本次得分为:13.50/15.00, 本次测试的提交时间为:2017-09-25 1单选(2分) 关于Spring MVC中Dispatche ...

  9. Java开发工程师(Web方向) - 04.Spring框架 - 第4章.数据访问

    第4章--数据访问 Spring JDBC DAO (Data Access Object) 实现数据访问相关接口(接口和实现分离) ORM (Object Relation Mapping) 对象关 ...

随机推荐

  1. DHCP, NAT

    DHCP Dynamic Host Configuration Protocol(动态主机配置协议) (RFC 2131) Bootstrap Protocol BOOTP(引导程序协议) allow ...

  2. springboot启动报错:Could not resolve placeholder

    SpringBoot1.5,项目启动报错: Could not resolve placeholder 很明显是找不到配置文件引起的,查看配置文件目录结构如下: 很正常呀. 完全可以加载applica ...

  3. 使用BSRR和BRR寄存器直接操作STM32的I/O端口

    STM32的每个GPIO端口都有两个特别的寄存器,GPIOx_BSRR和GPIOx_BRR寄存器,通过这两个寄存器可以直接对对应的GPIOx端口置'1'或置'0'. GPIOx_BSRR的高16位中每 ...

  4. GoBelieve-国内唯一开源IM服务

    GoBelieve-国内唯一开源IM服务 1. 一小时接入 专注IM,无冗余功能 几行代码,一小时接入 省时省力. 2. 自由定制 提供最新源码, 自行二次开发,业务协议 交互视觉均可根据业务需求 自 ...

  5. 工具 | Axure基础操作 No.4

    昨天因为有事没有学习,很愧疚,今天赶紧补上.俗话说,"学如逆水行舟,不进则退"还是很有道理的. 1.设置页面内容格式 这里主要是在浏览器中的内容所出现的对齐格式,左对齐或者是居中对 ...

  6. Swift_100个Swift必备Tips 王巍 PDF

    Swift_100个Swift必备Tips 王巍 PDF GitHub下载地址

  7. #leetcode刷题之路35-搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置.你可以假设数组中无重复元素. 示例 1:输入: [1,3,5,6], 5输出: ...

  8. ubuntu包管理机制

    1 ubuntu包管理机制 跟大家分享一下ubuntu的软件管理机制.如果你们有过: apt-get install 或者 apt-get update 失败的经历. 在众多的apt命令中迷失. 疑惑 ...

  9. 竞赛题解 - CF Round #524 Div.2

    CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...

  10. python名称空间介绍

    python名称空间介绍 名称空间 python 中名称空间分三种: 内置名称空间 全局名称空间 局部名称空间 内置名称空间: 原码里面的一些函数都是存在这个内存空间中,任何模块均可访问它,它存放着内 ...