c(jsp+servlet+xml)
package ningxia.bl.admin.contentrele;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ningxia.dao.admin.contentrele.ContentTreleMysql;
import ningxia.dao.admin.newsclassmang.NewsClassMysql;
import ningxia.dao.admin.newsmang.NewsMysql;
import ningxia.utils.db.DBConnectionDAO;
import ningxia.vo.admin.NewsBean;
import ningxia.vo.admin.NewsClassBean;
import ningxia.vo.admin.PageBean;
import ningxia.vo.SessionABC;
public class StaticPageServlet extends HttpServlet
{
private Connection conn;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
//System.out.println("**********project start***********");
String id = request.getParameter("static");
//System.out.println("The id is "+id);
int pageno = new Integer(request.getParameter("pageno")).intValue();
boolean commitflg = false;
try
{
//System.out.println("*************ready data out*********");
//建立到數據庫的鏈接
this.conn = DBConnectionDAO.getConnection();
conn.setAutoCommit(false);
//實例化ContentTreleMysql()類
ContentTreleMysql contenttrelemysql = new ContentTreleMysql(conn);
NewsClassMysql newsclassmysql = new NewsClassMysql(conn);
NewsBean news = new NewsBean();
news = contenttrelemysql.FindNew(id);
//System.out.println("**********The For Cycle is start*********");
/**
* 讀取數據庫存儲的新聞記錄
*/
String newsid = news.getNews_id();
//System.out.println("---------The newsid is "+newsid+"-----------------");
String title = news.getNews_title();//讀取新聞標題
String newssubtitle = news.getNews_subtitle();//讀取新聞副標題
String newsauthor = news.getNews_author();//讀取新聞作者姓名
String newsaddtime = news.getNews_addtime();//新聞添加時間
String newssource = news.getNews_source();//新聞採集地點
String newscontent = news.getNews_content();//新聞內容
String classid = news.getNews_classid();//新聞所屬欄目ID
String newstemppath = news.getNews_newstemppath();//新聞模板路徑
String newstempname = news.getNews_newstemplet();//讀取新聞模文件名
String newsvideoflg = news.getNews_videoflg();
//System.out.println("The video flg is "+newsvideoflg);
int newslevel = news.getNews_level();//取得該新聞的欄目深度
//判斷是否是視頻新聞
String videourl = "";
String videodisplay = "";
if(newsvideoflg.equals("1")==true)
{
//該新聞是視頻新聞
videodisplay = "block";
videourl = news.getNews_videosavepath()+news.getNews_videoname();
//System.out.println("The video url is "+videourl);
}
else
{
videodisplay = "none";
videourl = "";
}
//控制新聞標題的長度
String newstitle = "";
if(title.length()>=12)
{
newstitle = title.substring(0,11)+"......";
}
else
{
newstitle = title;
}
//根據newslevel來判斷生成的靜態頁引用樣式的路徑
String path = "";String pathurl = "";
for(int i = 1;i<=newslevel;i++)
{
path = path + "../";
}
pathurl = path+"../";
//根據newslevel來判斷
String newsclassid = "";String pareid = "";
newsclassid = news.getNews_classid();
pareid = newsclassid.substring(0,3);
//System.out.println("The pareid is "+pareid);
String pareclasscname = newsclassmysql.FindNewsClassCnameByClassID(pareid);
//System.out.println("The classcnname is "+classcname);
String classcnname = newsclassmysql.FindNewsClassCnameByClassID(newsclassid);
//初始化NewsClassBean
NewsClassBean newsclass = new NewsClassBean();
newsclass = contenttrelemysql.FindClassId(classid);
//String newsclass_enname = newsclass.getNewsclass_enname();//查詢欄目英文名稱
String newsclass_savefilepath = newsclass.getNewsclass_savefilepath();//查詢新聞欄目保存的路徑
String filepath = request.getRealPath("/")+newstemppath+newstempname;
String templateContent = "";
//調用時間函數
Calendar calendar = Calendar.getInstance();
String filename = String.valueOf(calendar.getTimeInMillis());
/**
* 下面的程序用來對靜態頁面生成並分頁
* 判斷新聞記錄種的</p>數量來分頁,每頁30行字符
* 用allpagenum來判斷頁面大小
* 當pagenum取餘數爲0時則用整頁顯示
* 當pagenum取餘數不爲0時則用pagenum+1頁顯示
*/
int line = 10 ;//每頁顯示10行
String arr[] = {};
//當分頁標誌即有</p>也有</br>時進行判斷分頁數
if(newscontent.split("</p>").length<=10&&newscontent.length()>=700&&newscontent.split("</br>").length>=10)
{
//用</br>來計算分頁的頁數
arr = newscontent.split("</br>");
}
else
if(newscontent.split("</br>").length<=10&&newscontent.length()>=700&&newscontent.split("</p>").length>=10)
{
//用</p>來計算分頁的頁數
arr = newscontent.split("</p>");
}
else
{
arr = newscontent.split("</p>");
}
//將數組內容按照</p>進行分割
int pagenum =arr.length/line;//需要多少頁來顯示數據
int length =arr.length;//數組長度
int other = arr.length%line;//最後一頁所剩的行數
if(other!=0)
{
pagenum+=1;
}
/**
* 將靜態頁所在路徑,靜態頁文件名稱,靜態頁所屬欄目ID存入數據庫
*/
String staticfilename = filename+"_0.htm";//首頁文件名
String staticfilepath = newsclass_savefilepath;//首頁路徑
String lp = filename+"_"+(pagenum-1)+".htm";//末頁文件名
news.setNews_staticpagepath(staticfilepath);
news.setNews_staticfilename(staticfilename);
news.setNews_staticpagenum(pagenum);
contenttrelemysql.AddStaticPage(news);
/**
* 開始生成靜態頁面
*/
int q = 0;
String content ="";
String page ="";
int h = line;
for(int k=1;k<=pagenum;k++)
{
String a = "<a href="+filename+"_"+(k-1)+".htm>"+k+"</a>";
page =page+a;
}
for(int j=1;j<=pagenum;j++)
{
if((j==pagenum)&&(other!=0))
{
//最後一頁
//取得剩餘行數
for(int n=q;n<=length-1;n++)
{
//System.out.println("The Q is "+q);
//循環取出數據
content+=arr[n];
}
//替換模板頁
FileInputStream fileinputstream = new FileInputStream(filepath);//讀取模塊文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
templateContent = templateContent.replaceAll("###path###", path);
templateContent = templateContent.replaceAll("###pathurl###", pathurl);
templateContent = templateContent.replaceAll("###newsId###", newsid);
templateContent = templateContent.replaceAll("###pareclassid###", pareid);
templateContent = templateContent.replaceAll("###pareclasscnname###", pareclasscname);
templateContent = templateContent.replaceAll("###newsclassid###", newsclassid);
templateContent = templateContent.replaceAll("###classcnname###", classcnname);
templateContent = templateContent.replaceAll("###newstitle###", newstitle);
templateContent = templateContent.replaceAll("###newssubtitle###", newssubtitle);
templateContent = templateContent.replaceAll("###newsauthor###", newsauthor);
templateContent = templateContent.replaceAll("###newsaddtime###", newsaddtime);
templateContent = templateContent.replaceAll("###newssource###", newssource);
templateContent = templateContent.replaceAll("###videodisplay###", videodisplay);
templateContent = templateContent.replaceAll("###videourl###", videourl);
templateContent = templateContent.replaceAll("###newscontent###", content);
templateContent = templateContent.replaceAll("###first###", staticfilename);
templateContent = templateContent.replaceAll("###number###", page);
templateContent = templateContent.replaceAll("###last###", lp);
String fileame_m = filename +"_"+(j-1)+".htm";
//System.out.println("The fileame is "+fileame);
String file = request.getRealPath("/")+newsclass_savefilepath+"/"+fileame_m;//生成html文件並存儲絕對路徑
//System.out.println("The file is "+file);
FileOutputStream fileoutputstream = new FileOutputStream(file);//建立文件輸出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
//System.out.println("This is the ++++"+j+"++++"+content);
}
else
{
//System.out.println("h is "+h);
for(int m=q;;)
{
content+=arr[m];
m++;
q=m;
if(q>=h)
{
//替換模板頁
FileInputStream fileinputstream = new FileInputStream(filepath);//讀取模塊文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
templateContent = templateContent.replaceAll("###path###", path);
templateContent = templateContent.replaceAll("###pathurl###", pathurl);
templateContent = templateContent.replaceAll("###newsId###", newsid);
templateContent = templateContent.replaceAll("###pareclassid###", pareid);
templateContent = templateContent.replaceAll("###pareclasscnname###", pareclasscname);
templateContent = templateContent.replaceAll("###newsclassid###", newsclassid);
templateContent = templateContent.replaceAll("###classcnname###", classcnname);
templateContent = templateContent.replaceAll("###newstitle###", newstitle);
templateContent = templateContent.replaceAll("###newssubtitle###", newssubtitle);
templateContent = templateContent.replaceAll("###newsauthor###", newsauthor);
templateContent = templateContent.replaceAll("###newsaddtime###", newsaddtime);
templateContent = templateContent.replaceAll("###newssource###", newssource);
templateContent = templateContent.replaceAll("###videodisplay###", videodisplay);
templateContent = templateContent.replaceAll("###videourl###", videourl);
templateContent = templateContent.replaceAll("###newscontent###", content);
templateContent = templateContent.replaceAll("###first###", staticfilename);
templateContent = templateContent.replaceAll("###number###", page);
templateContent = templateContent.replaceAll("###last###", lp);
String fileame_m = filename +"_"+(j-1)+".htm";
String file = request.getRealPath("/")+newsclass_savefilepath+"/"+fileame_m;//生成html文件並存儲絕對路徑
FileOutputStream fileoutputstream = new FileOutputStream(file);//建立文件輸出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
content = "";
h=line+h;
break;
}
}
}
}
NewsMysql newsmysql = new NewsMysql(conn);
PageBean pagebean = new PageBean();
pagebean = newsmysql.FindNewsByPage(pageno, classid);
conn.commit();
session.setAttribute(SessionABC.NEWSPAGEINFO, pagebean);
response.sendRedirect("admin/admin_newsmang.jsp");
}
catch (Exception e)
{
out.print(e.getMessage());
}
finally
{
if (conn != null)
{
try {
if (commitflg)
conn.rollback();
conn.close();
}
catch (SQLException sqe)
{
sqe.printStackTrace();
session.setAttribute("SessionABC.ERRORMSG", "數據庫連接關閉時發生錯誤");
response.sendRedirect("admin/error.jsp");
}
}
}
}
}
jsp生成靜態頁(收)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.