具体实现:
利用Filter的过滤功能把*.jsp过滤出来,判断是否存在对应的.html文件,如果不存在对应的.html文件,则将其内容读出写入指定的.html文件,再跳转到对应的.html即可。如果存在对应的.html文件,则直接跳转到对应的.html即可。
代码:
JspFilter.java
package com.zh.jsp.filter; import java.io.IOException; 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; /** * @name * @author zhanghao * @create date Nov 1, 2011 */ public class JspFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; System.out.println("访问的url:::" + request.getRequestURL()); String name = request.getRequestURL().substring( request.getRequestURL().lastIndexOf("/") + 1, request.getRequestURL().lastIndexOf(".")); if (request.getRequestURL().indexOf(".jsp") != -1 && (null == request.getParameter("type") || request .getParameter("type").equals(""))) { response.sendRedirect(request.getContextPath() + "/conversion?name=" + request.getRequestURL()); return; } chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } }
ConversionServlet.java
package com.zh.jsp.filter; 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.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @name * @author zhanghao * @create date Nov 1, 2011 */ public class ConversionServlet extends HttpServlet { protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { //设置输出格式 response.setContentType("text/html"); PrintWriter out = response.getWriter(); //获取请求的URL String requestUrl = request.getParameter("name"); //指定对应JSP的HTML名称 String name = requestUrl.substring(requestUrl.lastIndexOf("/") + 1,requestUrl.lastIndexOf(".")); //判断是否存在对应的HTML文件 File file = new File(request.getRealPath("/") + name + ".html"); if (!file.exists()) { // --------如果不存在对应的HTML文件 try { //创建HTML文件 file.createNewFile(); //将JSP的内容写入对应的HTML文件内 InputStream in; StringBuffer sb = new StringBuffer(""); //注意这里,不能直接访问请求的URL,如果直接访问的话,会陷入死循环 URL url = new java.net.URL(requestUrl.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, "UTF-8")); 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(); //转到与JSP对应的HTML页 response.sendRedirect(request.getContextPath() + "/" + name + ".html"); } catch (Exception e) { e.printStackTrace(); } } else { //如果存在指定的HTML,直接跳转到指定的HTML页 response.sendRedirect(request.getContextPath() + "/" + name + ".html"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doGet(req, resp); } @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); } @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); } }
WEB.XML配置:
<?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.zh.jsp.filter.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.zh.jsp.filter.ConversionServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>conversion</servlet-name> <url-pattern>/conversion</url-pattern> </servlet-mapping> </web-app>