Method of generating static page by JSP

Time:2021-10-24
Copy codeThe code is as follows:
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=””;
    String name=””;

        ServletContext sc = getServletContext();

         String   file_ name=request.getParameter(“file_name”);// The JSP file you want to access, such as index.jsp
  // Then you add parameters when accessing this servlet. For example http://localhost/toHtml?file_ name=index

         url  =  “/”+file_ name+”.jsp”;// This is the JSP file you want to generate HTML, such as
                                   // http://localhost/index.jsp Implementation results of the

         name=”/home/resin/resin-2.1.6/doc/”+file_ name+”.htm”;// This is the name of the generated HTML file, such as index.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 JSP output to xxx.htm
        os.writeTo(fos);
        fos.close();
        PrintWriter out=response.getWriter();
         out.print(“<p   align=center><font   size=3   Color = Red > the home page has been successfully generated! Andrew < / font > < / P > “);
    }
}

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]