捕获当前的JSF页面内容 [英] Capture current JSF page content
问题描述
我想捕获当前页面并将其发送到将其转换为pdf的应用程序.
I want to capture the current page and send it to an application that converts it to pdf.
这就是我正在使用的:
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是记录错误页面.
Since the website requires authentication, the pdf generated is the loging error page.
是否有一种方法可以捕获使用当前用户会话的页面内容?
Is there a way to capture the page's content that uses the current user's session?
谢谢.
推荐答案
只需在与当前请求相同的HTTP会话中请求页面即可.如果您的Web应用程序支持URL重写(默认情况下),则只需将会话ID附加为jsessionid
路径片段:
Just request the page in the same HTTP session as the current request. If your webapp supports URL rewriting (as by default), then just append session ID as jsessionid
path fragment:
String sessionId = ((HttpSession) externalContext.getSession()).getId();
InputStream input = new URL("http://localhost:8080/context/page.jsf;jsessionid=" + sessionId).openStream();
// ...
或者如果您的Web应用程序不接受URL重写,而仅接受Cookie,则可以按照通常的方式将其设置为请求Cookie:
Or if your webapp doesn't accept URL rewriting, but accepts cookies only, then set it as a request cookie the usual way:
URLConnection connection = new URL("http://localhost:8080/context/page.jsf").openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
InputStream input = connection.getInputStream();
// ...
请注意,我删除了setDoOutput()
,因为您似乎对执行POST请求不感兴趣.
Note that I removed setDoOutput()
since you don't seem to be interested in performing a POST request.
这篇关于捕获当前的JSF页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!