Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp file name issue  RSS feed

 
gendhe ranjith kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i am using a jsp to generate excel file

when user clicks save it saved to client machine.

if the user clicks open the excel file will open with "jsp file name "
if i click save as then excel file name taken as jsp file name.

in our case the file name should be what we passed to response.setContentType();

please give proper solution for this.

thanks & regards
ranjith
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
grk kumar wrote:please give proper solution for this.

Step 1: move the code from a JSP to a servlet.
Step 2: show us the servlet code, in particular the response headers
 
gendhe ranjith kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

thanks for giving response

we are using Oracle Application11i .
Server is "Apache Server".

if we move the code to servlet.
we have to give servlet path in "httd.conf" file.

we do not have permission to give servlet path in "httd.conf".

code:


<%
try
{

String fileName=request.getParameter("reportName").toString()+"-"+request.getParameter("fileName").toString().substring(0,request.getParameter("fileName").toString().indexOf(".csv"));
setResponseHeader(response, fileName);
int flag=Integer.parseInt(request.getParameter("flag").toString());
CreateExcel(response.getOutputStream(),flag,request.getParameter("fileName"),session.getValue("eis_home"),request.getParameter("reportName"));

%>

<% }
catch (Exception ex)
{
ex.printStackTrace();

}


%>
<%!
//method for set content type
private void setResponseHeader(HttpServletResponse response, String resultXLS)
{
response.setContentType("application/vnd.ms-excel; charset=UTF-8");
response.setHeader("content-disposition", "inline; filename=\"" + resultXLS + "\"");

}
%>

 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, it looks ugly, but it might possibly work.
What happens when you run this? What "doesn't work" about it?


>if we move the code to servlet.
>we have to give servlet path in "httd.conf" file.
really?
To add a new servlet to your web app you need to edit the APACHE configuration? But not a JSP?
Sounds fubar to me.

Can you edit your own web.xml file?
Heck, you could edit your web.xml file to add a servlet-mapping to say "actually this url is served by a servlet. It only looks like a JSP"


The main issue I think you would hit is the fact that the JSP will declare a response writer, and write to it, while you also call getOutputStream and write to it.
As you should know, you can't call both getWriter and getOutputStream. And the JSP is calling getWriter() because a JSP does that.
That is the primary reason you should use a servlet for binary output. JSPs are intended for text output.

Because of this, anything that you include in the JSP outside of <% scriptlet %> tags (including newlines!!!) will be echoed out to the response, potentially corrupting the file you are sending.
The best solution: move it to a servlet
The hacky solution which MIGHT work in a JSP: get rid of all white space outside of the <% tags %>

ie.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!