Java学习杂谈(八)

终于正式进入J2ee的细节部分了,首当其冲的当然是Servlet和Jsp了,上篇曾经提到过J2ee只是一个规范和指南,定义了一组必须要遵循的接口,核心概念是组件和容器。曾经有的人问笔者Servlet的Class文件是哪里来的?他认为是J2ee官方提供的,我举了一个简单的反例:稍微检查了一下Tomcat5.0里面的Servlet.jar文件和JBoss里面的Servlet.jar文件大小,很明显是不一样的,至少已经说明了它们不是源自同根的吧。其实Servlet是由容器根据J2ee的接口定义自己来实现的,实现的方式当然可以不同,只要都遵守J2ee规范和指南。

上述只是一个常见的误区罢了,告诉我们要编译运行Servlet,是要依赖于实现它的容器的,不然连jar文件都没有,编译都无法进行。那么Jsp呢? Java Server Page的简称,是为了开发动态网页而诞生的技术,其本质也是Jsp,在编写完毕之后会在容器启动时经过编译成对应的Servlet.只是我们利用Jsp 的很多新特性,可以更加专注于前后台的分离,早期Jsp做前台是满流行的,毕竟里面支持Html代码,这让前台美工人员可以更有效率的去完成自己的工作。然后Jsp将请求转发到后台的Servlet,由Servlet处理业务逻辑,再转发回另外一个Jsp在前台显示出来。这似乎已经成为一种常用的模式,最初笔者学习J2ee的时候,大量时间也在编写这样的代码。

尽管现在做前台的技术越来越多,例如Flash、Ajax等,已经有很多人不再认为Jsp重要了。笔者觉得Jsp带来的不仅仅是前后端分离的设计理念,它的另外一项技术成就了我们今天用的很多框架,那就是Tag标签技术。所以与其说是在学习Jsp,不如更清醒的告诉自己在不断的理解Tag标签的意义和本质。

1.Servlet以及Jsp的生命周期

Servlet是Jsp的实质,尽管容器对它们的处理有所区别。Servlet有init()方法初始化,service()方法进行Web服务,destroy()方法进行销毁,从生到灭都由容器来掌握,所以这些方法除非你想自己来实现Servlet,否则是很少会接触到的。正是由于很少接触,才容易被广大初学者所忽略,希望大家至少记住Servlet生命周期方法都是回调方法。回调这个概念简单来说就是把自己注入另外一个类中,由它来调用你的方法,所谓的另外一个类就是Web容器,它只认识接口和接口的方法,注入进来的是怎样的对象不管,它只会根据所需调用这个对象在接口定义存在的那些方法。由容器来调用的Servlet对象的初始化、服务和销毁方法,所以叫做回调。这个概念对学习其他J2ee技术相当关键!

那么Jsp呢?本事上是Servlet,还是有些区别的,它的生命周期是这样的:

a)一个客户端的Request到达服务器 ->

b)判断是否第一次调用 -> 是的话编译Jsp成Servlet

c)否的话再判断此Jsp是否有改变 -> 是的话也重新编译Jsp成Servlet

d)已经编译最近版本的Servlet装载所需的其他Class e)发布Servlet,即调用它的Service()方法

所以Jsp号称的是第一次Load缓慢,以后都会很快的运行。从它的生命的周期确实不难看出来这个特点,客户端的操作很少会改变Jsp的源码,所以它不需要编译第二次就一直可以为客户端提供服务。这里稍微解释一下Http的无状态性,因为发现很多人误解,Http的无状态性是指每次一张页面显示出来了,与服务器的连接其实就已经断开了,当再次有提交动作的时候,才会再次与服务器进行连接请求提供服务。当然还有现在比较流行的是Ajax与服务器异步通过 xml交互的技术,在做前台的领域潜力巨大,笔者不是Ajax的高手,这里无法为大家解释。2.Tag标签的本质

笔者之前说了,Jsp本身初衷是使得Web应用前后台的开发可以脱离耦合分开有效的进行,可惜这个理念的贡献反倒不如它带来的Tag技术对J2ee的贡献要大。也许已经有很多人开始使用Tag技术了却并不了解它。所以才建议大家在学习J2ee开始的时候一定要认真学习Jsp,其实最重要的就是明白标签的本质。

Html标签我们都很熟悉了,有 、 、 、 ,Jsp带来的Tag标签遵循同样的格式,或者说更严格的Xml格式规范,例如 、  、、等等。它们没有什么神秘的地方,就其源头也还是Java Class而已,Tag标签的实质也就是一段Java代码,或者说一个Class文件。当配置文件设置好去哪里寻找这些Class的路径后,容器负责将页面中存在的标签对应到相应的Class上,执行那段特定的Java代码,如此而已。</p> <p>说得明白一点的话还是举几个简单的例子说明一下吧:</p> <p>去哪里找执行什么class呢?首先这是个jsp类库的标签,当然要去jsp类库寻找相应的class了,同样它也是由Web容器来提供,例如 Tomcat就应该去安装目录的lib文件夹下面的jsp-api.jar里面找,有兴趣的可以去找一找啊!</p> <p>又去哪里找呢?这个是由Jsp2.0版本推荐的和核心标记库的内容,例如 就对应在页面中做if判断的功能的一断Java代码。它的class文件在jstl.jar这个类库里面,往往还需要和一个standard.jar类库一起导入,放在具体Web项目的WEB-INF的lib目录下面就可以使用了。</p> <p>顺便罗唆一句,Web Project的目录结构是相对固定的,因为容器会按照固定的路径去寻找它需要的配置文件和资源,这个任何一本J2ee入门书上都有,这里就不介绍了。了解Tag的本质还要了解它的工作原理,所以大家去J2ee的API里找到并研究这个包:javax.servlet.jsp.tagext.它有一些接口,和一些实现类,专门用语开发Tag,只有自己亲自写出几个不同功能的标签,才算是真正理解了标签的原理。别忘记了自己开发的标签要自己去完成配置文件,容器只是集成了去哪里寻找jsp标签对应class的路径,自己写的标签库当然要告诉容器去哪里找啦。</p> <p>说了这么多,我们为什么要用标签呢?完全在Jsp里面来个就可以在里面任意写Java代码了,但是长期实践发现页面代码统一都是与html同风格的标记语言更加有助于美工人员进行开发前台,它不需要懂Java,只要Java程序员给个列表告诉美工什么标签可以完成什么逻辑功能,他就可以专注于美工,也算是进一步隔离了前后台的工作吧!</p> </p> <p>3.成就Web框架框架是什么?曾经看过这样的定义:与模式类似,框架也是解决特定问题的可重用方法,框架是一个描述性的构建块和服务集合,开发人员可以用来达成某个目标。一般来说,框架提供了解决某类问题的基础设施,是用来创建解决方案的工具,而不是问题的解决方案。</p> <p>正是由于Tag的出现,成就了以后出现的那么多Web框架,它们都开发了自己成熟实用的一套标签,然后由特定的Xml文件来配置加载信息,力图使得Web 应用的开发变得更加高效。下面这些标签相应对很多人来说相当熟悉了:</p> </p> </p> </p> <p>它们分别来自Struts和JSF框架,最强大的功能在于控制转发,就是MVC三层模型中间完成控制器的工作。Struts-1实际上并未做到真正的三层隔离,这一点在Struts-2上得到了很大的改进。而Jsf向来以比较完善合理的标签库受到人们推崇。</p> <p>今天就大概讲这么多吧,再次需要强调的是Servlet/Jsp是学习J2ee必经之路,也是最基础的知识,希望大家给与足够的重视!</p> <div style="padding: 10px 0px;text-align: center"> 把自己当傻瓜,不懂就问,你会学的更多</p> </div> <center> <a href="https://www.note234.com/337701.html" rel="bookmark" title="Java学习杂谈(八)"><img src="https://www.note234.com/wp-content/themes/generatepress/images/16.jpg" alt="Java学习杂谈(八)" /></a> </center> <footer class="entry-meta" aria-label="条目 meta"> <span class="cat-links"><span class="gp-icon icon-categories"><svg viewBox="0 0 512 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"><path d="M0 112c0-26.51 21.49-48 48-48h110.014a48 48 0 0143.592 27.907l12.349 26.791A16 16 0 00228.486 128H464c26.51 0 48 21.49 48 48v224c0 26.51-21.49 48-48 48H48c-26.51 0-48-21.49-48-48V112z" /></svg></span><span class="screen-reader-text">分类 </span><a href="https://www.note234.com/category/bckf" rel="category tag">编程开发</a></span> <span class="tags-links"><span class="gp-icon icon-tags"><svg viewBox="0 0 512 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em"><path d="M20 39.5c-8.836 0-16 7.163-16 16v176c0 4.243 1.686 8.313 4.687 11.314l224 224c6.248 6.248 16.378 6.248 22.626 0l176-176c6.244-6.244 6.25-16.364.013-22.615l-223.5-224A15.999 15.999 0 00196.5 39.5H20zm56 96c0-13.255 10.745-24 24-24s24 10.745 24 24-10.745 24-24 24-24-10.745-24-24z"/><path d="M259.515 43.015c4.686-4.687 12.284-4.687 16.97 0l228 228c4.686 4.686 4.686 12.284 0 16.97l-180 180c-4.686 4.687-12.284 4.687-16.97 0-4.686-4.686-4.686-12.284 0-16.97L479.029 279.5 259.515 59.985c-4.686-4.686-4.686-12.284 0-16.97z" /></svg></span><span class="screen-reader-text">标签 </span><a href="https://www.note234.com/tag/jar" rel="tag">jar</a>、 <a href="https://www.note234.com/tag/jee" rel="tag">jee</a>、 <a href="https://www.note234.com/tag/servlet" rel="tag">servlet</a></span> <nav id="nav-below" class="post-navigation" aria-label="文章"> <div class="nav-previous"><span class="gp-icon icon-arrow-left"><svg viewBox="0 0 192 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M178.425 138.212c0 2.265-1.133 4.813-2.832 6.512L64.276 256.001l111.317 111.277c1.7 1.7 2.832 4.247 2.832 6.513 0 2.265-1.133 4.813-2.832 6.512L161.43 394.46c-1.7 1.7-4.249 2.832-6.514 2.832-2.266 0-4.816-1.133-6.515-2.832L16.407 262.514c-1.699-1.7-2.832-4.248-2.832-6.513 0-2.265 1.133-4.813 2.832-6.512l131.994-131.947c1.7-1.699 4.249-2.831 6.515-2.831 2.265 0 4.815 1.132 6.514 2.831l14.163 14.157c1.7 1.7 2.832 3.965 2.832 6.513z" fill-rule="nonzero" /></svg></span><span class="prev"><a href="https://www.note234.com/337700.html" rel="prev">Java建模:UML工作簿,第2部分——序列图中的条件逻辑</a></span></div><div class="nav-next"><span class="gp-icon icon-arrow-right"><svg viewBox="0 0 192 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M178.425 256.001c0 2.266-1.133 4.815-2.832 6.515L43.599 394.509c-1.7 1.7-4.248 2.833-6.514 2.833s-4.816-1.133-6.515-2.833l-14.163-14.162c-1.699-1.7-2.832-3.966-2.832-6.515 0-2.266 1.133-4.815 2.832-6.515l111.317-111.316L16.407 144.685c-1.699-1.7-2.832-4.249-2.832-6.515s1.133-4.815 2.832-6.515l14.163-14.162c1.7-1.7 4.249-2.833 6.515-2.833s4.815 1.133 6.514 2.833l131.994 131.993c1.7 1.7 2.832 4.249 2.832 6.515z" fill-rule="nonzero" /></svg></span><span class="next"><a href="https://www.note234.com/337702.html" rel="next">Java学习杂谈(七)</a></span></div> </nav> </footer> </div> </article> <div class="inside-article"> <h3>相关文章:</h3> <ul><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433559.html">xxx cannot be resolved to a type 错误解决方法</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433468.html">搭建tomcat网站日志分析</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433197.html">Hibernate学习:slf4j日志框架</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/433127.html">Oracle Coherence中文教程二十:预加载缓存</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/432846.html">配置SpringMVC返回JSON遇到的坑</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/432648.html">MyBatis基本环境搭建与MyBatisUtil</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/432439.html">在Solr4.10配置IKAnalyzer 同义词、扩展词库、停顿词详解</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/432372.html">spring MVC整合json</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/432146.html">Android ORMLite 框架的入门用法</a></div> </div></li><li class="widget-common-media-post"> <div class="mtb10 magazine-media-item"><div class="description t-sm c-sub text-3line"><a href="https://www.note234.com/432142.html">解决WebLogic12C与Hibernate3.3的Antlr包冲突问题</a></div> </div></li></ul> <h4>你感兴趣的文章:</h4> <ul> <li><a href="https://www.note234.com/176427.html">乐视超4 X43电视有内置WIFI吗?</a></li> <li><a href="https://www.note234.com/36742.html">继电器的符号和触点表达方法</a></li> <li><a href="https://www.note234.com/124253.html">ubuntu下设立apache目录地址</a></li> <li><a href="https://www.note234.com/225554.html">禁止Windows多用户登录方式</a></li> <li><a href="https://www.note234.com/279820.html">中兴努比亚nubia x6手机连接电脑方法图解</a></li> <li><a href="https://www.note234.com/346395.html">Python 3.x 中如何使用calendar模块进行日历生成和处理</a></li> </ul> <h4>标签云:</h4> <div class="customtag"> <a href='https://www.note234.com/wangzhan/tag/%e4%ba%9a%e6%b4%b2%e9%ab%98%e6%b8%85%e7%94%b5%e5%bd%b1%e5%9c%a8%e7%ba%bf/'>亚洲高清电影在线</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%85%8d%e8%b4%b9%e9%ab%98%e6%b8%85%e7%94%b5%e5%bd%b1/'>免费高清电影</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%85%ab%e6%88%92%e5%bd%b1%e9%99%a2%e5%a4%9c%e9%97%b4/'>八戒影院夜间</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%85%ab%e6%88%92%e7%94%b5%e5%bd%b1%e6%9c%80%e6%96%b0%e5%a4%a7%e7%89%87/'>八戒电影最新大片</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%87%ba%e8%bd%a8%e5%9c%a8%e7%ba%bf%e7%94%b5%e5%bd%b1/'>出轨在线电影</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%8d%88%e5%a4%9c%e7%94%b5%e5%bd%b1%e9%99%a2/'>午夜电影院</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%9c%a8%e7%ba%bf%e5%bd%b1%e9%99%a2a1166/'>在线影院a1166</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%9c%a8%e7%ba%bf%e7%94%b5%e5%bd%b1%e9%99%a2/'>在线电影院</a>, <a href='https://www.note234.com/wangzhan/tag/%e5%9c%a8%e7%ba%bf%e8%a7%82%e7%9c%8b%e7%be%8e%e5%89%a7%e4%b8%8b%e8%bd%bd/'>在线观看美剧下载</a>, <a href='https://www.note234.com/wangzhan/tag/%e6%97%a5%e6%9c%ac%e7%88%b1%e6%83%85%e7%94%b5%e5%bd%b1/'>日本爱情电影</a>, <a href='https://www.note234.com/wangzhan/tag/%e6%97%a5%e9%9f%a9%e9%ab%98%e6%b8%85%e7%94%b5%e5%bd%b1%e5%9c%a8%e7%ba%bf/'>日韩高清电影在线</a>, <a href='https://www.note234.com/wangzhan/tag/%e7%94%b5%e5%bd%b1%e5%a4%a9%e5%a0%82%e7%bd%91/'>电影天堂网</a>, <a href='https://www.note234.com/wangzhan/tag/%e7%9b%b4%e6%92%ad%e7%9b%92%e5%ad%90app/'>直播盒子app</a>, <a href='https://www.note234.com/wangzhan/tag/%e8%81%9a%e5%90%88%e7%9b%b4%e6%92%ad/'>聚合直播</a>, <a href='https://www.note234.com/wangzhan/tag/%e9%ab%98%e6%b8%85%e7%be%8e%e5%89%a7/'>高清美剧</a>, <a href='https://www.note234.com/wangzhan/tag/%e9%ab%98%e6%b8%85%e7%be%8e%e5%89%a7%e5%9c%a8%e7%ba%bf%e8%a7%82%e7%9c%8b/'>高清美剧在线观看</a> <a href='https://www.note234.com/wangzhan/tag/ehviewer-e%e7%ab%99/'>EhViewer-E站</a>, <a href='https://www.note234.com/wangzhan/tag/e%e7%ab%99/'>E站</a>, <a href='https://www.note234.com/wangzhan/tag/e%e7%ab%99%e7%bb%bf%e8%89%b2%e7%89%88/' >E站绿色版</a>, <a href='https://www.note234.com/wangzhan/tag/qqmulu-com/'>qqmulu.com</a>, <a href='https://www.note234.com/wangzhan/tag/qq%e7%9b%ae%e5%bd%95%e7%bd%91/'>qq目录网</a>, <a href='https://www.note234.com/wangzhan/tag/qq%e7%bd%91%e7%ab%99%e7%9b%ae%e5%bd%95/'>qq网站目录</a>, </div></div> </main> </div> <div class="widget-area sidebar is-right-sidebar" id="right-sidebar"> <div class="inside-right-sidebar"> <aside id="block-11" class="widget inner-padding widget_block"><div class="_2ho2ho6w023"></div> <script type="text/javascript"> (window.slotbydup = window.slotbydup || []).push({ id: "u6953233", container: "_2ho2ho6w023", async: true }); </script> <!-- 多条广告如下脚本只需引入一次 --> <script type="text/javascript" src="//cpro.baidustatic.com/cpro/ui/cm.js" async="async" defer="defer" > </script></aside> </div> <div id="box_float"> <div id="float" class="div1"> <aside id="block-12" class="widget inner-padding widget_block"> <div class="_x3rdkhcqm5n"></div> <script type="text/javascript"> (window.slotbydup = window.slotbydup || []).push({ id: "u6958514", container: "_x3rdkhcqm5n", async: true }); </script> </aside></div> </div> </div> </div> </div> </div> <div class="site-footer"> <footer class="site-info" aria-label="站点" itemtype="https://schema.org/WPFooter" itemscope> <div class="inside-site-info grid-container"> <div class="copyright-bar"> <p></p> <p>Copyright © 2023 <a href="https://www.note234.com/" target="_blank">摘录笔记网</a></p> <a href="https://www.note234.com/tags" target="_blank">tags</a>,<a href="https://www.note234.com/wp-sitemap.xml" target="_blank">xml地图</a>,<a href="https://www.note234.com/sitemap.html" target="_blank">网站地图</a>,<a href="https://www.note234.com/lxwm" target="_blank">联系我们</a>,<a href="https://www.yiwen55.com/" target="_blank">易海博客</a>,<a href="https://www.note234.com/wangzhan/" target="_blank">免费网站</a>,<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备20014300号-1</a> </div> </div> </footer> </div> <a title="回到顶部" aria-label="回到顶部" rel="nofollow" href="#" class="generate-back-to-top" data-scroll-speed="400" data-start-scroll="300"> <span class="gp-icon icon-arrow-up"><svg viewBox="0 0 330 512" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414"><path d="M305.863 314.916c0 2.266-1.133 4.815-2.832 6.514l-14.157 14.163c-1.699 1.7-3.964 2.832-6.513 2.832-2.265 0-4.813-1.133-6.512-2.832L164.572 224.276 53.295 335.593c-1.699 1.7-4.247 2.832-6.512 2.832-2.265 0-4.814-1.133-6.513-2.832L26.113 321.43c-1.699-1.7-2.831-4.248-2.831-6.514s1.132-4.816 2.831-6.515L158.06 176.408c1.699-1.7 4.247-2.833 6.512-2.833 2.265 0 4.814 1.133 6.513 2.833L303.03 308.4c1.7 1.7 2.832 4.249 2.832 6.515z" fill-rule="nonzero" /></svg></span> </a><script id="generate-a11y">!function(){"use strict";if("querySelector"in document&&"addEventListener"in window){var e=document.body;e.addEventListener("mousedown",function(){e.classList.add("using-mouse")}),e.addEventListener("keydown",function(){e.classList.remove("using-mouse")})}}();</script><!--[if lte IE 11]> <script src="https://www.note234.com/wp-content/themes/generatepress/assets/js/classList.min.js" id="generate-classlist-js"></script> <![endif]--> <script id="generate-menu-js-extra"> var generatepressMenu = {"toggleOpenedSubMenus":"1","openSubMenuLabel":"\u6253\u5f00\u5b50\u83dc\u5355","closeSubMenuLabel":"\u5173\u95ed\u5b50\u83dc\u5355"}; </script> <script src="https://www.note234.com/wp-content/themes/generatepress/assets/js/menu.min.js" id="generate-menu-js"></script> <script id="generate-back-to-top-js-extra"> var generatepressBackToTop = {"smooth":"1"}; </script> <script src="https://www.note234.com/wp-content/themes/generatepress/assets/js/back-to-top.min.js" id="generate-back-to-top-js"></script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?95d0945b5c7afc67da1ee2166f8ca4b1"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html> <!-- Cached by ezCache --> <!-- Cache created in 0.83s -->