Hello David,
I was just going through some reference in the net and
tried HttpServletRequest/HttpServletResponse
and it worked for me.But if i go through the Portlet
API and use PortletRequest/PortletResponse it doesn't give me the option
of setHeader
and setContenytType.
The "content-disposition" is helping me get the save-As
dialog.
As you have mentioned that the "response" stream has
the data already present and it is aggregating the data that i write.
I am using a servlet take my text area data,setting the
header to word and writing to the output stream.
Isn't this the way we used to follow when we used JSP
and servlets.I have tried the same combination with JSP and servlets it
works wonderfully with no
hassles.
Is there a elegant way of doing what i am trying to
acheive.??????
Please show me the way.
Best Regards,
Pallavi
________________________________
From: Nebinger, David [mailto:[EMAIL PROTECTED]
Sent: Wednesday, November 29, 2006 2:56 AM
To: MyFaces Discussion
Subject: RE: response.Header() not working in JSF Portlet
Hmm, well first of all, since you're running within a portlet I'm
actually surprised you're not getting class cast exceptions when casting
the request and response objects to
HttpServletRequest/HttpServletResponse; the portal should be ensuring
that they are PortletRequest/PortletResponse objects, and not
HttpServletRequest/HttpServletResponse.
However, if you can forward to your word servlet, you're passing in the
given request/response objects so why take the time to try to get to the
text area through the value binding? It should appear as a request
parameter that you could access directly (you'd have to enumerate your
parameter names to find one ending with ":text1" as the parm names will
have the various view root info encoded into them plus portal namespace
stuff).
Have you tried that?
As far as the whole page as output, I'd guess that a) there is already
some data in the response buffer before you start generating your word
doc and/or b) you're really still within the portal itself; it is
calling your portlet on it's own behalf and aggregating the resulting
data from this one call with the response information from the rest of
the portlets embedded on the page.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, November 23, 2006 8:35 AM
To: [email protected]
Subject: RE: response.Header() not working in JSF Portlet
Hi David,
Thank you for your suggestions.
I have now included the servlet and JSF portlet in
the same war file and deployed on Websphere Portal Server.
But currently the saving of the text area contents
into word document is happening but it saves the entire portlet page
instead of only the text area contents.
Could you help me in my last minute hitch with the
code.
1) JSF Portlet Page:
------------------------
<h:inputTextarea id="text1"
value="#{pc_TextAreaView.textAreaValue}" rows="10" cols="50" />
<h:commandButton id="save1" value="Save with
Servlet" action="#{pc_TextAreaView.SaveAsWord}" />
2) Backing bean Save method:
----------------------------------------
public void SaveAsWord() {
System.out.println("I invoke this");
FacesContext context =
FacesContext.getCurrentInstance();
ExternalContext ctx =
context.getExternalContext();
try{
HttpServletResponse response =
(HttpServletResponse)ctx.getResponse();
HttpServletRequest request =
(HttpServletRequest)ctx.getRequest();
RequestDispatcher view =
request.getRequestDispatcher("/WordServ1");
view.forward(request,response);
}catch(Exception e){e.printStackTrace();}
3) Servlet to handle the saving in word document
------------------------------------------------------------------
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
FacesContext facesContext =
FacesContext.getCurrentInstance();
ValueBinding vb =
facesContext.getApplication().createValueBinding("#{pc_TextAreaView}");
TextAreaView mybean = (TextAreaView)
vb.getValue(facesContext);
String mytextarea = mybean.getTextAreaValue();
String filen = "default.doc";
response.setContentType("application/vnd.ms-word");
response.setHeader(
"content-disposition","attachment;name=\""+filen+"\";filename=\""+filen+
"\"");
response.setHeader("Cache-Control", "no-cache");
try{
byte[] buf=new byte[4*1024];
InputStream inStream=new
ByteArrayInputStream(mytextarea.getBytes());
OutputStream outStream=response.getOutputStream();
int sizeRead;
while ( ( sizeRead=inStream.read(buf, 0, buf.length) ) !=
-1 )
{
outStream.write(buf, 0, sizeRead);
}
inStream.close();
outStream.close();
}
catch(IOException ex){ ex.printStackTrace();}
}
}
Best Regards,
Pallavi
________________________________
From: Nebinger, David [mailto:[EMAIL PROTECTED]
Sent: Wednesday, November 22, 2006 8:25 PM
To: MyFaces Discussion
Subject: RE: response.Header() not working in JSF Portlet
To expand on what Stephan provided, in order to share
application scope between your JSF application and a non-JSF servlet,
they need to be deployed in the same war file and entered in the same
web.xml.
Combining them in this way will provide the connectivity you'd
need based upon Stephan's original response.
-----Original Message-----
From: Strittmatter, Stephan
[mailto:[EMAIL PROTECTED]
Sent: Wednesday, November 22, 2006 6:49 AM
To: MyFaces Discussion
Subject: AW: response.Header() not working in JSF
Portlet
Hi Pallavi,
I use Liferay on Tomcat. The servlet I included within
my portlet war. Not in a separate war.
Sorry for my short answer, but we have currently system
tests with our customer...
Regards, Stephan
________________________________
Von: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
Gesendet: Mittwoch, 22. November 2006 08:08
An: [email protected]
Betreff: RE: response.Header() not working in JSF
Portlet
Hi Stephan,
My concepts are not very clear
regarding the portal server.
I am using RAD as my IDE and Wepsphere
portal server and application server(WAS).
Which Portal server are you using.Are
you using the same combination.
I wanted to know whether you are
creating a seperate war file to deploy your servlet on WAS.
Then invoking a call to your servlet
from your portlet.
Can you show me some light on this grey
area.
Best Regards,
Pallavi
________________________________
From: Strittmatter, Stephan
[mailto:[EMAIL PROTECTED]
Sent: Tuesday, November 21, 2006 9:29 PM
To: MyFaces Discussion
Subject: AW: response.Header() not working in JSF
Portlet
Hi Pallavi,
within a portlet it is not allowed to chenge the
response header. Otherwise a portlet would damage the portal around your
portlet.
I had this problem also and created a servlet, which I
assigned with the special extention. The only problem was, that I had to
include the part in a verbatim tag to have a form with an own defined
target probably there is a different more elegant way...:
<f:verbatim>
<form action="${request.contextPath}/download.xml"
target="_blank" method="post">
<input type="submit" value="Download" title="Download
XML file" id="execute" style="width:7em;" class="portlet-form-button" />
<input type="hidden" name="execute" value="execute" />
</form>
</f:verbatim>
In the web.xml you have to define then the servlet and
assign it to the extention.
In the servlet you can access the backing bean by
following to get some values:
FacesContext c = FacesContext.getCurrentInstance();
Application application = c.getApplication();
MyBackingBean bBean= (MyBackingBean)
application.createValueBinding("#{MyBackingBean}").getValue(c);
Regards,
Stephan
________________________________
Von: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
Gesendet: Montag, 20. November 2006 12:25
An: [email protected]
Betreff: response.Header() not working in JSF Portlet
Hi All,
I have a JSF portlet where i want to save the
contents of my Text area on the click of a button to a word document.
(IBM Websphere portal)
<h:inputTextarea id="text1"
value="#{pc_TextAreaView.textAreaValue}" rows="10" cols="50" />
<h:commandButton id="save" value="Save As Word"
action="#{pc_TextAreaView.SaveAsWord}"/>
In my page bean i have written the follwoing code but
the response.setHeader() doesn't seem to work.
I am not getting any pop dialog and i cannot save my
file.
Please let me know what is wrong in this code
snippet.How can i make it work in my portlet.
========================================================================
=======
public String SaveAsWord() {
FacesContext context =
FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse)context.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-Disposition",
"attachment; filename=myfile.doc");
response.setHeader("Cache-Control", "no-cache");
try{
byte[] buf=new byte[4*1024];
InputStream inStream=new
ByteArrayInputStream(textAreaValue.getBytes());
OutputStream outStream=response.getOutputStream();
int sizeRead;
while ( ( sizeRead=inStream.read(buf, 0,
buf.length) ) != -1 )
{
outStream.write(buf, 0, sizeRead);
}
inStream.close();
outStream.close();
}
catch(IOException ex){
ex.printStackTrace();
}
return null;
}
Best Regards,
Pallavi
The information contained in this electronic message and any attachments
to this message are intended for the exclusive use of the addressee(s)
and may contain proprietary, confidential or privileged information. If
you are not the intended recipient, you should not disseminate,
distribute or copy this e-mail. Please notify the sender immediately and
destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient
should check this email and any attachments for the presence of viruses.
The company accepts no liability for any damage caused by any virus
transmitted by this email.
www.wipro.com
The information contained in this electronic message and any attachments
to this message are intended for the exclusive use of the addressee(s)
and may contain proprietary, confidential or privileged information. If
you are not the intended recipient, you should not disseminate,
distribute or copy this e-mail. Please notify the sender immediately and
destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient
should check this email and any attachments for the presence of viruses.
The company accepts no liability for any damage caused by any virus
transmitted by this email.
www.wipro.com
The information contained in this electronic message and any attachments
to this message are intended for the exclusive use of the addressee(s)
and may contain proprietary, confidential or privileged information. If
you are not the intended recipient, you should not disseminate,
distribute or copy this e-mail. Please notify the sender immediately and
destroy all copies of this message and any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient
should check this email and any attachments for the presence of viruses.
The company accepts no liability for any damage caused by any virus
transmitted by this email.
www.wipro.com
The information contained in this electronic message and any attachments to
this message are intended for the exclusive use of the addressee(s) and may
contain proprietary, confidential or privileged information. If you are not the
intended recipient, you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately and destroy all copies of this message and
any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should
check this email and any attachments for the presence of viruses. The company
accepts no liability for any damage caused by any virus transmitted by this
email.
www.wipro.com