html页面转换jsp页面,JSP页面转换为HTML页面,动态转静态

前台运用静态页面的优点:没有数据库的交互用户接见网页加载更快,相传搜索引擎会更轻易抓取静态网页的内容,所以前台运用静态页面照样有必要的。

转化的流程:

背景servlet中取到须要转换的动态JSP页面的地点,在原位置天生一个响应的html文件。如:

test/index.jsp 如许的就能够天生一个 test/index.html文件。

接下来看详细的代码完成历程:

1.一个依据JSP文件的详细地点获得详细代码的要领,此要领是能够重复运用的,所以我们能够将它封装为到东西类里下次直接运用,详细代码以下:

public static String getCode(String httpUrl ){ //参数是一个详细的http服务器的地点

String code="";//定义返回的详细代码

try{

InputStream in;

URL url = new UTL(httpUrl);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestProperty("User-Agent", "Mozilla/4.0");

connection.connect();

in = connection.getInputStream();

InputStreamReader inputStreamReader = new InputStreamReader(in, "GBK");

BufferedReader reader = new BufferedReader(inputStreamReader);

String currentLine = "";

while((currentLine = reader.readLine()) != null ){

htmlCode += currentLine + "\n";

}catch{

reader.close();

inputStreamReader.close();

in.close();

}

return htmlCode;

}

2.以上代码完成了读取JSP文件内容并取到代码的历程,接下来要做的是将这些代码写入到一个HTML文件里,请开下面的详细要领:

public static synchronized void writeHtml(String filePath,String info){

PrintWriter writer = null;

try {

File file = new File(filePath);

boolean isExist = file.exists();

if(isExist != true){

file.createNewFile();

}else{

if(!flag.equals("NO")){

file.delete();

file.createNewFile();

}

}

writer = new PrintWriter(new FileOutputStream(file, true));

writer.print(info);

writer.close();

} catch (Exception e) {

e.printStackTrace();

}finally{

writer.close();

}

}

3.以上两个要领都会在servlet中挪用,详细参数的值会在下面说到,下面是servlet中的详细代码:

protected void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

request.setCharacterEncoding("GBK");

response.setCharacterEncoding("GBK");

response.setContentType("text/html,charset=GBK");

try {

String s = request.getRequestURL().toString();

String url = "";

String filePath = "";

url=s.substring(0, s.indexOf("/servlet"))+"/index.jsp";//index.jsp 是须要转变为静态页面的地点

String path = request.getSession().getServletContext().getRealPath("/");

filePath = path+"index.html";//天生html文件的绝对路径

String info=StringUtils.getHtmlCode(url);

StringUtils.writeHtml(filePath, info);

} catch (Exception e) {

e.printStackTrace();

}

}

以上代码就简朴的完成了JSP转换为HTML的历程。个中的两个要领也能够写为一个要领。程度有限,请斧正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值