- 浏览: 343218 次
- 性别:
- 来自: 北京
-
最新评论
-
hoey168:
请问楼主,ICE 客户端连接多个服务端,tcp -h 172. ...
ZeroC ICE之旅------负载均衡及容错 -
iOracleSun:
makeC++SharedLib 增加 -G参数即可链接成功 ...
AIX apache module问题 -
fanyonglu:
不错,讲的很细,学习中
ZeroC ICE之旅------java -
click_guobin:
...
我在深圳,每月收入850元,怎么也花不完,晒一晒我是怎么开销和投资的(zz) -
hanyu332:
引用修改%apache%/conf/httpd.conf修改为 ...
awstats日志分析小结(1)
Introduction
All modern Java web applications use Servlets and Filters. They are the backbone of Java EE, the communication gateway to the World Wide Web.
Now there is a new specification coming, Servlet 3.0 (JSR-315). The Early Draft of this specification features some new really neat features, and in my opinion some mistakes. In this article I'm going to show the new additions to the EOD (ease of development), comment on them, and try to improve them.
Servlets
Since Servlet 2.3 we've been using the Servlet interface to create our Servlets. This interface has one main method called service(ServletRequest reand a ServletResponse. The abstract class HttpServlet helps us to create a HTTP suitable Servlet. It contains methods like doGet and doPost.
The easiest way to create custom behavior was to extend the HttpServlet and implement your own doGet and/or doPost.
public class GetPostServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { .... } }
To instruct a Servlet Container to use your Servlet you have to add them to a file called the web.xml. This file contains mapping information from URL to Servlet, so when a user requests the mapped URL the Servlet will be called and asked for a response.
JSR-315
Servlet 3.0 has a couple of proposed changes. One of the big changes is adding support for continuations, in the new API you can call request.suspend() to suspend the request, and later (from another thread) call request.resume(), the Servlet is then again called to resume the request.
The second important addition is pluggability. Servlets can now be added to the container during runtime. If you create an instance of the ServletContextListener you can call servletContext.addServlet() and servletContext.addServletMapping() to add Servlets on runtime. Of course this poses a security risk, because included JARs could register their own Servlets, something you definitely don't want. But the specification is aware of this, and working on making it safer (for example, giving you a switch to turn auto-detection off).
The third major addition are the new annotations. Here is an example of these annotations in use:
@Servlet(urlMapping={"/myServlet"}, name="MyServlet") public class PojoServlet() { @GET public void handleGet(HttpServletRequest req, HttpServletResponse resp) { .... } }
As you can see there are a couple of new features. First of all, the Servlet is a POJO; there is no more interface/abstract class. Also notice the url mapping in the @Servlet annotation; no need for the mapping in the web.xml.
Then there are the Filters:
@ServletFilter @FilterMapping(urlPatter="/myFilter") public class PojoFilter() { public void doFilter(HttpServletRequest req, HttpServletResponse resp) { .... } }
Here we can see a ServletFilter with the new annotations. This again has the mapping in the annotation, no need for the web.xml and it is a POJO again, no more interface/abstract class.
What is the problem?
So you might ask, what is wrong with this proposals new ease of development?
Well, maybe a couple of things. First of all, lets see what these new annotations add:
- The mapping information is in the Servlet
- The Servlets are POJO's
- You can specify which method handles the GET and POST
Well, first of all the mapping information, which is a great feature. Although the naming of the annotations is a bit unfortunate. Why does the @Servlet annotation have the mapping inside, and does the @ServletFilter have a seperate annotation @FilterMapping? It would be much cleaner to have one way of declaring these mappings.
The second and third change (the ability to specify the GET and POST with @GET, @POST, @HEAD, @PUT etc.) has only one small advantage. Who hasn't written the following code while creating Servlets:
public class GetPostServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { handleRequest(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { handleRequest(req,res); } /* Handle GET and POST */ public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { .... } }
This could be done a lot easier with the new annotations:
@Servlet public class GetPostServlet { @GET @POST public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { .... } }
So, nothing wrong here! Well, I asked if you could specify two of these annotation on one method during the launch of the specification on JavaOne 2008, but the spec-lead wasn't sure this could be done. So this is probably not the way its meant to be used.
And there is more, this also adds problems to the code, for example what if I coded this:
@Servlet public class GetPostServlet { @GET public void requestOne(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { .... } @GET public void requestTwo(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { .... } }
Now which method will be called? I have no idea! This will probably not even be documented, just like having two Servlets mapped on the same URL, you just can't predict what will happen, its up to the container.
These annotations are (supposed to be) added to help the developers, but there are more problems. The IDE doesn't know you are making a Servlet just because you add one annotation, so there is no support from the current generation of IDE's. When we had the interface or abstract class you would just override the methods you want, and you have code-completion. This is a huge advantage we'll loose when programming with these annotations!
It gets even worse if you look at the @ServletFilter. When you add this annotation to your code it turns the POJO into a Filter. But there is no check if you have implemented the doFilter() method. Also, it is much more error-prone, what if you accidently misspell "doFilter"?
The same thing also applies to the @ServletListenerContext, just look at this example:
@ServletContextListener public class MyListener { public void contextInitialized(ServletContextEvent sce) { .... } }
Again, nothing will check if you correctly spelled "contextInitialized" (which is a pretty hard word to type over and over without error-checking), you'll always have to manually double check. I'd rather type "implements ServletContextListener" instead of "@ServletContextListener" and then let my IDE write the implementing methods.
Okay, that is enough tough feedback. Next I would like to do something constructive and explain what direction I would like to see Servlet 3.0 go.
Proposal 1
The idea of having annotations for the url-mapping if very good. URLs are a good example of metadata, it doesn't say anything about the class or inner workings, but it does tell the container when to call it. This is something you would use an annotation for, also because its not mandatory to have mapping in the class, it would work perfectly without it. But as I said before, the naming is a bit off, I'd rather see this:
@ServletMapping(name="....", mapping={"...","..."}) //Use for both Servlets and Filters
The second thing is the annotations (@GET, @POST). I don't think this will help the user in any way, so just leave them out. I know the standard reply will be "Just don't use them if you don't like them...".
But there are already a lot of Java features we should just 'not use'. The problem is, people WILL use them, and it doesn't help them! In fact it will only add more choices/options for the programmers, making it all less clear if you incidentally have to work with Servlets. I personally haven't heard anybody complain about the interfaces/abstract classes before... And I can't seem to find any new advantage that it adds, only loosing the type-safety!
This was exactly the thing I was afraid of when they introduced annotations, people who start using them because they can, not because they have some metadata they want to have near the classes instead of in separate XML files.
Proposal 2
But *sigh* if they really want to use the annotations on the methods, then please add functionality to the Servlets. Currently they'll just serve as a (bad) alternative way of writing Servlets. But with a slight change you could actually add new features with these annotations, like this:
public class ExampleServlet { @ServletMethod(name="LoginServlet", mapping="/login", type={ServletType.GET, ServletType.POST}) public void handleLogin(...) { ... } //And: @FilterMethod(name="AuthorizationFilter", mapping="/login", type={ServletType.POST}) public void doAuthorization(...) { ... } }
This way you can have one class (POJO) with all the handling methods for a specific Use Case. More or less like the new Spring MVC Controllers. You'll still have the same functionality as the proposed JSR-315 annotations, but now you can group Servlet methods in one class! You still have the drawbacks I mentioned before, regarding the type-safety, but by using these annotations you actually gain some functionality and freedom.
Conclusion
My preference would still be not using annotations at all for GET/POST methods, but I've read the Java EE 6 specification is promoting the use of annotations like this and the JSR-315 writers have 'no choice' (bad excuse). The comments I made in this article have been send to the JSR-group, but I haven't got a response yet.
Also I've been unable to reach members for a reaction and/or explanation and/or clarification. Recently the Java EE 6 specification went on public review, and it contains references to this Servlet 3.0 specification, so it will become part of Java EE 6. This must mean they are working actively, maybe even franticly, to finish it on time. But I plead them to take the time to reconsider their choices about the annotations.
发表评论
-
apache 2.2.13
2009-08-10 13:41 2130Changes with Apache 2.2.13 * ... -
May 2009 Web Server Survey
2009-06-02 14:30 1145In the May 2009 survey we recei ... -
俄罗斯农民乘法
2009-02-10 18:29 3112规则:什么是俄罗斯农民 乘法?我要怎么使用它? ... -
Google搜索消耗的能量相当于烧一壶茶
2009-01-13 22:11 1080两条Google 搜索真的能产 ... -
人保部:男女退休年龄或推至65岁
2008-11-06 14:03 1001据《羊城晚报》报道 “相关部门正在酝酿条件成熟时延长法定退休年 ... -
王牌军排名
2008-10-28 08:59 1064第1名.王牌铁军--43军 43军在中国人民解放军中资历老 ... -
迪拜负债476亿美元超过GDP 阿拉伯财富神话破灭
2008-10-17 16:40 1812很长时间以来,迪拜债 ... -
红杉资本给CEO们的信
2008-10-16 14:48 1001现在的形势非常严 ... -
AdSense 推介计划即将暂停
2008-07-02 16:52 1015from http://adsense.googlechina ... -
7家顶级GPS软件企业大揭秘(ZZ)
2008-05-09 13:18 1809在揭秘前,首先给这国内顶级6家GPS企业分别冠名,之后在逐一阐 ... -
IBM架设second life私有土地(ZZ)
2008-04-11 19:05 1519SECOND LIFE,4月2号(路透社)——IBM声称,本周 ... -
4大技巧教你成为沟通中的说话高手(ZZ)
2008-04-07 15:59 1559有个故事讲,在酒足饭 ... -
贾鹏雷:请江南春停止撒谎(ZZ)
2008-04-02 11:30 1169上周老贾写了中心思想 ... -
我在深圳,每月收入850元,怎么也花不完,晒一晒我是怎么开销和投资的(zz)
2008-03-24 13:27 2518既然大家都在晒收入, ... -
51、校内、占座、海内、蚂蚁,中国sns谁能笑在最后?(ZZ)
2008-03-18 09:40 121451的用户基数据称已经过亿了,它的高明之处在于,为生活情趣匮乏 ... -
牛根生刁难马云俞敏洪:再创业你们谁会行?(ZZ)
2008-03-17 11:14 1365牛根生在做考官,他要 ... -
牛根生VS史玉柱:举重若轻俩巨人 千金散尽还复来(zz)
2008-03-04 13:41 1524篮子与蛋的关系 1997年 ...
相关推荐
例如,JSR 315是关于Java Servlet 3.0规范的请求,它引入了诸如依赖注入和异步处理等功能。JSR 340则定义了Java EE 7平台的核心规范。 **3. HTML与JavaScript的关系** HTML是构建网页的基础,定义了页面结构。...
在Java EE 5中,依赖注入主要通过JSR-250(Java Specification Request 250)标准实现。 ##### Java 持久性 API 模型 Java Persistence API (JPA) 是Java EE 5 引入的一项重要技术,它提供了一个面向对象的持久性...
内容概要:本文详细介绍了基于MATLAB/Simulink的电动助力转向系统(EPS)模型的构建及其控制方法。首先,文中阐述了EPS在提升驾驶体验和安全性方面的重要意义。接着,重点讲解了四个关键模型的搭建:整车二自由度模型用于研究车辆转向特性;助力特性曲线模型确定不同驾驶条件下助力电机提供的助力力矩;助力电机模型模拟助力电机的工作过程;齿条模型描述助力电机转矩转化为车轮转向的动作。每个模型都有具体的参数设定和代码示例。此外,文章还解释了模型的输入(如前轮转角、方向盘力矩)和输出(转向助力力矩),并指出控制方法基于各模型间的输入输出关系,利用基本数学公式和逻辑判断实现。 适用人群:汽车工程领域的研究人员、工程师和技术爱好者。 使用场景及目标:适用于希望深入了解EPS工作原理的研究人员,以及需要进行EPS系统设计和优化的工程师。目标是掌握EPS系统的建模方法和控制策略,为实际项目提供理论支持和技术指导。 其他说明:文中提供了丰富的代码片段和详细的模型介绍,有助于读者更好地理解和实践。同时强调了EPS对于提高驾驶安全性和舒适性的重要性。
实训商业源码-帝国cms7.5 7.2 UTF-8移动端同步插件-酷网站-论文模板.zip
内容概要:本文详细介绍了基于Lasso分位数回归的数据回归预测方法。首先阐述了Lasso分位数回归作为一种结合Lasso回归与分位数回归的统计方法,能够在处理变量选择和模型复杂度方面发挥重要作用。接着解释了其基本原理,即在分位数回归基础上加入Lasso正则化项,从而确保模型既能良好拟合数据,又能有效避免过拟合现象。随后讨论了具体实施流程,从数据预处理到最终预测,涵盖了特征选择、模型构建以及参数优化等多个环节。最后强调了该方法在多个行业(如金融、医疗)的实际应用场景及其潜在价值。 适合人群:对统计学、机器学习有一定了解的研究人员和技术爱好者。 使用场景及目标:适用于需要精确预测并同时考虑多维度因素影响的场合,特别是在面对高维数据时,希望通过减少冗余变量来提高预测准确性的情况。 其他说明:文中提到的方法不仅限于特定领域,而是可以在多种不同类型的预测任务中发挥作用,为决策提供科学依据。
这段代码实现了一个 三维状态的扩展卡尔曼滤波 (Extended Kalman Filter, EKF) 算法。通过生成过程噪声和观测噪声,对真实状态进行滤波估计,同时对比了滤波前后状态量的误差和误差累积分布曲线。 只有一个m文件,下载后使用MATLAB打开运行即可,带误差输出。
毕业设计-百川多公众号集字福袋 2.0.5开源-整站商业源码.zip
实训商业源码-多商家营销活动平台V1.3.9小程序前后端完整全开源解密源码-论文模板.zip
ISC大作业论文
毕业论文-在线进销存-整站商业源码.zip
毕业设计-步数宝步数换购小程序 7.8.1-整站商业源码.zip
实训商业源码-叮咚-门店会员卡小程序4.8.2开源-论文模板.zip
毕业论文-芸众圈子社区V1.7.6 开源版-整站商业源码.zip
内容概要:本文探讨了多智能体强化学习(MARL)在配电网有功电压控制中的应用。文中介绍了将电压约束转化为势垒函数的方法,并在Dec-POMDP框架下对七种最先进的MARL算法进行了大规模实验。实验表明,设计合理的电压势垒函数对于提高电压控制效果至关重要。此外,作者还建立了开源环境,旨在促进电力社区和MARL社区的合作,推动MARL算法的实际应用。 适合人群:从事电力系统自动化、智能电网研究的专业人士,以及对多智能体系统和强化学习感兴趣的科研人员。 使用场景及目标:适用于需要优化配电网电压控制的场景,特别是希望通过软件手段而非硬件升级来提升电力质量和缓解电力拥塞的情况。目标是展示MARL在电力系统中的潜力,并为后续研究提供工具和支持。 其他说明:文章不仅讨论了理论和技术细节,还包括大量代码片段,帮助读者理解和实践MARL在电压控制中的具体应用。
内容概要:本文基于PFC3D(Particle Flow Code 3D)软件,详细探讨了岩石注浆过程中的破坏现象及其背后的机理。首先介绍了注浆破坏的复杂性,指出这是由材料特性、地质构造和计算机模拟技术共同决定的。接着重点讲解了注浆速度和流量的调整方法,强调适当的速度和流量对于确保注浆效率和避免过度破坏的重要性。最后讨论了在不考虑渗流场的情况下,如何根据岩石结构特征选择最佳的注浆孔位置,以提高注浆效果并保护周围岩石结构。 适合人群:从事地质工程领域的研究人员和技术人员,尤其是那些希望深入了解岩石注浆过程的人。 使用场景及目标:适用于需要利用PFC3D进行岩石注浆模拟的研究项目,旨在帮助用户掌握注浆速度、流量调节技巧以及合理的注浆孔位选择方法。 其他说明:文中提供了简单的PFC3D模拟代码框架,便于读者快速上手实践。同时提醒读者注意实际操作时应结合实验室理论模型和现场具体情况来进行参数优化。
内容概要:本文详细介绍了IEEE标准节点仿真模型系列,涵盖了从简单到复杂的多个节点配置,如2机5节点、6节点、3机9节点、13节点、5机14节点、15节点、30节点、33节点、34节点、10机39节点以及69节点。所有模型均已成功调试并实现了潮流计算,适用于短路仿真、稳定性研究和电能质量研究等领域。文中还特别强调了三相等效电源的应用,这是模拟真实电力系统的关键要素之一。 适合人群:从事电力系统研究、仿真和优化的专业人士和技术人员。 使用场景及目标:①用于电力系统短路仿真的建模与分析;②评估电力系统的稳定性和可靠性;③研究电能质量问题,提升电力设备的运行效率和寿命。 阅读建议:本文提供了丰富的背景知识和具体应用场景,建议读者结合实际项目需求选择合适的模型进行深入研究和应用。
实训商业源码-【超人】积分商城 5.2.26-论文模板.zip
实训商业源码-思创兼职小程序V6.7.6 开源版-论文模板.zip
2025年手绘风格毕业设计答辩模板范文
内容概要:本文档详细介绍了使用C语言实现常用的数据结构和算法。首先阐述了算法与数据结构的重要性,并具体讲解了链表、栈、队列、二叉树、图等数据结构的实现方法及其操作函数。接着深入探讨了快速排序和二分查找这两种高效的排序与查找算法,提供了完整的代码示例并解释了每个部分的作用。最后还讨论了图结构的深度优先搜索(DFS)和广度优先搜索(BFS)遍历算法,强调了内存管理和防御性编程的重要性。所有代码示例均可直接编译运行,建议在Linux环境下使用gcc编译测试。 适合人群:具备一定编程基础,尤其是熟悉C语言的初学者或有一定经验的研发人员。 使用场景及目标:①帮助读者理解并掌握常见的数据结构(如链表、栈、队列、二叉树、图)及其基本操作;②通过实际编码练习提高读者对经典算法(如快速排序、二分查找)的理解;③培养良好的编程习惯,如内存管理和防御性编程。 阅读建议:由于文档包含大量代码片段和详细的实现步骤,读者应边阅读边动手实践,尝试编译和运行提供的代码示例,同时注意理解每段代码背后的逻辑和设计思想。此外,建议读者关注文档中提到的编程规范和最佳实践,以提升自身的编程技能。