静态网站网页的一种做法

我要说的做法是:
用后台任务程序根据制定的条件 将jsp页面的内容复制到html上,使用者看到的都是html页面!

下面的一个类是用于将jsp页面的内容复制到html上

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

/**
*
* To change the template for this generated type comment go to Window>Preferences>Java>Code Generation>Code and Comments
*/
public class MakeHtml {
private static long star = 0;
private static long end = 0;
private static long ttime = 0;

// 返回html代码
public static String getHtmlCode(String httpUrl) {
Date before = new Date();
star = before.getTime();
String htmlCode = "";
try {
InputStream in;
URL url = new java.net.URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection = (HttpURLConnection) url.openConnection();
//connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, "GBK"));
String currentLine;
while ((currentLine = breader.readLine()) != null) {
htmlCode += currentLine;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Date after = new Date();
end = after.getTime();
ttime = end - star;
System.out.println("执行时间:" + ttime + "秒");
}
return htmlCode;
}

// 存储文件
public static synchronized void writeHtml(String filePath, String info, String flag) {

PrintWriter pw = null;
try {
File writeFile = new File(filePath);
boolean isExit = writeFile.exists();
if (isExit != true) {
writeFile.createNewFile();
} else {
if (!flag.equals("NO")) {
writeFile.delete();
writeFile.createNewFile();
}
}
//pw = new PrintWriter(new FileOutputStream(filePath, true));//true表示接着原来的往下写
pw = new PrintWriter(new FileOutputStream(filePath));
pw.println(info);
pw.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
pw.close();
}
}

public static void main(String[] args) {
String url = "http://192.168.61.113:8080/makehtml/index.jsp";
writeHtml("http://192.168.61.113:8080/index.html", getHtmlCode(url), "NO");
}
}


如果想制定时间就更新html,那可以考虑用java自带的java.util.Timer、TimerTask
可以参考
http://jimjiang.iteye.com/blog/110790或
http://blog.csdn.net/tianhappy/archive/2006/08/27/1126841.aspx

还有一种更简单的时间调度框架Quartz,可参考
http://www.ibm.com/developerworks/cn/java/j-quartz/index.html

我用Quartz简单地写了一个例子,比如每5分钟更新一次html网页,运行下面的CronTriggerRunner类就可以了(Quartz需要jar包,要自己去下载http://www.opensymphony.com/quartz/download.action)
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.huanglq.tools.MakeHtml;

public class SimpleQuartzJob implements Job {

public SimpleQuartzJob() {
}

public void execute(JobExecutionContext context) throws JobExecutionException {
String url = "http://192.168.61.113:8080/makehtml/index.jsp";
MakeHtml.writeHtml("http://192.168.61.113:8080/index.html", MakeHtml.getHtmlCode(url), "NO");
}
}

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class CronTriggerRunner {

public void task() throws SchedulerException
{
// Initiate a Schedule Factory
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = schedulerFactory.getScheduler();

// current time
long ctime = System.currentTimeMillis();
System.out.println(ctime);

// Initiate JobDetail with job name, job group, and executable job class
JobDetail jobDetail = new JobDetail("jobDetail2", "jobDetailGroup2", SimpleQuartzJob.class);
// Initiate CronTrigger with its name and group name
CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup2");
try {
// setup CronExpression
CronExpression cexp = new CronExpression("* 0/5 * * * ?");
// Assign the CronExpression to CronTrigger
cronTrigger.setCronExpression(cexp);
} catch (Exception e) {
e.printStackTrace();
}
// schedule a job with JobDetail and Trigger
scheduler.scheduleJob(jobDetail, cronTrigger);

// start the scheduler
scheduler.start();
}

public static void main (String args[])
{
try {
CronTriggerRunner qRunner = new CronTriggerRunner();
qRunner.task();
} catch (Exception e)
{
e.printStackTrace();
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值