如何在server端打印jsp返回的html页面源代码?

launch401 2006-07-18 05:01:05
我想放在servlet filter里面做,但是不知道如何实现。
...全文
242 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
silverseven7 2006-07-25
  • 打赏
  • 举报
回复
发的反对法
launch401 2006-07-18
  • 打赏
  • 举报
回复
感谢kevinliuu提供思路,现在已经能够实现了,下面是filter的代码,因为http请求的话可能是servlet/jsp或者图片等资源,所以在web.xml里面不能写<url-pattern>/*</url-pattern>,我用的是类似于struts的统一入口servlet,所以我的url-pattern就指向那个统一入口了:

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
String requestUrl = req.getRequestURL().toString();
String requestQuery = req.getQueryString();
System.out.println("requetURL:"+requestUrl);
System.out.println("requestQuery:"+requestQuery);
System.out.println("###########begin watch filter###################");
String test = request.getParameter("test");
System.out.println("test:" + test);

if (test == null){
//如果test为空,则说明是正常调用页面;否则是获取页面的,则不需要再继续。


try {
System.out.println("*****begin get html*****");
String urlString;
if (requestQuery == null){
urlString = requestUrl + "?test=true";
}else{
urlString = requestUrl + "?test=true&" + requestQuery;
}
URL url = new URL(urlString);
URLConnection con = url.openConnection();
Enumeration enu1= req.getHeaderNames();
while (enu1.hasMoreElements()){
String headerName = (String)enu1.nextElement();
String headerValue = req.getHeader(headerName);
con.addRequestProperty(headerName, headerValue);
}
InputStream in = con.getInputStream();
// buffer the input to increase performance
in = new BufferedInputStream(in);
// chain the InputStream to a Reader
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1) {
System.out.print((char) c);
}

}
catch (MalformedURLException ex) {
System.err.println(ex);
}
System.out.println("*****end get html*****");

}

System.out.println("###########end watch filter###################");

chain.doFilter(request, response);

}
  • 打赏
  • 举报
回复
我想你可以在返回HTML前用文件操作函数在SERVER上生成要返回的HTML文件,再打印该文件;或直接用文件操作函数直接转向输出到打印机上
funcreal 2006-07-18
  • 打赏
  • 举报
回复
httpwatch是个ie插件,可以完全解决你的问题。
他可以拦截各种请求,包括请求头,请求体,返回码等等一切。
kevinliuu 2006-07-18
  • 打赏
  • 举报
回复
全局过滤器,过滤所有的http请求,在dopost或doget方法中,通过

request.getHeader("reffer") 能得到请求的url

再利用java.net.URL类进行对指定url的网页抓代码,然后打印

最后再分发请求
launch401 2006-07-18
  • 打赏
  • 举报
回复
没人知道吗?
顶一下
launch401 2006-07-18
  • 打赏
  • 举报
回复
说思路也行
Javafan0 2006-07-18
  • 打赏
  • 举报
回复
做过,但代码忘了,帮顶.
launch401 2006-07-18
  • 打赏
  • 举报
回复
随便,我就是调试用
kevinliuu 2006-07-18
  • 打赏
  • 举报
回复
打印到控制台?

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧