JSP声明

JSP声明用于声明变量和方法。在JSP声明中声明方法看起来很特别,似乎没有类,只有方法定义,方法似乎可以脱离类独立存在。实际上,JSP声明将会转换成Servlet的成员变量或成员方法,因此JSP声明依然符合Java语法。

JSP声明的语法格式如下:

<%! 声明部分 %>

看下面使用JSP声明的示例页面。

程序清单:codes\02\2.2\jspPrinciple\declare.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" %>声明测试< /TITLE></HEAD><!-- 下面是JSP声明部分 --><%!//声明一个整型变量public int count;//声明一个方法public String info(){return "hello";}%><BODY><%//将count的值输出后再加 1out.println(count++);%><br /><%//输出info()方法的返回值 out.println(info());%></BODY></HTML> </p> <p>在浏览器中测试该页面时,可以看到正常输出了count值,每刷新一次,count值将加1,同时也可以看到正常输出了info方法的返回值。</p> <p>上面粗体字代码部分声明了一个整型变量和一个普通方法,表面上看起来这个变量和方法不属于任何类,似乎可以独立存在,但这只是一个假象。打开Tomcat的 work\Catalina\localhost\jspPrinciple\org\apache\jsp目录下declare_jsp.java文件,看到如下代码片段:</p> <p>public final class declare_jsp extends org.apache.jasper.runtime.HttpJspBaseimplements org.apache.jasper.runtime.JspSourceDependent {//声明一个整型变量public int count;//声明一个方法public String info(){return “hello”;}…}</p> <p>上面的粗体字代码与JSP页面的声明部分完全对应,这表明JSP页面的声明部分将转换成对应Servlet的成员变量或成员方法。</p> <p>由于JSP声明语法定义的变量和方法对应于Servlet的成员变量和方法,所以JSP声明部分定义的变量和方法可以使用private、public等访问控制符修饰,也可使用static修饰,将其变成类属性和类方法。但不能使用abstract修饰声明部分的方法,因为抽象方法将导致JSP对应 Servlet变成抽象类,从而导致无法实例化。</p> <p>打开多个浏览器,甚至可以在不同的机器上打开浏览器来刷新该页面,将发现所有客户端访问的count值是连续的,即所有客户端共享了同一个count变量。这是因为:JSP页面会编译成一个Servlet类,每个Servlet在容器中只有一个实例;在JSP中声明的变量是成员变量,成员变量只在创建实例时初始化,该变量的值将一直保存,直到实例销毁。</p> <p>值得注意的是,info()的值也可正常输出。因为JSP声明的方法其实是在JSP编译生成的Servlet的实例方法——Java里的方法是不能独立存在的,即使在JSP页面中也不行。</p> <p>JSP声明中独立存在的方法,只是一种假象。<!-- --></p> <div class="d-flex justify-content-between flex-wrap my-4 font-weight-bold"> <div> 上一篇: <a href="https://www.javatt.com/p/127447" rel="next">在JSP中使用JavaMail</a> </div> <div> 下一篇: <a href="https://www.javatt.com/p/127445" rel="prev">葵花宝典 十七 jsp指令的使用</a> </div> </div> </div> </div> </article><!-- #post-## --> <div class="comments-area mt-5 text-small" id="comments"> <div id="respond" class="comment-respond"> <h3 id="reply-title" class="comment-reply-title">发表评论 <small><a rel="nofollow" id="cancel-comment-reply-link" href="/p/127446#respond" style="display:none;">取消回复</a></small></h3> <form action="https://www.javatt.com/wp-comments-post.php" method="post" id="commentform" class="comment-form" novalidate> <p class="comment-notes"><span id="email-notes">邮箱地址不会被公开。</span> 必填项已用<span class="required">*</span>标注</p><div class="form-group comment-form-comment"> <label for="comment">评论 <span class="required">*</span></label> <textarea class="form-control" id="comment" name="comment" aria-required="true" cols="45" rows="8"></textarea> </div><div class="form-group comment-form-author" <label for="author">昵称 <span class="required">*</span></label> <input class="form-control" id="author" name="author" type="text" value="" size="30" aria-required='true'></div> <div class="form-group comment-form-email"><label for="email">邮箱 <span class="required">*</span></label> <input class="form-control" id="email" name="email" type="email" value="" size="30" aria-required='true'></div> <div class="form-group comment-form-url"><label for="url">网站</label> <input class="form-control" id="url" name="url" type="url" value="" size="30"></div> <p class="form-submit"><input name="submit" type="submit" id="submit" class="btn btn-secondary" value="发表评论" /> <input type='hidden' name='comment_post_ID' value='127446' id='comment_post_ID' /> <input type='hidden' name='comment_parent' id='comment_parent' value='0' /> </p><p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="d65575aa77" /></p><input type="hidden" id="ak_js" name="ak_js" value="207"/><textarea name="ak_hp_textarea" cols="45" rows="8" maxlength="100" style="display: none !important;"></textarea> </form> </div><!-- #respond --> </div><!-- #comments --> </main><!-- #main --> </div><!-- #primary --> <div class="col-md-3 widget-area" id="global-sidebar" role="complementary"> <aside id="recent-comments-2" class="widget widget_recent_comments"><h3 class="widget-title">近期评论</h3><ul id="recentcomments"><li class="recentcomments"><span class="comment-author-link"><a href='http://xybernetics.com/techtalk/how-to-use-android-java-libraries-jar-or-aar-in-xamarin-forms/' rel='external nofollow' class='url'>How to use Android Java Libraries (.JAR or .AAR) in Xamarin Forms - Xybernetics</a></span>发表在《<a href="https://www.javatt.com/p/78053#comment-10181">Xamarin binding an Android jar library</a>》</li><li class="recentcomments"><span class="comment-author-link">Jodie</span>发表在《<a href="https://www.javatt.com/p/98063#comment-8555">java的%d和%f 是什么意思</a>》</li><li class="recentcomments"><span class="comment-author-link"><a href='https://aqzt.com/27552.html' rel='external nofollow' class='url'>JVM性能优化系列-(1) Java内存区域 安全专题</a></span>发表在《<a href="https://www.javatt.com/p/60329#comment-8554">基于JDK1.8 分析运行时常量池、字符串常量池、各种常量池</a>》</li><li class="recentcomments"><span class="comment-author-link">dev</span>发表在《<a href="https://www.javatt.com/p/106108#comment-8546">Kotlin 打包 Jar</a>》</li><li class="recentcomments"><span class="comment-author-link">Ligz</span>发表在《<a href="https://www.javatt.com/p/29007#comment-8245">Spring Boot上传文件过大处理方式(基于Spring Boot2.0)</a>》</li><li class="recentcomments"><span class="comment-author-link">wandi</span>发表在《<a href="https://www.javatt.com/p/43718#comment-7812">虚拟机漫谈(Lua、Java、C#)</a>》</li><li class="recentcomments"><span class="comment-author-link">gary</span>发表在《<a href="https://www.javatt.com/p/118389#comment-7716">深入剖析tomcat之servlet容器</a>》</li></ul></aside> </div> </div><!-- .row --> </div><!-- Container end --> </div><!-- Wrapper end --> <div class="wrapper" id="wrapper-footer"> <div class="container"> <div class="row"> <div class="col-md-12"> <footer class="site-footer" id="colophon"> <div class="site-info"> <span>Theme: <a href="https://www.awaimai.com/maizi">Maizi</a>.</span> <span>Powered by <a href="https://wordpress.org/" target="_blank">WordPress</a>.</span> <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank"title="工业和信息化部ICP/IP地址/域名信息备案管理系统">粤ICP备17138695号</a> <div class="widget_text footer-copyright"><div class="textwidget custom-html-widget"><script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?ebde28b07bff5f32b87cc5b5092c7e4c"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </div></div> </div><!-- .site-info --> <a href="#" class="scroll-top"> <i class="dashicons dashicons-arrow-up-alt2 icon16"></i> </a> </footer><!-- #colophon --> </div><!--col end --> </div><!-- row end --> </div><!-- container end --> </div><!-- wrapper end --> </div><!-- #page --> <script type='text/javascript' src='https://www.javatt.com/wp-content/themes/maizi/assets/js/popper.min.js?ver=1.14.7'></script> <script type='text/javascript' src='https://www.javatt.com/wp-content/themes/maizi/assets/bootstrap/js/bootstrap.min.js?ver=4.3.1'></script> <script type='text/javascript' src='https://www.javatt.com/wp-content/themes/maizi/assets/js/js.cookie-2.2.1.min.js?ver=2.2.1'></script> <script type='text/javascript' src='https://www.javatt.com/wp-content/themes/maizi/assets/js/theme.js?ver=1.0.0'></script> <script type='text/javascript'> var $ = jQuery; var cookieName = 'aside-content-tab-id'; var tabId = Cookies.get(cookieName); if (tabId !== undefined) { $('#myTab a[href="#' + tabId + '"]').tab('show'); } $('.aside-content').find('.nav-link').on('click', function() { tabId = $(this).attr('aria-controls'); Cookies.set(cookieName, tabId, { expires: 7, path: '/' }) }); jQuery.post('https://www.javatt.com/wp-admin/admin-ajax.php', { action : 'update_post_views', post_id : 127446 }); </script> <script type='text/javascript' src='https://www.javatt.com/wp-includes/js/wp-embed.min.js?ver=5.0.3'></script> <script async="async" type='text/javascript' src='https://www.javatt.com/wp-content/plugins/akismet/_inc/form.js?ver=4.1.9'></script> </body> </html>