jsp页面的静态化处理方法一

jsp:

   index.jsp 首页,显示产品信息查询的连接。

   product.jsp 显示产品列表。

   detail.jsp 显示每个产品的详细信息。

 

java

   Product .java  构造model数据,用户页面显示。

   ProductDao.java 也是为了构造数据。直接用个列表生成数据

   EncodingFilter.java 为了显示中文。

   ToHtml.java 一个Servlet主要是为了根据link里面的file_name 参数生成html文件的。参考下面的连接。

                        http://blog.csdn.net/zoucui/archive/2009/03/26/4027378.aspx

   JspToHtmlFilter.java 对请求的Jsp做过滤,如果是在配置的范围内,就直接转向静态文件(前提,静态文件存在)

   HtmlUtil.java 用户配置哪些jsp页面需要转换成html。用Servlet的ServletPath匹配。

   CallHtml.java  用户客户端请求配置好的jsp连接,让服务端的servlet去执行文件的转换。

 

 

index.jsp文件如下:

<%@ page language="java" contentType="text/html;  charset=gb2312"%>
<%@ page pageEncoding="gb2312"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<head/>
<body>
<br>
<a href="<%=request.getContextPath()%>/product.jsp">产看产品信息</a>
</body>
<html>

 

 

product.jsp :

 

<%@ page language="java" contentType="text/html;  charset=gb2312"%>
<%@ page pageEncoding="gb2312"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@page import="com.liuxt.jsphtml.util.*" %>

<%request.setAttribute("productList",ProductDao.getInstance().getProductList()); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<head/>
<body>
<br>产品信息如下:
<table border="1">
<th>产品ID</th><th>产品名称</th>
<c:forEach items="${productList}" var="product">
<tr>
  
  <td><a href="<%=request.getContextPath()%>/detail.jsp?id=<c:out value='${product.id}'/>"><c:out value="${product.id}" /></a></td>
  <td><c:out value="${product.name}" /></td>
</tr>
</c:forEach>
</table>

</body>
<html>

 

detail.jsp

 

 

<%@ page language="java" contentType="text/html;  charset=gb2312"%>
<%@ page pageEncoding="gb2312"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@page import="com.liuxt.jsphtml.util.*" %>

<%

  String productId=request.getParameter("id");
  Product p=ProductDao.getInstance().getProduct(productId);
  request.setAttribute("product",p);
%>

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<head/>
<body>
<br>产品信息如下:
<br>产品ID
<br><c:out value="${product.id}"/>

<br>产品名称
<br><c:out value="${product.name}"/>

<br>产品描叙
<br><c:out value="${product.describe}"/>

<br>产品价格
<br><c:out value="${product.price}"/>

</body>
<html>

 

Product.java

 

package com.liuxt.jsphtml.util;

public class Product {
 
 String id;
 String name;
 String describe;
 String price;

 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Product(String id, String name) {
  super();
 
  this.id = id;
  this.name = name;
 }
 
 public Product() {
 }
 public String getDescribe() {
  return describe;
 }
 public void setDescribe(String describe) {
  this.describe = describe;
 }
 public String getPrice() {
  return price;
 }
 public void setPrice(String price) {
  this.price = price;
 }
 
}


ProductDao.java

 

package com.liuxt.jsphtml.util;

import java.util.ArrayList;
import java.util.List;

public class ProductDao {
 
 private static List productList=new ArrayList();
 private static ProductDao productDao=new ProductDao();
 
 @SuppressWarnings("unchecked")
 private ProductDao(){
 
  Product product1=new Product();
  product1.setId("T01");
  product1.setName("草莓");
  product1.setDescribe("好吃有好看");
  product1.setPrice("5.00");

  Product product2=new Product();
  product2.setId("T02");
  product2.setName("芒果");
  product2.setDescribe("新鲜的,刚从福建运过来的");
  product2.setPrice("4.00");
 
  Product product3=new Product();
  product3.setId("T03");
  product3.setName("大樱桃");
  product3.setDescribe("绝对的,吃了还想买的。好品牌的,大红灯。");
  product3.setPrice("15.00");
  productList.add(product1);
  productList.add(product2);
  productList.add(product3);
 }
 
 public static ProductDao getInstance(){
  return productDao;
 }

 
 @SuppressWarnings("unchecked")
 public List getProductList(){
  return  productList;

 }
 
 public Product getProduct(String id){
  Product p=null;
  for(int i=0;i<productList.size();i++){
   p=(Product) productList.get(i);
   if(p!=null){
    if (p.getId().equals(id)){
     return p;
    }
   }
  }
  return null;
 }
 
}

 


EncodingFilter.java

 

package com.liuxt.jsphtml.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {

 private FilterConfig filterConfig;

 
 public void init(FilterConfig filterConfig) throws ServletException {
  this.filterConfig=filterConfig;
 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {

  request.setCharacterEncoding("gb2312");
  chain.doFilter(request, response);

 }

 public void destroy() {

 }

}


HtmlUtil.java

 

package com.liuxt.jsphtml.util;

import java.util.HashSet;
import java.util.Set;

public class HtmlUtil {
 
 public static final Set<String> urlSet=new HashSet<String>();
 
 static{
  urlSet.add("/product.jsp");
  urlSet.add("/index.jsp");
  urlSet.add("/test/index.jsp");
 
 }
 
 

}


JspToHtmlFilter.java

 

package com.liuxt.jsphtml.util;

import java.io.File;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JspToHtmlFilter implements Filter {

 private FilterConfig filterConfig;
 
 
 public void destroy() {

 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {

   //前台发送的请求,过滤处理
  HttpServletRequest res=(HttpServletRequest)request;
  String jspURL=res.getServletPath();
  System.out.println("jspURL========"+jspURL);
 
  if (HtmlUtil.urlSet.contains(jspURL)) {
     String realpath = filterConfig.getServletContext().getRealPath("/");
     String file_name=jspURL;
     file_name=file_name.replace("/","_");
     file_name=file_name.replace(".","_");
     String fileName = realpath + "staticPages/" + file_name + ".html";
     System.out.println("fileName in filter=="+fileName);
     File file = new File(fileName);
     // 判断文件是否存在
     if (file.exists()) {// 不存在 生成
    // 文件存在,直接转发
    // 转向到该html页面
    HttpServletResponse hsr = (HttpServletResponse) response;
    hsr.sendRedirect("staticPages/" + file_name+".html");
    return;
     }
   }
  //参数为空,继续chain中的其它filter.
  chain.doFilter(request, response);
 
 }

 public void init(FilterConfig filterConfig) throws ServletException {
  this.filterConfig = filterConfig;
  //加入初始化的连接,将一些定义好的连接,需要转化为html的,放入集合中。
 }

}


CallHtml.java

 

package com.liuxt.jsphtml.util;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;

public class CallHtml {
 
 
 public static void main(String[] args) {
  CallHtml callHtml=new CallHtml();
  callHtml.makeHtml();
 }
 
 
    private void makeHtml() {
    
     Iterator iter=HtmlUtil.urlSet.iterator();
     while(iter.hasNext()){
      String path=(String)iter.next();
      callOnePage(path);
     }
 }


 public  void callOnePage(String fileName) {
           try {
            String str = "http://localhost:8080/htmlweb/toHtml?file_name=" + fileName;
            System.out.println("link========="+str);
            int httpResult;
            URL url = new URL(str);
            URLConnection connection = url.openConnection();
            connection.connect();
            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
            httpResult = httpURLConnection.getResponseCode();
            if (httpResult != HttpURLConnection.HTTP_OK) {
             System.out.println("没有连接成功");
            } else {
             System.out.println("连接成功了 ");
            }
           } catch (Exception e) {
            e.printStackTrace();
           }
          }


}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
 <display-name>htmlweb</display-name>
 <servlet>
  <description></description>
  <display-name>toHtml</display-name>
  <servlet-name>toHtml</servlet-name>
  <servlet-class> com.liuxt.jsphtml.util.ToHtml</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>toHtml</servlet-name>
  <url-pattern>/toHtml</url-pattern>
 </servlet-mapping>
 
 <filter>
  <filter-name>JspToHtmlFilter</filter-name>
  <filter-class>
   com.liuxt.jsphtml.util.JspToHtmlFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>JspToHtmlFilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
 </filter-mapping>
 
 
 <filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>
   com.liuxt.jsphtml.util.EncodingFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>EncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值