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) 对象关 ...
随机推荐
- 如何在IAR工程中创建和使用模板
路径为:Edit -> Code Templates -> Edit Templates 如下图: #TEMPLATE "&FileDeclare>&Fi ...
- EF Core 2.0中Transaction事务会对DbContext底层创建和关闭数据库连接的行为有所影响
数据库 我们先在SQL Server数据库中建立一个Book表: CREATE TABLE [dbo].[Book]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Cr ...
- mysql where 加 多个 或者条件
select * from table where id=1 and uid=2 and (status=2 or status=3 or status=4);
- css代码添加背景图片常用代码
css代码添加背景图片常用代码 1 背景颜色 { font-size: 16px; content: ""; display: block; width: 700px; heigh ...
- Oracle作业5——多表查询、子查询
一.基础练习: 1.查询和scott相同部门的员工姓名ename和雇用日期hiredate SELECT ENAME,HIREDATE FROM EMP WHERE DEPTNO=(SELECT DE ...
- CF考古活动
Codeforces Beta Round #1 http://codeforces.com/contest/1 A.测试用水题,呵呵.给三个数nma,求ceil(n/a)*ceil(m/a). 长整 ...
- 竞赛题解 - NOIP2018 保卫王国
\(\mathcal{NOIP2018}\) 保卫王国 - 竞赛题解 按某一个炒鸡dalao名曰 taotao 的话说: \(\ \ \ \ \ \ \ \ \ "一道sb倍增题" ...
- ABAP术语-qRFC-Monitor
qRFC-Monitor 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/10/1098543.html Central monitoring ...
- Win10英文系统 JDK1.8安装及环境变量配置
前提 今天换新电脑了,需要重新安装一遍JDK.写个随笔记录一下整个过程. 下载 官网上JDK已经出到10了,但是回忆起JDK9都有各种坑(不支持一些软件),决定还是用JDK8. 下载地址: http: ...
- TinyMCE插件:FileManager [4.x-6.x] 配置及BUG处理
FileManager最新版已升级到9.x,9.x新增了对文件的批量处理,但仍然有部分同学在继续使用6.x,这里大叔整理了一份自己在配置6.x时,遇到的问题和解决方案. 安装 下载安装包解压后,在根目 ...