class="java">public String jspToHtml(String url, HttpServletRequest request,
HttpServletResponse response){
final CharArrayWriter charArray = new CharArrayWriter();
HttpServletResponse rep = new HttpServletResponseWrapper(response){
public PrintWriter getWriter() {return new PrintWriter(charArray);}
};
try {
request.getRequestDispatcher(url).include(request, rep);
rep.getWriter().flush();
return charArray.toString();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try{ rep.getWriter().close(); } catch (Exception e){}
}
}
以上代码是有效的,但我有更进一步的要求,如果是后台程序想将Jsp转成html,并没有发起请求,那该怎么处理呢?不管怎么找怎么想都搞不定,又确实不想用freemarker之类的其它技术,头疼ing。
最后只好用非常笨的方法,就是用HttpURLConnection向自己发送请求
求路过的大侠留下更优雅的解决方法。