• Java开发工程师(Web方向)


    第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(" "); out.write("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> "); out.write("<html> "); out.write("<head> "); out.write("<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> "); out.write("<title>Insert title here</title> "); out.write("</head> "); out.write("<body> "); out.write("<!-- this is jsp comment --> "); out.write(" "); out.write("request uri is "); out.print(request.getRequestURI() ); out.write(" "); out.write("<br/> "); name = "abc"; out.println("name is " + name); out.write(" "); out.write("</body> "); 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()

     

  • 相关阅读:
    笔记:2016-06-20
    笔记2016-06-22
    javascript中的call(),apply(),bind()方法的区别
    JavaScript实现大整数减法
    Javascript实现大整数加法
    Javascript toString()、toLocaleString()、valueOf()三个方法的区别
    排序算法时间空间复杂度比较
    H-ui框架制作选项卡
    es6 解构赋值
    fullPage全屏高度自适应
  • 原文地址:https://www.cnblogs.com/FudgeBear/p/7339086.html
Copyright © 2020-2023  润新知