Static html page implementation class automatically generated in jsp

Source: Internet
Author: User
Tags flush http post

Example 1

The code is as follows: Copy code

Public class JspToHtml {
 
Private static String title = "title 1 ";
Private static String context = "title 2 ";
Private static String editer = "title 3 ";
 
Public static boolean jspToHtmlFile (String filePath, String htmlFile ){
 
String str = "";
Try {
FileInputStream is = new FileInputStream (filePath );
BufferedReader br = new BufferedReader (new InputStreamReader (is ));
String tempStr = "";
While (tempStr = br. readLine ())! = Null ){
Str = str + tempStr;
   }
  
Br. close ();
Is. close ();
} Catch (IOException e ){
E. printStackTrace ();
Return false;
  }
 
Try {
Str = str. replaceAll ("# title ##", title );
Str = str. replaceAll ("# context #", context );
Str = str. replaceAll ("# editer #", editer );
System. out. println (str );
File file = new File (htmlFile );
BufferedWriter writer = new BufferedWriter (new FileWriter (file ));
Writer. write (str );
Writer. close ();
  
} Catch (IOException e ){
E. printStackTrace ();
Return false;
  }
 
Return true;
 
 }
 
Public static void main (String [] args ){
String url = "D: Workspacesbase.html ";
String savePath = "d:" + 11 + ". html ";
 
JspToHtmlFile (url, savePath );
 }

}


 

Example 2

ToHtml. java (can be used directly without modification)
    
  

The code is as follows: Copy code

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 things, such as images, is correct. name = request. 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>


Example 3

The code is as follows: Copy code

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 ();

String file_name = request. getParameter ("file_name"); // The jsp file you want to access, such as index. jsp
// Add parameters when you access this servlet, such as http: // localhost/toHtml? File_name = index

Url = "/" + file_name + ". jsp"; // This is the execution result of the jsp file to generate HTML, such as // http: // localhost/index. jsp.

Name = file_name + ". htm"; // the generated HTML file name, such as index.htm.
PName = "../WebModule2/" + file_name + ". htm"; // Generate the complete html path


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 (pName); // write the jsp output content to the htm file in the specified path
OS. writeTo (fos );
Fos. close ();
Response. sendRedirect (name); // go to the htm page after writing
   }

 

}

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.