How the servlet generates static HTML

Source: Internet
Author: User
* * To change this template, choose Tools |
 Templates * and open the template in the editor.
* * Package servlets;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
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;  /** * * @author Administrator/public class Tohtmlpath extends HttpServlet {/** * processes for
    Both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet Request * @param response servlet response */protected void ProcessRequest (HTTPSERVL Etrequest request, HttpServletResponse response) throws Servletexception, IOException {} @Override public V 
            OID Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
            Response.setcontenttype ("Text/html;charset=utf-8");
            PrintWriter out = Response.getwriter ();
            String url = ""; 
            String name = ""; 
            ServletContext ServletContext = Getservletcontext ();
            Final String Context_path=servletcontext.getrealpath ("/"); 
            String file_name = Request.getparameter ("file_name");//The JSP file you want to access, such as NEWS.JSF.  File_name=file_name+ ". JSP";
            Only the JSP page generation is currently set up.

file_name such as: filedetail.jsf?fileid=56. If there is a parameter, there is only one argument. 
            and takes the name of the parameter as the file name. String build_name = Request.getparameter ("Build_name");//file name to save. such as index;
            Can not have this parameter. String Path = request.getparameter ("path"); The path to the JSP file you want to access. such as news. 
            Can not have this parameter. String Real_path = ReQuest.getparameter ("Real_path");//You want to save the path of the file, such as html-news. Note that you can not have this parameter. 
             if (Build_name==null | | build_name.equals ("")) {int a = 0; 
             A = File_name.indexof ("=") + 1; 
             Build_name = File_name.substring (a); if (Build_name.indexof (".") 
             >0) {build_name = file_name.substring (0, File_name.indexof (".")); 
            The page to be accessed is constructed under//. 
             if (Path ==null | | path.equals ("")) {URL = "/" + file_name;//This is the JSP file you want to generate HTML for, such as} else { url = "/" + path + file.separator + file_name;//This is the JSP file you want to generate HTML for, such as}//below constructs the file name to be saved, and the path 
            。
            1, if there are Real_path, then save under Real_path. 
            2, if a path is saved under path. 
            3, otherwise, save in the root directory. if (Real_path = null | | real_path.equals ("")) {if (path = = NULL | | Path.equals ("")) {name =context_path + file.separator + build_name + ". html"//This is the generated HTML filename, such as index.htm. Description: Confconstants.context_path for your context path. else {Name =context_path + file.separator + PATH + file.separator + build_name +. html 
             ;//This is the generated HTML filename, such as index.htm. } else {Name =context_path + file.separator + real_path +file.separator + Bui 
            Ld_name + ". html";//This is the generated HTML filename, such as index.htm. 
            //access the requested page and generate the specified file. RequestDispatcher dispatcher = servletcontext.getrequestdispatcher (URL);
            For this URL request final Bytearrayoutputstream Byteos = new Bytearrayoutputstream (); Final Servletoutputstream stream = new Servletoutputstream () {//only handles byte streams, while PrintWriter is the process character stream, and @Override// Rewrite the inside of the method public void write (byte[] data, int offset, int length) {byteos.write (data, offset, l
             Ength); @Override//Rewrite the inside method public void write (int b) tHrows IOException {byteos.write (b);
            }             };
            Final PrintWriter printw; printw= New PrintWriter (New OutputStreamWriter (Byteos, "UTF-8"));
 
            For encoding conversion, the setting is valid when the output stream is converted from a bit stream to a character streams. HttpServletResponse rep = new Httpservletresponsewrapper (response) {@Override public Servletou 
             Tputstream Getoutputstream () {return stream;
             @Override public PrintWriter getwriter () {return printw; 
          
            }             }; 
            Dispatcher.include (Request, Rep);
            Printw.flush (); FileOutputStream Fileos = new FileOutputStream (name);
            Write the contents of the JSP output to xxx.htm//out.print (name);
            Byteos.writeto (Fileos);
            Fileos.close ();
           Out.print ("Publish success!"); }//<editor-fold defaultstate= "collapsed" desc= HttpServlet method. Click the + sign on the left to edit the code. 
">/**    * Handles the HTTP <code>GET</code> method. * @param request servlet Request * @param response servlet response */@Override protected void doget (Http ServletRequest request, HttpServletResponse response) throws Servletexception, IOException {ProcessRequest (re
    Quest, Response);
    }/** * Handles HTTP <code>POST</code> method. * @param request servlet Request * @param response servlet response */@Override protected void DoPost (HTT Pservletrequest request, HttpServletResponse response) throws Servletexception, IOException {ProcessRequest (r
    Equest, response);
    }/** * Returns A short description of the servlet.
    * * @Override public String Getservletinfo () {return ' short description '; }//</editor-fold>}

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.