zoukankan      html  css  js  c++  java
  • JSP中的声明语句块和表达式块

    一、JSP中的声明语句块
      在JSP页面中使用<%!%>括起来的部分,称为声明语句块。声明语句块中的内容,将被JSP引擎翻译到Servlet的类体中,是不会被包含到某个方法中的。
      这样的话,在JSP的声明语句块中就可以声明实例变量、实例方法、静态方法、静态代码块等内容。并且,这些内容均可被JSP的的Java代码块中的代码访问。因为可以访问类属性的。不过,还是不建议在JSP的声明语句块中声明实例变量。因为JSP是运行在单例多线程环境下的,实例变量将会引起线程安全问题。
      需要注意的是,在JSP的声明语句块中,是不能编写某些普通的Java语句的(比如System.out.println("");)。因为这些代码最终会被放置在Java类体当中的,而不属于某个方法,所以类体中不能出现诸如:System.out.println("");的java语句的。

    二、示例

      2.1 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>JSP_Declaration(JSP声明)</title>
    </head>
    <body>
    <!-- 我是HTML注释,我注释的内容是会被发送到客户端的 ,可以右键源码的形式查看我所注释的内容。-->
    <%--我是JSP注释,我注释的内容不会被发送到客户端的,客户端是不可以通过右键源码的形式查看我所注释内容的。--%>
    jsp声明!
    <%!
    //声明一个静态私有的类变量a
    private static int a=11;
    /**
    声明一个方法
    */
    private static int getNum(int a,int b){
        return  a+b;
    }
    %>
    </body>
    <%
        /*结果*/
        System.out.println("getNum(3,5)的返回值为:"+getNum(3,5));
    %>
    </html>

      2.2 jsp引擎翻译后的.java源文件如下:  

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.85
     * Generated at: 2019-01-22 23:04:10 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 Declaration_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {
    
    
    //声明一个静态私有的类变量a
    private static int a=11;
    /**
    声明一个方法
    */
    private static int getNum(int a,int b){
        return  a+b;
    }
    
      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 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 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 {
    
        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=UTF-8");
          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=UTF-8">
    ");
          out.write("<title>JSP_Declaration(JSP声明)</title>
    ");
          out.write("</head>
    ");
          out.write("<body>
    ");
          out.write("<!-- 我是HTML注释,我注释的内容是会被发送到客户端的 ,可以右键源码的形式查看我所注释的内容。-->
    ");
          out.write("
    ");
          out.write("jsp声明!
    ");
          out.write("
    ");
          out.write("</body>
    ");
    
        /*结果*/
        System.out.println("getNum(3,5)的返回值为:"+getNum(3,5));
    
          out.write("
    ");
          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);
        }
      }
    }

      2.3 对比图

      

      

     


    三、JSP中的表达式块

      在JSP页面中使用<%= %>括起来的部分,称为表达式块。其可在JSP页面中输出变量、常量及它们组成的各种表达式的值。注意是表达式而不是语句,是不能有分号的。该表达式将被JSP引擎翻译到_jspService()方法的out.write()中输出。

      3.1示例  

    <%@ 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>JSP_Declaration(JSP声明)</title>
    </head>
    <body>
    <!-- 我是HTML注释,我注释的内容是会被发送到客户端的 ,可以右键源码的形式查看我所注释的内容。-->
    <%--我是JSP注释,我注释的内容不会被发送到客户端的,客户端是不可以通过右键源码的形式查看我所注释内容的。--%>
    jsp声明!
    <%!
    //声明一个静态私有的类变量a
    private static int a=11;
    /**
    声明一个方法
    */
    private static int getNum(int a,int b){
        return  a+b;
    }
    %>
    <br/> a+2的值为:<%=a+2 %> </body> <% /*结果*/ System.out.println("getNum(3,5)的返回值为:"+getNum(3,5)); %> </html>

      结果截图如下:

      

    如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

  • 相关阅读:
    中序遍历
    二叉树前序遍历
    A Real Stewart
    走遍美国 听写
    2016-12-12——2016-12-16友邻
    英语百日听力
    6.2分鱼问题两种解法
    Bootstrap组件1
    Bootstrap图标及另一个好用图标网站介绍
    Bootstrap全局CSS样式之图片
  • 原文地址:https://www.cnblogs.com/aeon/p/10306999.html
Copyright © 2011-2022 走看看