类: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", "");