引言:最近在看一个开源的聊天室AjaxChat 时看到一个被引用的包:javawebparts,处于好奇去看了一下,突然发现这么好的一个常用web组件不去用实在是太可惜了,下面逐一介绍,详细文档大家可以去官方文档看看,最好看它的Demo,很直观。
javawebparts的口号是:不用重新发明轮子 !
对这点我是严重支持啊,在我的身边看到N多所谓的牛人来到项目组作的第一件事就是把以前的项目推倒,重新实现自己的所谓一套先进的东西,靠,你有多先进啊,你的自己"方言"一边自我欣赏,一边还让别人接受,再来一个人又把这个推倒,循环。。。。无语.
所以,用开源吧 ,是不是绕的太远了? :) 让我们言归正传吧.
一。管理你的web配置文件信息
web的配置文件是我们经常要用到的,配置信息我们可以分为两大类,一类是最简单的"平板"型的,比如:
For an XML config file like this:
<
config
>
<
firstName
>
Frank
firstName
>
config
>
The following bean could be used:
public
class
MyConfigBean {
private
static
String firstName;
public
void
setFirstName(String inFirstName) {
firstName
=
inFirstName;
}
public
String getFirstName() {
return
firstName;
}
}
在应用中你不用去自己解析xml了,javawebparts已经帮你做好了,你只要getFirstName()就可以了,是不是很方便?
你所要做的工作就是在web.xml中引用一个listener,配置如下:
<
context-param
>
<
param-name
>
configFile
param-name
>
<
param-value
>
/WEB-INF/app_config.xml
<
/param-value
>
context-param
>
<
context-param
>
<
param-name
>
rootElement
param-name
>
<
param-value
>
config
param-value
>
context-param
>
<
context-param
>
<
param-name
>
configClass
param-name
>
<
param-value
>
javawebparts.sampleapp. SampleAppConfigBean
param-value
>
context-param
>
<
context-param
>
<
param-name
>
AjaxPartsTaglibConfig
param-name
>
<
param-value
>
/WEB-INF/ajax_config.xml
param-value
>
context-param
>
<
listener
>
<
listener-class
>
javawebparts.listener. AppConfigContextListener
listener-class
>
listener
>
这种平板型结构的数据这样就已经OK了,还有一种是复杂一点的,有层次结构,比如在Bean中可能还有collection,需要自己实现一下,也不复杂直接调用apache的Digester就可以了(实际上在javawebparts到处都有Digester的影子),需要了解的请参看 用Digester解析xml到bean
二。缓存页面
比如,我想缓存所有的gif图片,但是jpg的不缓存,web.xml中配置如下:
<
filter
>
<
filter-name
>
CacheControlFilter
filter-name
>
<
filter-class
>
javawebparts.filter. CacheControlFilter
filter-class
>
<
init-param
>
<
param-name
>
pathSpec
param-name
>
<
param-value
>
include
param-value
>
init-param
>
<
init-param
>
<
param-name
>
pathList
param-name
>
<
param-value
>
*/header1.gif, *.jpg
param-value
>
init-param
>
filter
>
<
filter-mapping
>
<
filter-name
>
CacheControlFilter
filter-name
>
<
url-pattern
>
/*
url-pattern
>
filter-mapping
>
它的后台实现机制很简单,内部部分代码如下:
((HttpServletResponse)response).setHeader(
"
Pragma
"
,
"
No-cache
"
);
((HttpServletResponse)response).setHeader(
"
Cache-Control
"
,
"
no-cache,no-store,max-age=0
"
);
((HttpServletResponse)response).setDateHeader(
"
Expires
"
,
1
);
三。图片压缩
是不是对gzip压缩很亲切? 对的,就是传说中可以压缩图片减少网络传输体积的玩意,现在你只要在web.xml中增加以下内容就可以轻松实现了:
<
filter
>
<
filter-name
>
CompressionFilter
filter-name
>
<
filter-class
>
javawebparts.filter. CompressionFilter
filter-class
>
<
init-param
>
<
param-name
>
pathSpec
param-name
>
<
param-value
>
include
param-value
>
init-param
>
<
init-param
>
<
param-name
>
pathList
param-name
>
<
param-value
>
*/bigimage2.gif
param-value
>
init-param
>
<
init-param
>
<
param-name
>
compressType
param-name
>
<
param-value
>
gzip_first
param-value
>
init-param
>
filter
>
<
filter-mapping
>
<
filter-name
>
CompressionFilter
filter-name
>
<
url-pattern
>
/*
url-pattern
>
filter-mapping
>
上面的配置表示只对bigimage2.gif的文件进行压缩,其他不用,pathSpec的属性有两种include,exclude,意思已经很明确了,相反地意思,而pathList得内部实现也是用正则表达式实现的,支持*等用法.这些内部实现你觉得复杂的话就不用管它了,反正人家已经给你实现好了,你只要说"给我压缩'美女与野兽.JPG'"就可以了
四.控制只在设定的时间范围内访问应用
比如说只允许在工作时间内可以访问应用,其他时间禁止访问,虽然不是很普遍不过这种需求还是有的,如果自己实现还是比较麻烦的,我们用现成的,配置如下:(怎么都是配置阿?对,全部是配置就可以了,难道不好吗?太爽了啊,乖乖龙地动)
<
filter
>
<
filter-name
>
AppAvailabilityFilter
filter-name
>
<
filter-class
>
javawebparts.filter. AppAvailabilityFilter
filter-class
>
<
init-param
>
<
param-name
>
pathSpec
param-name
>
<
param-value
>
include
param-value
>
init-param
>
<
init-param
>
<
param-name
>
pathList
param-name
>
<
param-value
>
*/AAFTestTarget.jsp
param-value
>
init-param
>
<
init-param
>
<
param-name
>
monday
param-name
>
<
param-value
>
0600-2359
param-value
>
init-param
>
<
init-param
>
<
param-name
>
tuesday
param-name
>
<
param-value
>
0600-2359
param-value
>
init-param
>
<
init-param
>
<
param-name
>
wednesday
param-name
>
<
param-value
>
0600-2359
param-value
>
init-param
>
<
init-param
>
<
param-name
>
thursday
param-name
>
<
param-value
>
0600-2359
param-value
>
init-param
>
<
init-param
>
<
param-name
>
friday
param-name
>
<
param-value
>
0600-2359
param-value
>
init-param
>
<
init-param
>
<
param-name
>
saturday
param-name
>
<
param-value
>
0600-2359
param-value
>
init-param
>
<
init-param
>
<
param-name
>
sunday
param-name
>
<
param-value
>
0600-2359
param-value
>
init-param
>
<
init-param
>
<
param-name
>
redirectTo
param-name
>
<
param-value
>
AAFReject.jsp
<
/param-value
>
init-param
>
<
init-param
>
<
param-name
>
bypassCheckClass
<
/param-name
>
<
param-value
>
javawebparts.sampleapp. AAFBypassCheck
param-value
>
init-param
>
filter
>
<
filter-mapping
>
<
filter-name
>
AppAvailabilityFilter
filter-name
>
<
url-pattern
>
/*
url-pattern
>
filter-mapping
>
上面的配置很明确了吧,表示在访问AAFTestTarget.jsp 文件的时候必须在设定的时间内,其他则无限制,很清楚了哦.
五。统一重定向
redirect或者forward大家都再熟悉不过了,不过这个是基于配置的,不用你写代码,比如,你希望在访问某个目录的时候自动重订向到其他文件,或者直接转到其他网站比如google.com,配置如下:
<
filter
>
<
filter-name
>
URLRedirectFilter
filter-name
>
<
filter-class
>
javawebparts.filter.URLRedirectFilter
filter-class
>
<
init-param
>
<
param-name
>
configFile
param-name
>
<
param-value
>
/WEB-INF/redirect_config.xml
param-value
>
filter
>
<
filter-mapping
>
<
filter-name
>
URLRedirectFilter
filter-name
>
<
url-pattern
>
/*
url-pattern
>
filter-mapping
>
其中redirect_config.xml配置如下:
<
config
>
<
redirect
from
="*/app/redirect_test/*"
to
="http://www.google.com"
/>
<
forward
from
="*/app/forward/test.htm"
to
="/index.jsp"
/>
config
>
很直观吧?凡是访问/app/redirect_test下的所有应用都重订向到google,太简单了,我就不多说了.
六.估算每个请求所花费的时间
这个对性能调整是很有好处的,性能的调整的前提是你了解你的系统的瓶颈在什么地方,更具体的说是你的应用哪些地方花费的资源较多,一个是监控sql的执行情况,这里推荐jdbmonitor,大家可以去看看,另一种就是本文所说的查看每个请求的执行时间,他会告诉你本请求花费了多少秒,很直观了.
配置如下:
<
filter
>
<
filter-name
>
ElapsedTimeFilter
filter-name
>
<
filter-class
>
javawebparts.filter.ElapsedTimeFilter
filter-class
>
<
init-param
>
<
param-name
>
pathSpec
param-name
>
<
param-value
>
include
param-value
>
init-param
>
<
init-param
>
<
param-name
>
pathList
param-name
>
<
param-value
>
*
param-value
>
init-param
>
filter
>
<
filter-mapping
>
<
filter-name
>
ElapsedTimeFilter
filter-name
>
<
url-pattern
>
/*
url-pattern
>
filter-mapping
>
上面的一个include ,一个pathList的*就代表了所有的应用都过滤,反之,你想暂时取消这个功能就直接把include换成exclude就行了
应用在启动后你直接观察后台日志就可以了,就这么就OK了,还要你做什么呢? 没有.
七.监控session的会话
经常碰到这样的一个情况,用户在登陆系统一段时间后session实效,但是他接着点击里面的连接,结果报异常,因为session里已经取不到数据了,常常是空指针错误,在每一个页面里都进行这种低级问题的判断是很烦人的,现在我们有新的方式可以一次性解决所有的问题,还是一段简单的配置:
<
filter
>
<
filter-name
>
SessionInactivityFilter
<
/filter-name
>
<
filter-class
>
javawebparts.filter. SessionInactivityFilter
filter-class
>
<
init-param
>
<
param-name
>
forwardTo
param-name
>
<
param-value
>
/login.jsp
<
/param-value
>
init-param
>
filter
>
<
filter-mapping
>
<
filter-name
>
SessionInactivityFilter
<
/filter-name
>
<
url-pattern
>
/*
url-pattern
>
filter-mapping
>
表示会话失效后直接重定向到登陆界面,让用户重新登陆.
八.控制访问应用的IP
出于安全考虑,过滤某些IP能或不能访问,配置如下:
<
filter
>
<
filter-name
>
IPAccessControlFilter
filter-name
>
<
filter-class
>
javawebparts.filter. IPAccessControlFilter
filter-class
>
<
init-param
>
<
param-name
>
pathSpec
param-name
>
<
param-value
>
include
param-value
>
init-param
>
<
init-param
>
<
param-name
>
pathList
param-name
>
<
param-value
>
*/IACTestTarget.jsp
param-value
>
init-param
>
<
init-param
>
<
param-name
>
addressSpec
param-name
>
<
param-value
>
include
param-value
>
init-param
>
<
init-param
>
<
param-name
>
addressList
param-name
>
<
param-value
>
localhost
param-value
>
init-param
>
<
init-param
>
<
param-name
>
forwardTo
param-name
>
<
param-value
>
/IACReject.jsp
param-value
>
init-param
>
filter
>
<
filter-mapping
>
<
filter-name
>
IPAccessControlFilter
filter-name
>
<
url-pattern
>
/*
url-pattern
>
filter-mapping
>
以上的配置效果就是只允许是从服务器上访问应用的来源(太苛刻了吧? :))
九.随机显示文字
比如每日一语,随机广告,很实用的,配置如下:
<
servlet
>
<
servlet-name
>
TextReturnerServlet
servlet-name
>
<
servlet-class
>
javawebparts.servlet. TextReturnerServlet
servlet-class
>
<
init-param
>
<
param-name
>
order
param-name
>
<
param-value
>
random
param-value
>
init-param
>
<
init-param
>
<
param-name
>
itemsFile
param-name
>
<
param-value
>
/WEB-INF/text_returner_items.xml
param-value
>
init-param
>
servlet
>
<
servlet-mapping
>
<
servlet-name
>
TextReturnerServlet
servlet-name
>
<
url-pattern
>
/textReturner
url-pattern
>
servlet-mapping
>
上面的配置表示随机显示text_returner_items.xml 中的文字,text_returner_items.xml 内容如下:
<
TextReturnerServletItems
>
<
item
>
0 - It's always darkest before the dawn
item
>
<
item
>
1 - A journey of a thousand miles begins with a single step
item
>
<
item
>
2 - The universe is like stupidity: both are infinite and actually, I'm not sure about the universe
item
>
<
item
>
3 - I believe there is intelligent life in the universe, except for some parts of New Jersey
item
>
<
item
>
4 - If your falling of a cliff, you might as well try and fly
item
>
<
item
>
5 - The cost of life is to chance the consequence
item
>
TextReturnerServletItems
>
十.随机显示图片
和上面有点类似,只是显示的内容可以是图片
<
servlet
>
<
servlet-name
>
RedirectReturnerServlet
servlet-name
>
<
servlet-class
>
javawebparts.servlet. RedirectReturnerServlet
servlet-class
>
<
init-param
>
<
param-name
>
order
param-name
>
<
param-value
>
random
param-value
>
init-param
>
<
init-param
>
<
param-name
>
itemsFile
param-name
>
<
param-value
>
/WEB-INF/redirect_returner_items.xml
param-value
>
init-param
>
servlet
>
<
servlet-mapping
>
<
servlet-name
>
RedirectReturnerServlet
servlet-name
>
<
url-pattern
>
/redirectReturner
url-pattern
>
servlet-mapping
>
redirect_returner_items.xml的内容如下:
<
RedirectReturnerServletItems
>
<
item
>
http://www.google.com/intl/en/images/logo.gif
item
>
<
item
>
/img/header1.gif
item
>
<
item
>
/img/mouse.gif
item
>
<
item
>
http://javawebparts.sourceforge.net/logo.jpg
item
>
RedirectReturnerServletItems
>
我就不用解释了吧
十一.生成文字图片
你提供文字,系统帮你生成对应的图片,是不是很有用? 比如验证码里经常需要用到,生成的效果如下:
配置如下:
<
servlet
>
<
servlet-name
>
TextRendererServlet
servlet-name
>
<
servlet-class
>
javawebparts.servlet. TextRendererServlet
servlet-class
>
<
init-param
>
<
param-name
>
fontName
param-name
>
<
param-value
>
arial
param-value
>
init-param
>
<
init-param
>
<
param-name
>
fontPoint
param-name
>
<
param-value
>
24
param-value
>
init-param
>
<
init-param
>
<
param-name
>
color
param-name
>
<
param-value
>
255,0,0
param-value
>
init-param
>
<
init-param
>
<
param-name
>
backColor
param-name
>
<
param-value
>
255,255,0
param-value
>
init-param
>
<
init-param
>
<
param-name
>
stylePlain
param-name
>
<
param-value
>
false
param-value
>
init-param
>
<
init-param
>
<
param-name
>
styleBold
param-name
>
<
param-value
>
true
param-value
>
init-param
>
<
init-param
>
<
param-name
>
styleItalic
param-name
>
<
param-value
>
true
param-value
>
init-param
>
servlet
>
<
servlet-mapping
>
<
servlet-name
>
TextRendererServlet
servlet-name
>
<
url-pattern
>
/textRenderer
url-pattern
>
servlet-mapping
>
好了,基本介绍到这里,基本感觉web常碰到的东西都有所涉及,另外还有Ajax的封装,但我个人感觉没什么大用,如果需要可以考虑别的开源产品,比如prototype或者DWR等,感觉更方便.
好东西不敢独享,如果你觉得有点用我就很开心了 :)