2021年4月12日星期一

070_JSP详解

目录
  • 什么是JSP
  • JSP原理
    • JSP源码分析
    • JSP流程图
    • JSP页面转java代码
  • JSP基础语法
    • 示例
      • 创建maven-archetype-webapp项目
      • 替换web.
      • 查询https://mvnrepository.com,添加依赖到pom.
      • 配置Tomcat
    • JSP基础语法
      • JSP表达式
      • JSP脚本
        • JSP文件生成的java文件
      • JSP声明
      • 区别:JSP声明会被编译到JSP生成的java类中,JSP表达式和JSP声明则会被编译到JSP生成的_jspService方法中
  • JSP指令
    • page 定义网页属性
    • include 包含其他页面
    • taglib 引入标签库
  • 9大内置对象
  • JSP标签、JSTL标签、EL表达式
    • 需要依赖的包
    • JSP标签
    • JSTL标签
      • 安装使用步骤
      • 核心标签
      • 格式化标签
      • SQL 标签
      • JSTL 函数
    • EL表达式
      • 获取数据
      • 执行运算
      • 获取web开发的常用对象
  • JavaBean-实体类
    • JavaBean有特定的写法
    • ORM-对象关系映射


什么是JSP

Java Server Pages Java服务器端页面,和Servlet一样,用于动态Web技术

特点:写JSP,就像写HTML一样
与HTML的区别:

  • HTML只能给用户提供静态的数据
  • JSP页面中可以嵌入Java代码,为用户提供动态数据

JSP原理

  1. Tomcat中有一个work目录

image.png

  1. IDEA中使用Tomcat会在IDEA的Tomcat中产生一个work目录

image.png

  1. jsp文件会转为java程序
  2. 浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/9.0.43 * Generated at: 2021-03-16 15:12:15 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 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 = null; } 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 { if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {  final java.lang.String _jspx_method = request.getMethod();  if ("OPTIONS".equals(_jspx_method)) {  response.setHeader("Allow","GET, HEAD, POST, OPTIONS");  return;  }  if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {  response.setHeader("Allow","GET, HEAD, POST, OPTIONS");  response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");  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");  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("<html>\n");  out.write("<body>\n");  out.write("<h2>Hello World!</h2>\n");  out.write("</body>\n");  out.write("</html>\n"); } 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); } }}

JSP源码分析

// 初始化 public void _jspInit() { }// 销毁public void _jspDestroy() {}//JSPService public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)  throws java.io.IOException, javax.servlet.ServletException {// 1.判断请求 if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {  final java.lang.String _jspx_method = request.getMethod();  if ("OPTIONS".equals(_jspx_method)) {  response.setHeader("Allow","GET, HEAD, POST, OPTIONS");  return;  }  if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {  response.setHeader("Allow","GET, HEAD, POST, OPTIONS");  response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");  return;  } }// 2.内置对象 final javax.servlet.jsp.PageContext pageContext;// 页面上下文 javax.servlet.http.HttpSession session = null;// 会话 final javax.servlet.ServletContext application;// Servlet上下文 final javax.servlet.ServletConfig config;// config javax.servlet.jsp.JspWriter out = null;// out final java.lang.Object page = this;// page当前页面 javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try {// 3.输出页面前增加的代码,以下的对象可以在jsp页面中直接使用  response.setContentType("text/html");// 设置响应的页面类型  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;// 4.输出页面  out.write("<html>\n");  out.write("<body>\n");  out.write("<h2>Hello World!</h2>\n");  out.write("</body>\n");  out.write("</html>\n"); } 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); } }

JSP流程图

image.png

JSP页面转java代码

  1. 页面中的java代码,会直接应用到java代码中
  2. 页面中的HTML代码,会转换为 out.write("<html>");  ,输出到页面

JSP基础语法

示例

创建maven-archetype-webapp项目

image.png

<?

配置Tomcat

image.png

JSP基础语法

JSP作为java技术的一种应用,它拥有一些自己扩充的语法,java所有语法都支持。

JSP表达式

<%--JSP表达式作用:用来将程序的输出,输出到客户端语法:<%= 变量或者表达式 %>--%><%=new java.util.Date()%><hr><%=2*3%>

JSP脚本

<%--JSP脚本作用:用来写java方法内代码语法:<% java方法类代码 %>   --%><% int sum = 0; for (int i = 1; i <= 100; i++) {  sum += i; } out.println("<h1>sum=" + sum + "</h1>");%>
<%--在代码中嵌入HTML--%><% for (int i = 0; i < 5; i++) {%><h1>hello,world <%=i%></h1><% }%>

image.png

JSP文件生成的java文件

 public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)  throws java.io.IOException, javax.servlet.ServletException { if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {  final java.lang.String _jspx_method = request.getMethod();  if ("OPTIONS".equals(_jspx_method)) {  response.setHeader("Allow","GET, HEAD, POST, OPTIONS");  return;  }  if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {  response.setHeader("Allow","GET, HEAD, POST, OPTIONS");  response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");  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");  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("<html>\n");  out.write("<body>\n");  out.write('\n');  out.print(new java.util.Date());  out.write("\n");  out.write("<hr>\n");  out.print(2*3);  out.write("\n");  out.write("<hr>\n");  out.write('\n'); int sum = 0; for (int i = 1; i <= 100; i++) {  sum += i; } out.println("<h1>sum=" + sum + "</h1>");  out.write('\n');  out.write('\n'); for (int i = 0; i < 5; i++) {  out.write("\n");  out.write("<h1>hello,world ");  out.print(i);  out.write("</h1>\n"); }  out.write("\n");  out.write("</body>\n");  out.write("</html>\n"); } 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); } }

JSP声明

<%--JSP声明作用:用来写java类内代码语法:<%! java类内代码 %>   --%><%! static {  System.out.println("静态代码块"); } private int globalVal = 0; public void test() {  System.out.println(globalVal); }%>

生成的java代码

static {  System.out.println("静态代码块"); }private int globalVal = 0;public void test() { System.out.println(globalVal);}

区别:JSP声明会被编译到JSP生成的java类中,JSP表达式和JSP声明则会被编译到JSP生成的_jspService方法中

JSP指令

page 定义网页属性

<%--导包--%><%@ page import="java.util.Date" %>
<%--定制错误页面,错误时跳转到自定义错误页面--%><%@ page errorPage="error/500.jsp" %>
<%--显式声明这是一个错误页面--%><%@ page isErrorPage="true" %>
<%--定义页面编码--%><%@ page pageEncoding="utf-8" %>

include 包含其他页面

<%--jsp指令--%><%--引入公用头部--%><%@ include file="common/header.jsp"%><h1>网页主体</h1><%--引入公用底部--%><%@ include file="common/footer.jsp"%><%--两者的区别:jsp指令,会将两个页面合并到本页面jsp标签,拼接页面,本质还是三个页面jsp标签比较常用,注:jsp标签路径加/,代表web根目录--%><%--jsp标签--%><%--引入公用头部--%><jsp:include page="/common/header.jsp"/><h1>网页主体</h1><%--引入公用底部--%><jsp:include page="/common/footer.jsp"/>

taglib 引入标签库

9大内置对象

  1. javax.servlet.http.HttpServletRequest request
  2. javax.servlet.http.HttpServletResponse response
  3. javax.servlet.ServletException exception
  4. javax.servlet.jsp.PageContext pageContext
  5. javax.servlet.http.HttpSession session
  6. javax.servlet.ServletContext application
  7. javax.servlet.ServletConfig config
  8. javax.servlet.jsp.JspWriter out
  9. java.lang.Object page = this
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body><%--内置对象--%><% pageContext.setAttribute("name1", "1号");// 保存的数据在一个页面有效 request.setAttribute("name2", "2号");// 保存的数据在一个请求有效,请求转发会携带这个数据 session.setAttribute("name3", "3号");// 保存的数据在一个回话有效,从打开浏览器到关闭浏览器 application.setAttribute("name4", "4号");// 保存的数据在服务器中有效,从启动服务器到关闭服务器%><%--脚本片段中的代码会原封不动的生成到jsp.java,所以jsp脚本中的代码遵守java语法,注释也用java注释--%><% // 从pageContext中取值,通过寻找的方式来,从底层到高层(作用域):pageContext<request<session<application String name1 = (String) pageContext.findAttribute("name1"); String name2 = (String) pageContext.findAttribute("name2"); String name3 = (String) pageContext.findAttribute("name3"); String name4 = (String) pageContext.findAttribute("name4"); String name5 = (String) pageContext.findAttribute("name5");%><%--使用JSP表达式和EL表达式输出的区别:针对没有的数据,JSP表达式会输出null,EL表达式不会输出--%><h1>JSP表达式输出值</h1><h3><%=name1%></h3><h3><%=name2%></h3><h3><%=name3%></h3><h3><%=name4%></h3><h3><%=name5%></h3><hr><h1>EL表达式输出值</h1><h3>${name1}</h3><h3>${name2}</h3><h3>${name3}</h3><h3>${name4}</h3><h3>${name5}</h3></body></html>

JSP标签、JSTL标签、EL表达式

需要依赖的包

  <!--JSTL表达式依赖-->  <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->  <dependency>   <groupId>javax.servlet.jsp.jstl</groupId>   <artifactId>jstl-api</artifactId>   <version>1.2</version>  </dependency>  <!--standard标签库-->  <!-- https://mvnrepository.com/artifact/taglibs/standard -->  <dependency>   <groupId>taglibs</groupId>   <artifactId>standard</artifactId>   <version>1.1.2</version>  </dependency>

JSP标签

<%--包含页面--%><jsp:include page="header.jsp"></jsp:include><%--请求转发--%><jsp:forward page="500.jsp"> <jsp:param name="id" value="1"></jsp:param> <jsp:param name="name" value="张三丰"></jsp:param></jsp:forward>

JSTL标签

https://www.runoob.com/jsp/jsp-jstl.html

JSTL标签库的使用是为了弥补HTML标签的不足,它自定义了许多标签,可以供我们使用,标签的功能和java代码一样。

安装使用步骤

  1. Tomcat安装JSTL标签库

image.png

<?
  1. 使用任何库,必须在每个 JSP 文件中的头部包含  标签。

核心标签

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

image.png

格式化标签

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

image.png

SQL 标签

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

image.png


JSTL 函数

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

image.png

EL表达式

获取数据

执行运算

获取web开发的常用对象

JavaBean-实体类

JavaBean有特定的写法

  1. 必须有一个无参构造,如果没有有参构造,无参构造隐式默认有
  2. 属性必须私有化private
  3. 必须有对应的get/set方法

一般用来和数据库的字段做映射

ORM-对象关系映射

  • 表-->类
  • 字段-->属性
  • 行记录-->对象








原文转载:http://www.shaoqun.com/a/678735.html

跨境电商:https://www.ikjzd.com/

aca:https://www.ikjzd.com/w/1371

auction:https://www.ikjzd.com/w/2311


目录什么是JSPJSP原理JSP源码分析JSP流程图JSP页面转java代码JSP基础语法示例创建maven-archetype-webapp项目替换web.查询https://mvnrepository.com,添加依赖到pom.配置TomcatJSP基础语法JSP表达式JSP脚本JSP文件生成的java文件JSP声明区别:JSP声明会被编译到JSP生成的java类中,JSP表达式和JSP声明则
e票联:https://www.ikjzd.com/w/1452
慧聪商务网:https://www.ikjzd.com/w/1836
kk馆:https://www.ikjzd.com/w/1713
图文详解:如何通过Applications功能实现listing优化:https://www.ikjzd.com/home/59793
女友主动献身有钱哥们报复我出轨:http://www.30bags.com/m/a/251233.html
带你了解外贸行业里欧美客户的特点:https://www.ikjzd.com/home/140506

没有评论:

发表评论