This example describes a method for generating static HTML for dynamic JSP pages. Share to everyone for your reference. Specifically as follows:
Specific implementation:
Filter filtering function of filter to *.jsp, to determine whether there is a corresponding. html file, if there is no corresponding. html file, read out its contents to the specified. html file, and then jump to the corresponding. html. If there is a corresponding. 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 generate method stubs} public void Dofilter (SERVL Etrequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {HTTPSERVL
Etrequest 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 generate 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} Publ IC void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {R
Esponse.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
----Get the requested URL String reqtarget = request.getparameter ("name"); ----Specifies the HTML name of the corresponding JSP String name = reqtarget.substring (reqtarget. LastIndexOf ("/") + 1,reqtarget.lastindexof ("."));
---to determine if there is a corresponding HTML file = filename = new file (Request.getrealpath ("/") + name + ". html"); if (!file.exists ()) {//--------if there is no corresponding HTML file try {file.createnewfile ();
--------Create an HTML file//-------write the contents of the JSP into the corresponding HTML file InputStream in;
StringBuffer sb = new StringBuffer ("");
----Note here, you cannot directly access the requested URL, if direct access, will fall into the dead loop 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 response.sendredirect (Request.getcontextpath () + "/" +name + ". html") corresponding to the JSP;
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, Ioexce
ption {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/"
Java ee "
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>
I hope this article will help you with your JSP programming.