Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 关于用servlet生成静态页面的问题?
sun7bear





发贴: 16
于 2007-02-01 15:44 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
package slt;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ToHtml
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
service(request,response);
}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}

public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String url = "";
String name = "";
String pName = "";

ServletContext sc = getServletContext();

url = "/indexbak125.jsp"; //这是要生成HTML的jsp文件,如//http://localhost/index.jsp的执行结果.

name = "indexa.html"; //这是生成的html文件名,如index.htm.
pName = "/usr/tomcat/webapps/ROOT/indexa.html"; //生成html的完整路径

RequestDispatcher rd = sc.getRequestDispatcher(url);

final ByteArrayOutputStream os = new ByteArrayOutputStream();

final ServletOutputStream stream = new ServletOutputStream() {
public void write(byte[] data, int offset, int length) {
os.write(data, offset, length);
}

public void write(int b) throws IOException {
os.writeBeer;
}
};

final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response) {
public ServletOutputStream getOutputStream() {
return stream;
}

public PrintWriter getWriter() {
return pw;
}
};
rd.include(request, rep);
pw.flush();
FileOutputStream fos = new FileOutputStream(pName); //把jsp输出的内容写到指定路径的htm文件中
os.writeTo(fos);
fos.close();
response.sendRedirect(name); //书写完毕后转向htm页面
}
}
我用这个servlet生成了静态页面,可为什么生成出来的静态页面是乱码,而且还不能转向呢?




话题树型展开
人气 标题 作者 字数 发贴时间
4215 关于用servlet生成静态页面的问题? sun7bear 2007 2007-02-01 15:44

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923