我想捕获当前页面并将其发送到将其转换为pdf的应用程序。在java web中捕获当前页面内容
这是我在用的:
FacesContext facesContext=FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)
facesContext.getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
// RequestPrinter.debugString();
response.reset();
// download a pdf file
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename="+new Date().toString()+".pdf");
prince.setVerbose(true);
prince.setLog(logFile);
try{
//getPath() to the page the user is currently on
URL pagePath=new URL(this.getPath());
URLConnection urlConnection = pagePath.openConnection();
urlConnection.setDoOutput(true);
int length = urlConnection.getContentLength();
//Lets use inputStream
BufferedInputStream bis=new BufferedInputStream(urlConnection.getInputStream());
response.setContentLength(length);
//this.getPageUsingJSoup().data().getBytes();
//call prince and pass params for inputstream outputStream
prince.convert(bis,response.getOutputStream());
urlConnection.getInputStream().close();
}catch(MalformedURLException mu){
mu.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
facesContext.responseComplete();
由于网站需要身份验证,生成的PDF是登录电子错误页面。
有没有办法捕捉使用当前用户会话的页面内容?
预先感谢您。
Hi @BalusC,我们的webapp支持URL重写。我尝试了你的第一个建议,但我仍然得到错误登录页面。该Web应用程序使用Seam身份验证和王子(我用来打印PDF的应用程序)是我的Windows机器上的安装。有没有办法通过登录或将新的urlconnection绑定到已验证的会话。谢谢 –
我不熟悉Seam身份验证,所以我不能详细介绍,但理论上很可能需要额外的请求Cookie。我会监视您的普通网页浏览器(按Chrome/IE10/Firebug中的F12,并检查“网络”/“网络”部分)和网络服务器之间的HTTP流量,以查看请求标头中是否没有其他Cookie。如果是这样,那么你应该在你的程序化请求中模拟相同的cookie。 – BalusC
,在浏览器上(清除缓存并再次登录后),我只有一个jsessionid cookie并且会话存储为空。在tomcat管理器中,Active HttpSessions信息下,活动会话具有列名:会话ID,猜测区域,猜测用户名,创建时间,上次访问时间,使用时间,非活动时间和TTL。 –