|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java主要分三块,j2se:java的基础核心语言。j2me:java的微型模块,专门针对内存小,没有持续电源等小型设备。j2ee:java的企业模块,专门针对企业数据库服务器的连接维护。
6.克制站点过滤器
假如你但愿在你的过滤器检测到不一般的非常而半途中止前面的过滤历程时,可如许做:
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,
FilterChainchain)throwsServletException,IOException{
HttpServletRequestreq=(HttpServletRequest)request;
HttpServletResponseres=(HttpServletResponse)response;
if(isUnusualCondition(req)){
res.sendRedirect("http://www.somesite.com");
}else{
chain.doFilter(req,res);
}
}
下例是一个克制站点过滤器,假如不但愿某些站点会见你的网站,你能够在web.xml的param-value中列出它的站点,然后使用下面的道理跳出惯例过滤,给出克制会见的页面。
BannedAccessFilter.java- packagecom.zj.sample;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.net.MalformedURLException;
- importjava.net.URL;
- importjava.util.HashSet;
- importjava.util.StringTokenizer;
- importjavax.servlet.Filter;
- importjavax.servlet.FilterChain;
- importjavax.servlet.FilterConfig;
- importjavax.servlet.ServletException;
- importjavax.servlet.ServletRequest;
- importjavax.servlet.ServletResponse;
- importjavax.servlet.http.HttpServletRequest;
- publicclassBannedAccessFilterimplementsFilter{
- privateHashSet<String>bannedSiteTable;
- /**
- *Denyaccessiftherequestcomesfromabannedsiteorisreferredhere
- *byabannedsite.
- */
- publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,
- FilterChainchain)throwsServletException,IOException{
- System.out.println("WithinBannedAccessFilter:FilteringtheRequest...");
- HttpServletRequestreq=(HttpServletRequest)request;
- StringrequestingHost=req.getRemoteHost();
- StringreferringHost=getReferringHost(req.getHeader("Referer"));
- StringbannedSite=null;
- booleanisBanned=false;
- if(bannedSiteTable.contains(requestingHost)){
- bannedSite=requestingHost;
- isBanned=true;
- }elseif(bannedSiteTable.contains(referringHost)){
- bannedSite=referringHost;
- isBanned=true;
- }
- if(isBanned){
- showWarning(response,bannedSite);
- }else{
- chain.doFilter(request,response);
- }
- System.out.println("WithinBannedAccessFilter:FilteringtheResponse...");
- }
- /**
- *Createatableofbannedsitesbasedoninitializationparameters.
- *Rememberthatversion2.3oftheservletAPImandatestheuseofthe
- *Java2Platform.Thus,itissafetouseHashSet(whichdetermines
- *whetheragivenkeyexists)ratherthantheclumsierHashtable
- *(whichhasavalueforeachkey).
- */
- publicvoidinit(FilterConfigconfig)throwsServletException{
- bannedSiteTable=newHashSet<String>();
- StringbannedSites=config.getInitParameter("bannedSites");
- //Defaulttokenset:whitespace.
- StringTokenizertok=newStringTokenizer(bannedSites);
- while(tok.hasMoreTokens()){
- StringbannedSite=tok.nextToken();
- bannedSiteTable.add(bannedSite);
- System.out.println("Banned"+bannedSite);
- }
- }
- publicvoiddestroy(){}
- privateStringgetReferringHost(StringrefererringURLString){
- try{
- URLreferringURL=newURL(refererringURLString);
- return(referringURL.getHost());
- }catch(MalformedURLExceptionmue){//Malformedornull
- return(null);
- }
- }
- //Replacementresponsethatisreturnedtousers
- //whoarefromorreferredherebyabannedsite.
- privatevoidshowWarning(ServletResponseresponse,StringbannedSite)
- throwsServletException,IOException{
- response.setContentType("text/html");
- PrintWriterout=response.getWriter();
- StringdocType="<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0"
- +"Transitional//EN">
- ";
- out.println(docType+"<HTML>
- "
- +"<HEAD><TITLE>AccessProhibited</TITLE></HEAD>
- "
- +"<BODYBGCOLOR="WHITE">
- "+"<H1>AccessProhibited</H1>
- "
- +"Sorry,accessfromorvia"+bannedSite+"
- "
- +"isnotallowed.
- "+"</BODY></HTML>");
- }
- }
复制代码 <p>
什么时候上述的三种开发工具能和三为一,什么时候java的竞争力才更强,才有机会拉拢更多的程序员投入到对java的开发上,因为到时的开发工具将会比.net的更简单。还有一点也很关键,什么时候java推出的jsf能成为真正意义上的标准。 |
|