1 tohtml. Java (use it directly without modification)
Package com. jetsum. mystatic;
Import java. Io. bytearrayoutputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. outputstreamwriter;
Import java. Io. printwriter;
Import javax. servlet. requestdispatcher;
Import javax. servlet. servletcontext;
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;
Public class tohtml extends httpservlet {
Public void Service (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
String url = request. getparameter ("URLs ")! = NULL? Request. getparameter ("URLs "):"";
// URL is the JSP page for generating htm
String name = "";
Response. setcontenttype ("text/html; charset = gb2312 ");
Servletcontext SC = getservletcontext ();
System. Out. println ("request. getrealpath" + request. getrealpath (""));
Name = request. getrealpath ("") + "/index.htm";/* generate the HTM page in % server root directory %/project name/index.htm For example: D: /tomcat5.0/webapp/studyteach/index.htm. Note that this is generated in the root directory. You can generate it and put it in the folder you want to put, make sure that the path of other parts is correct. Name = request is used here. getrealpath ("") + "/kszx/kszx.htm ";*/
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 ();
Fileoutputstream Fos = new fileoutputstream (name); // write the jspoutput content to xxx.htm
OS. writeto (FOS );
FOS. Close ();
Printwriter out = response. getwriter ();
Out. Print ("<p align = center> <font size = 3 color = Red> the homepage is successfully generated! </Font> </P> ");
}
}
2 add in Web. xml
<Servlet>
<Servlet-Name> tohtm </servlet-Name>
<Servlet-class> com. jetsum. mystatic. tohtml </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> tohtm </servlet-Name>
<URL-pattern>/web/tohtm </url-pattern>
</Servlet-mapping>
Easy to use mytest. jsp (under wwwroot)
<A href = "Web/tohtm? URLs =/kszx. jsp "> Generate a static page </a> kszx. jsp: the dynamic page for generating htm
If it is not under wwwroot, it must be written under wwwroot/PP.
<A href = "../web/tohtm? URLs =/kszx. jsp "> generate static pages </a>