zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    软件工程——结构化方法
    静态变量的坑
    OpenCV中对Mat的遍历访问与赋值
    SQL SERVER 自定义函数 整数转成指定长度的16进制 转换成指定长度的16进制 不足补0
    Ext.Net中的Task控件的使用
    字符集越界 正则表达式匹配车牌
    [TOEIC] 2013年12月25日托业考试总结
    WebKit 内容整理
    [C#/.NET]how to implement web application localization in .net 4.0
    C# 代码为什么比 C++代码 编译速度快?
  • 原文地址:https://www.cnblogs.com/huhewei/p/12995033.html
Copyright © 2011-2022 走看看