高手近来看看啊,关于用servlet生成html页面的乱码问题!!!!
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 ";
FileOutputStream fos =null;
String 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.write(b);
}
};
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();
try
{
fos = new FileOutputStream(pName);
fos.write(value.getBytes(charset));
os.writeTo(fos);
fos.close();
}
catch (
FileNotFoundException e)
{
e.printStackTrace();
}
catch (Unsupported
EncodingException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
response.sendRedirect(name); //书写完毕后转向htm页面
}
}
这是我写的代码??
到底该怎么定义那个value才能解决生成的页面不是乱码并可以显示呢?
------解决方案--------------------