This article describes the method of generating static HTML in dynamic JSP pages. Share it for your reference. The details are as follows:
Specific implementation:
Use Filter's filtering function to filter out *.jsp to determine whether the corresponding .html file exists. If the corresponding .html file does not exist, then read out and write its contents to the specified .html file, and then jump to the corresponding .html. If the corresponding .html file exists, just jump to the corresponding .html.
Code:
:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class JspFilter implements Filter { public void destroy() { // TODO automatically generates method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hreq = (HttpServletRequest) request; HttpServletResponse hresp = (HttpServletResponse) response; String name = ().substring( ().lastIndexOf("/") + 1, ().lastIndexOf(".")); if (().indexOf(".jsp") != -1 && (null == ("type") || ("type").equals(""))) { (()+"/conversion?name="+()); return ; } (request, response); } public void init(FilterConfig arg0) throws ServletException { // TODO automatically generates method stub } }
:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class ConversionServlet extends HttpServlet { public ConversionServlet () { super(); } public void destroy() { (); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ("text/html"); PrintWriter out = (); //---- Get the requested URL String reqTarget = ("name"); //----Specify the HTML name corresponding to JSP String name = (("/") + 1,(".")); //----Determine whether there is a corresponding HTML file File file = new File(("/") + name + ".html"); if (!()) { //----------If the corresponding HTML file does not exist try { (); //-----------------Create HTML file //---------Write the content of the JSP into the corresponding HTML file InputStream in; StringBuffer sb = new StringBuffer(""); //----Note here, you cannot directly access the requested URL. If you access it directly, you will fall into a dead loop URL url = new (()+"?type=11"); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); ("User-Agent", "Mozilla/4.0"); (); in = (); breader = new BufferedReader( new InputStreamReader(in, "GBK")); String currentLine; FileOutputStream fos = new FileOutputStream(file); while ((currentLine = ()) != null) { (currentLine); (()); } if (null != breader) (); if (null != fos) (); //----------------------Go to the HTML page corresponding to JSP (()+"/"+name + ".html"); } catch (Exception e) { (); } }else{ //---------- If the specified HTML exists, jump directly to the specified HTML page (()+"/"+name + ".html"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { // Put your code here } }
Configuration:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="/xml/ns/j2ee" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/j2ee /xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>jspfilter</filter-name> <filter-class></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> </servlet-class> </servlet> <servlet-mapping> <servlet-name>conversion</servlet-name> <url-pattern>/conversion</url-pattern> </servlet-mapping> </web-app>
I hope this article will be helpful to everyone's JSP programming.