网站首页如何静态化

网站是个投资类的门户,www.investide.cn 投资潮。
首页内容较多,有四屏多吧,数据库调用也很多,虽然数据库层做了缓存,页面上也使用了oscache,但还是感觉慢,于是想到做静态化。
用quartz来定时抓取动态页面的内容,生成静态页面
首页顶部的登录,用ajax来加载
<!-- 首页静态页的定时任务 ,每5分钟运行一次-->
	<bean id="indexHtmlJobBean" class="com.dbcapital.job.HtmlJob" singleton="false"></bean>
	<bean id="indexHtmlJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    	<property name="targetObject">
    		<ref bean="indexHtmlJobBean"/>
    	</property>
    	<property name="targetMethod">
    		<value>execute</value>
    	</property>
	</bean>	
	<bean id="indexHtmlJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="indexHtmlJob"/>
		<property name="cronExpression" value="0 0/5 * * * ?"/>
	</bean>	
	<!-- 首页静态页任务配置结束 -->

public void execute() {
		String url="http://www.investide.cn/index.do";
		String filePath=PropertyUtil.getProperty("dbcapital.root.path")+"index.html";
		//创建文件
		File file=new File(filePath);
		try{
			if(!file.exists()){
				file.createNewFile();
			}
		}catch(IOException e){}
		//生成文件
		String content=UrlfileInfo.readUrlContent(url);
		if(!StringUtil.isEmpty(content)){
			DocUtil.writeFile(content,filePath);//写静态文件
		}
	}

public static String readUrlContent(String url){
		StringBuffer sb=new StringBuffer();
		try{  
			String   strLine;  
			URL   urlObj   =   new   URL(url);  
			InputStream   streamObj   =   urlObj.openStream();  
			InputStreamReader   readerObj   =   new   InputStreamReader(streamObj,"GBK");  
			BufferedReader   buffObj             =   new   BufferedReader(readerObj);  
			while((strLine   =   buffObj.readLine())!=null) 
				sb.append(strLine+"\r");
			buffObj.close();  
		}catch(MalformedURLException   e){  
			System.err.println("url   error");  
		}catch(IOException   e){  
			System.out.println("IO   error");  
		} 
		return sb.toString();
	}


public static boolean writeFile(String str,String filePath){
		boolean result=true;
		try{
		Writer fw = new OutputStreamWriter(new FileOutputStream (filePath),"GBK");
		fw.write(str);
		fw.close();
		}catch(IOException e){
			result=false;
			System.out.println("写文件失败");
		}
		return result;
	}

ajax加载登录代码
<span id="loginInfo">正在载入,请稍后...</span>

将login.jsp的状态加载到loginInfo里
theHREF = window.top.location.href.split('/');
			hostHREF='';
			for(i=3;i<theHREF.length;i++){
				hostHREF=hostHREF+'/'+theHREF[i];
			}
			setTimeout("fillData('/top_login.jsp?fromUrl="+hostHREF+"&t="+new Date()+"','loginInfo')",1000);

替换登录内容的js
function fillData(url,elementId){
	http_request=false;
	if(window.ActiveXObject){
		try{
			http_request=new ActiveXObject("MSXML2.XMLHttp"); 
		}catch(e){
			try{
				http_request=new ActiveXObject("Microsoft.XMLHTTP"); 
			}catch(e){}
		} 
	}else if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		http_request.overrideMimeType('text/xml');
	}
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				var str=http_request.responseText;
				document.getElementById(elementId).innerHTML=str;
			} else {
				alert("服务器忙,请稍候再试!");
			}
		} else {
			document.getElementById(elementId).innerHTML="载入中,请稍候...";
		}
	};
	http_request.open('GET', url+"&t="+new Date(), true);
	http_request.send(null);
}

你可能感兴趣的:(java,Ajax,jsp,quartz,Safari)