Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot Open Excel file in Browser. + JSF + JSP + Bean

 
roger malik
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a class where the following fuction is written:
================================================
public String getMyCSV()
{
HttpServletResponse Response;
try
{
Response = (HttpServletResponse)getExternalContext().getResponse();

ServletOutputStream outs = Response.getOutputStream();

Response.setContentType("application/vnd.ms-excel");
Response.setHeader("Content-Disposition", "attachment; filename=\"log.csv\"");

Response.setHeader("Cache-Control", "public");

Response.setHeader("pragma", "");
try {
outs.print(getCSVOutput());
} catch (IOException ioe) {
debugLog("Error exporting file", ioe);
}


--------------------------------------->
I have another file which is accessed in the browser to access this function:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title></title>
</head>
<body>
<f:view>
<hutputText value="#{logReport.myCSV}"/>
</f:view>
</body>
</html>


------->
Observed: when this JSP file is accessed i get the following error:
Exception occured while loading the page: Servlet response already use stream, Writer not possible.

I was suspecting the following:
the mime-mapping was not available in the web.xml so i added the following:
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>

I would be glad if someone could give me some advice on this.
Regards
 
Sid Murrey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the JSP is being translated by the application container, it has already opened an output stream and you cannot write your own data to it. As you seem to serve static content, why don't you just create a simple file-serving servlet and provide a link to it on the JSP page? Or even easier, just statically serve the file using a webserver or the application container's ability to serve static content.
 
roger malik
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont have access to the source file for the bean which im accessing.
I have decompiled the file to view the code.

Here is the complete code for this function:

public String getExportToCSV()
{
HttpServletResponse Response;
try
{
Response = (HttpServletResponse)getExternalContext().getResponse();

ServletOutputStream outs = Response.getOutputStream();

Response.setContentType("application/vnd.ms-excel");
Response.setHeader("Content-Disposition", "attachment; filename=\"logReport.csv\"");

Response.setHeader("Cache-Control", "public");

Response.setHeader("pragma", "");
try {
outs.print(getMYCSV());
} catch (IOException ioe) {
debugLog("Error exporting file", ioe);
}
outs.flush();
outs.close();

getFacesContext().responseComplete();
} catch (Exception e) {
debugLog("Error exporting file", e);
}
return "";
}

public String getMYCSV()
{
String output = "";

output = output + getDisplayString("messages.faults", "reportTimestamp");
output = output + ",";
output = output + getDisplayString("messages.faults", "reportSystemName");
output = output + ",";
output = output + getDisplayString("messages.faults", "reportCategory");
output = output + ",";
output = output + getDisplayString("messages.faults", "reportEventSeverity");
output = output + ",";
output = output + getDisplayString("messages.faults", "reportEventCode");
output = output + ",";
output = output + getDisplayString("messages.faults", "reportEventMessage");
output = output + ",";
output = output + getDisplayString("messages.faults", "reportContext");
output = output + ",";
output = output + getDisplayString("messages.faults", "reportExceptionInfo");
output = output + System.getProperty("line.separator");

if (this.logs != null)
for (int i = 0; i < this.logs.size(); ++i) {
LogEntry logRecord = (LogEntry)this.logs.get(i);
output = output + logRecord.getTimeStamp().toString();
output = output + ",";
output = output + logRecord.getSystemName();
output = output + ",";
output = output + logRecord.getCategory();
output = output + ",";
output = output + logRecord.getLevelString();
output = output + ",";
output = output + logRecord.getEventCode();
output = output + ",";
output = output + logRecord.getEventMessage();
output = output + ",";
output = output + logRecord.getContext();
output = output + ",";
output = output + logRecord.getExceptionInfo();
output = output + System.getProperty("line.separator");
}
else
debugLog("logs is null", null);

return output;
}
-----------------------------------------------------
or should this block of code needs to be changed?
Observed:
try {
outs.print(getMYCSV());
} catch (IOException ioe) {
debugLog("Error exporting file", ioe);
}

To be changed to:
try {
return getMYCSV();
} catch (IOException ioe) {
debugLog("Error exporting file", ioe);
}



Please advice.
Thanks
 
Sid Murrey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see. I'd guess that changing to 'return' will just print everything on the page.

I have to give up here, sorry, I cannot give you further advice. Maybe someone else can help?
 
roger malik
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorted the issue.
The problem was due to the Rich Faces Filter that other application is using.It was pointing to the Faces Servlet.

Im now using url-pattern instead to point to the particular applications that are using it.

Thanks once again for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic