• jsp中的this


    网上解释:

      this永远表示的是当前对象。在jsp中有九大内置对象,其中page对应this关键字。JSP网页本身,page对象是当前页面转换后的Servlet类的实例。从转换后的Servlet类的代码中,可以看到这种关系:Object page = this;在JSP页面中,很少使用page对象。

      Jsp本质上就是Servlet,而servlet本质上就是一个类,你还不清楚this是什么意思么?就是指jsp对应的那个servlet对象本身。

    自己理解:

      jsp在显示前会先生成对应的.java文件,再生成对应的.class文件,在由容器执行.class文件后显示,例如hello.jsp,运行时会顺序生成hello_jsp.java及hello_jsp.class,位于tomcat目录的work目录下,在.java文件中为生成的对应的hello_jsp类,以下为hello_jsp.java文件结构:

    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 {

      //此处为文件内容省略

        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.Object page = this;

          //此处为文件内容省略

        }

        //此处为文件内容省略

      }

      由此是否可以理解为,this为该hello_jsp类的实例对象,该对象也为Servlet接口的一个实例,及jsp文件运行时的Servlet接口实例对象,因此在jsp中可以有如下的this使用:

      hello.jsp文件内容如下:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>jsp中的this使用</title></head>
    <body>
    <%
    String realpath = this.getServletContext().getRealPath("/") ;
    %>
    <h3>真实路径: <%=realpath%></h3>
    </body>
    </html>

  • 相关阅读:
    25. Spring Boot与缓存 JSR-107、Spring缓存抽象
    24. Spring Boot 自定义Starter (未整理,待续)
    UGUI 锚点
    UGUI Button控件
    UGUI Image控件
    UGUI Text控件
    Unity 角色复活和重新开始游戏
    Unity 读取Excel
    IOS使用C#预处理命令,多种SDK共存
    Unity扩展 四种Menu的区别
  • 原文地址:https://www.cnblogs.com/huhewei/p/12995033.html
Copyright © 2020-2023  润新知