日期:2014-05-17 浏览次数:20566 次
package com.servlet;?? 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;?? public class JspFilter implements Filter {?? ??? public void destroy() {?? ??????? // TODO 自动生成方法存根?? ??? }?? ??? 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 自动生成方法存根?? ??? }?? }?? ConversionServlet .java?? package com.servlet;?? 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();?? ??????? //----获取请求的URL?? ??????? String reqTarget = request.getParameter("name");?? ??????? //----指定对应JSP的HTML名称?? ??????? String name = reqTarget.substring(reqTarget.lastIndexOf("/") + 1,reqTarget.lastIndexOf("."));?? ?????????? ??????? //---判断是否存在对应的HTML文件?? ??????? File file = new File(request.getRealPath("/") + name + ".html");?? ?????????? ??????? if (!file.exists()) {?? //--------如果不存在对应的HTML文件?? ??????????? try {?? ?????????????????? ??????????????? file.createNewFile();?? //--------创建HTML文件?? ??????????????? //-------将JSP的内容写入对应的HTML文件内?? ??????????????? InputStream in;?? ??????????????? StringBuffer sb = new StringBuffer("");?? ??????????? //----注意这里,不能直接访问请求的URL,如果直接访问的话,会陷入死循环?? ??????????????? URL url = new java.net.URL(reqTarget.toString()+"?type=11");?? ??????????????? HttpURLConnection connection = (HttpURLConnection) url?? ??????????????????????? .openConnection();?? ??????????????? connection.setRequestProperty("User-Agent",
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
|