Home>

Implementation:

Use the filter function of filter to filter out * .jsp,Determine if there is a corresponding.html file, if there is no corresponding one.html file, read its contents and write to the specified one.html file, then jump to the corresponding.html. If there is a corresponding one.html file, jump directly to the corresponding.html.

Code:

jspfilter.java:

import java.io.bufferedreader;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.net.httpurlconnection;
import java.net.url;
import javax.servlet.filter;
import javax.servlet.filterchain;
import javax.servlet.filterconfig;
import javax.servlet.servletexception;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
public class jspfilter implements filter {
  public void destroy () {
    //todo automatically generates method stubs
  }
  public void dofilter (servletrequest request, servletresponse response,      filterchain chain) throws ioexception, servletexception {
    httpservletrequest hreq=(httpservletrequest) request;
    httpservletresponse hresp=(httpservletresponse) response;
    string name=hreq.getrequesturl (). substring (
          hreq.getrequesturl (). lastindexof ("/") + 1,          hreq.getrequesturl (). lastindexof ("."));
    if (hreq.getrequesturl (). indexof (". jsp")!=-1&&(null == hreq.getparameter ("type") || hreq.getparameter ("type"). equals (""))) {
        hresp.sendredirect (hreq.getcontextpath () + "/conversion?name =" + hreq.getrequesturl ());
        return;
    }
    chain.dofilter (request, response);
  }
  public void init (filterconfig arg0) throws servletexception {
    //todo automatically generates method stubs
  }
}

conversionservlet.java:

import java.io.bufferedreader;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.httpurlconnection;
import java.net.url;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
public class conversionservlet extends httpservlet {
  public conversionservlet () {
    super ();
  }
  public void destroy () {
    super.destroy ();//just puts "destroy" string in log
    //put your code here
  }
  public void doget (httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    response.setcontenttype ("text/html");
    printwriter out=response.getwriter ();
    //---- Get the requested URL
    string reqtarget=request.getparameter ("name");
    //---- specify the html name corresponding to jsp
    string name=reqtarget.substring (reqtarget.lastindexof ("/") + 1, reqtarget.lastindexof ("."));
    //--- determine if there is a corresponding html file
    file file=new file (request.getrealpath ("/") + name + ".html");
    if (! file.exists ()) {//-------- If there is no corresponding html file
      try {
        file.createnewfile ();//-------- create html file
        //------- write the contents of jsp to the corresponding html file
        inputstream in;
        stringbuffer sb=new stringbuffer ("");
      //---- note here,The requested url cannot be accessed directly, if it is accessed directly,End up in an endless loop
        url url=new java.net.url (reqtarget.tostring () + "?type=11");
        httpurlconnection connection=(httpurlconnection) url
            .openconnection ();
        connection.setrequestproperty ("user-agent", "mozilla/4.0");
        connection.connect ();
        in=connection.getinputstream ();
        java.io.bufferedreader breader=new bufferedreader (
            new inputstreamreader (in, "gbk"));
        string currentline;
        fileoutputstream fos=new fileoutputstream (file);
        while ((currentline=breader.readline ())!=null) {
          sb.append (currentline);
          fos.write (currentline.getbytes ());
        }
        if (null!=breader)
          breader.close ();
        if (null!=fos)
          fos.close ();
        //--------------- Go to the html page corresponding to jsp
        response.sendredirect (request.getcontextpath () + "/" + name + ".html");
      } catch (exception e) {
        e.printstacktrace ();
      }
    } else {
      //---------- If the specified html exists, jump directly to the specified html page
      response.sendredirect (request.getcontextpath () + "/" + name + ".html");
    }
  }
  public void dopost (httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    doget (request, response);
  }
  public void init () throws servletexception {
    //put your code here
  }
}

web.xml configuration:

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <filter>
  <filter-name>jspfilter</filter-name>
  <filter-class>com.beanutil.jspfilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>jspfilter</filter-name>
  <url-pattern>*. Jsp</url-pattern>
 </filter-mapping>
 <servlet>
  <servlet-name>conversion</servlet-name>
  <servlet-class>com.beanutil.conversionservlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>conversion</servlet-name>
  <url-pattern>/conversion</url-pattern>
 </servlet-mapping>
</web-app>
jsp
  • Previous PHP to implement online address book function (with source code)
  • Next Smarty advanced application cache operation skills analysis