JSP generate HTML (simple method, use only one Serlvet)

Source: Internet
Author: User

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> ");
}
}
Web. xml
Add
<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 not under wwwroot, under wwwroot/PP
Write
<A href = "../web/tohtm? URLs =/kszx. jsp "> generate static pages </a>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.