=相关新闻=
 =本站服务=
 =最新作品=
海西品牌网
模拟整形网
更多技术文档...
 =站长信息=
NAME: ヤ從伈゛開始
WEB: http://www.987114.cn
QQ: 540573223
EMAIL: 540573223@qq.com
AGE: MiMi
怀旧论坛
计数统计
当然位置: 海西品牌网 >>java编程>> 技术文档 >>正文          保护视力色:默认色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色)

java静态页面生成,静态新闻页面发布<一>

发布时间:2009-10-28 浏览数:加载中…

类:CallHtml

package servlet;

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

import javax.servlet.http.HttpServletRequest;

public class CallHtml {
 public static boolean callOnePage(HttpServletRequest request,String fileName, String path,
   String realName, String realPath) {
  try {
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
   System.out.println(basePath);
   String str = basePath+"toHtmlPath?file_name="
     + fileName + "&&path=" + path + "&&realName=" + realName
     + "&&realPath=" + realPath;
   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("连接失败 ");
    return false;
   } else {
    System.out.println("连接成功了 ");
    return true;  
   }
  } catch (Exception e) {
   // TODO: handle exception
  }
  return false;

 }
}

类:ToHtmlPath

package servlet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import common.Logger;

public class ToHtmlPath extends HttpServlet {
 private static final long serialVersionUID = 1L;
 private Logger logger = Logger.getLogger(ToHtmlPathclass);

 public void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  // 编码方式,可以配置到 web.xml 里。
  String encoding = "gbk";
  // 得到真实的请求地址
  // String templatePath = simpleURLReWrite(request);
  String templatePath = request.getParameter("file_name");
  String realPath = request.getSession().getServletContext().getRealPath(
    "/");
  // 想要生成的静态html文件的名字
  String htmlName = request.getParameter("realName");
  // 静态html的名字,包含绝对路径
  String path = request.getParameter("path");
  String cachFileName = realPath + File.separator +path+"\\" + htmlName;
  logger.debug("cachFileName = " + cachFileName);

  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  final Out stream = new Out(os);
  
  final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,
    encoding));
  Rep rep = new Rep(response,stream,pw);
   
  logger.debug("HtmlCreatorServlet RequestDispatcher = " + templatePath);
  // 使用 RequestDispatcher 去处理真正的请求。 
  RequestDispatcher dispatcher = getServletContext()
    .getRequestDispatcher(templatePath);
  dispatcher.include(request, rep);
  pw.flush();
  FileOutputStream fos = null;
  try {
   if (os.size() == 0) {
    // 如果请求的地址无效,那么就发送一个404错误。
    response.sendError(HttpServletResponse.SC_NOT_FOUND, "");
   } else {
    // 生成静态文件,并且显示这个静态文件
    fos = new FileOutputStream(cachFileName);
    os.writeTo(fos);
    dispatcher = getServletContext().getRequestDispatcher(
      "//"+path+"//" + htmlName);
    dispatcher.forward(request, response);
    // response.sendRedirect(url);
   }
  } finally {
   if (fos != null) {
    fos.close();
   }
  }
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to
  * post.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException
  *             if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

 类:Out

package servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.ServletOutputStream;

public class Out extends ServletOutputStream {
 ByteArrayOutputStream os = new ByteArrayOutputStream();
 public ByteArrayOutputStream getOs() {
  return os;
 }
 public void setOs(ByteArrayOutputStream os) {
  this.os = os;
 }
 public Out(ByteArrayOutputStream os) {
  super();
  this.os = os;
 }

 public void write(int b) throws IOException {
  // TODO Auto-generated method stub
  this.os.write(b);
 }

}

类:Rep

package servlet;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;


public class Rep extends HttpServletResponseWrapper {
 Out stream;
 PrintWriter pw;
 
 public Out getStream() {
  return stream;
 }
 public void setStream(Out stream) {
  this.stream = stream;
 }
 public PrintWriter getPw() {
  return pw;
 }
 public void setPw(PrintWriter pw) {
  this.pw = pw;
 }
 
 public Rep(HttpServletResponse response, Out stream, PrintWriter pw) {
  super(response);
  this.stream = stream;
  this.pw = pw;
 }

 public Out getOutputStream() {
  return this.stream;
 }
 public PrintWriter getWriter() {
  return this.pw;
 }
 
}

  web.xml配置

  <servlet>
    <servlet-name>ToHtmlPath</servlet-name>
    <servlet-class>servlet.ToHtmlPath</servlet-class>
  </servlet>

<servlet-mapping>
    <servlet-name>ToHtmlPath</servlet-name>
    <url-pattern>/toHtmlPath</url-pattern>
  </servlet-mapping>

在你的新闻发布save时,调用方法。

CallHtml.callOnePage(request,filename,path,realName,realPath);

request:HttpServletRequest  request

filename  "/new.do?task=detNew&sid=1", 

path "news"//存放静态页面目录

realName:"100.html" //静态页面名称

 CallHtml.callOnePage(request, "/new.do?task=detNew&sid=1", "news", "100.html", "");

本文首发:java编程(http://www.987114.cn:80/blog/),转载请注明出处。
文章来源于:java编程 http://www.987114.cn:80/blog/

 

评论
上海市的网友: 2009-12-19
太帅了
越南的网友: 2009-12-21
太帅了
科威特的网友: 2009-12-21
太帅了
美国/加拿大的网友: 2009-12-27
太帅了
美国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
太帅了
英国的网友: 2009-12-29
太帅了
英国的网友: 2009-12-29
太帅了
英国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
太帅了
美国的网友: 2009-12-29
glacial evaporation adderall xr methane adipex-p adipex no rx windows allowing buy adipex online no prescription adipex online
 =友情链接=
海西品牌网 模拟整形网 海西论坛 爱美尔整形
版权所有:海路传媒