Dynamic JSP page to generate static HTML, dynamic jsp static html
This example describes how to generate static HTML on a dynamic JSP page. Share it with you for your reference. The details are as follows:
Specific implementation:
Use the Filter function to set * filters. If the corresponding. html file exists, the corresponding. html file can be directly copied.
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 ja Vax. servlet. servletResponse; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class JspFilter implements Filter {public void destroy () {// TODO Automatic Generation Method stub} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {HttpServletRequest hreq = (HttpServletRequest) request; HttpServle TResponse 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. http ServletResponse; 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. getWrite R (); // ---- obtain the request URL String reqTarget = request. getParameter ("name"); // ---- specify the HTML name of the corresponding JSP String name = reqTarget. substring (reqTarget. lastIndexOf ("/") + 1, reqTarget. lastIndexOf (". "); // --- determine whether the corresponding HTML File exists. file = new File (request. getRealPath ("/") + name + ". html "); if (! File. exists () {// -------- if the corresponding HTML file does not exist try {file. createNewFile (); // -------- create an HTML file // ------- write JSP content into the corresponding HTML file InputStream in; StringBuffer sb = new StringBuffer (""); // ---- note that you cannot directly access the requested URL here. If you access the URL directly, it will be stuck in an endless 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 corresponding to JSP. sendRedirect (request. getContextPath () + "/" + name + ". html ");} catch (Exception e) {e. printStackTrace () ;}} else {// ---------- if a 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>
I hope this article will help you with JSP program design.