Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ServletOutputStream to Browser OK in NS, but not IE??

Reply
Thread Tools

ServletOutputStream to Browser OK in NS, but not IE??

 
 
TomG
Guest
Posts: n/a
 
      05-03-2004
Hi All,

I am replacing a Netscape Application Server appLogic with a servlet.
The servlet is supposed to take a URL and stream the file to the
user's browser. I checked a number of examples from post in this news
group to get examples of how to do this (thanks for the good info).
The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
the current browser session), but always pops up the file download box
in IE 6. In IE the file is always downloaded and opened. This
happens even for html files. This is a problem, since the appLogic
opens most files in the browser, Word docs get opened in a separate
instance of MS Word. However, the user never sees the download box.

I tried modifiying the web.xml for this servelet to allow adding the
filename to to be appended to the servlet name,
GetContentItem/filename.jpg, but that didn't make any difference. The
content type and filename appear appear in the download dialog.

Here is the servelt code, any suggestions will be appreciated:

public class GetContentItem extends HttpServlet {
// initalise the servlet within init()
private static Log log = LogFactory.getLog(GetContentItem.class);

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
if (log.isDebugEnabled())
log.debug("IN " + GetContentItem.class + " doGet" );

String sCRBaseURLRoot =
"http://supporttest1.web.lucent.com:20101/cr/";
String sDocPath = "Live/Product/5ESS/R16/Retrofit_Information/090094058001c628.jpg";
String sDocPath2 =
"Live/Product/5ESS/R16/Release_Information/0900940580026c41/french.pdf";
String sDocPath3 =
"/Live/Product/5ESS/R16/Release_Information/0900940580026cc1/spanish.doc";
String sDocPath4 =
"/Live/Product/5ESS/R16/Release_Information/090094058002791c/61402_Release_Deployment.html";
String filename = "090094058001c628.jpg";
String filename2= "french.pdf";
String filename3= "spanish.doc";
String filename4 = "61402_Release_Deployment.html";
sDocPath=sDocPath4;
filename = filename4;
String sUrl = sCRBaseURLRoot + sDocPath;
java.io.InputStream in=null;
ServletOutputStream stream = null;

try{
URL u = new URL(sUrl);
URLConnection c = u.openConnection();
c.connect();

in = new DataInputStream(c.getInputStream());

String sHeaderField=null;
String sHeaderFieldKey=null;
int i = 1;
stream = response.getOutputStream();
response.setContentType(c.getContentType());
response.setContentLength(c.getContentLength());
response.setHeader("Content-disposition","attachment;filename=\"" +
filename + "\"");

int bytes_read;
int BUFSIZE = 8192;
byte[] buffer = new byte[BUFSIZE];
while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
stream.write(buffer,0,bytes_read);
}

} catch(FileNotFoundException e){
log.error("FileNotFoundException accessing document " + sDocPath +
e );

} catch(Exception e){
log.error("Exception accessing document " + sDocPath + e );

} finally{
try{
in.close();
stream.flush();
stream.close();
} catch (Exception e){}
}
}
}

I hope the code display format is not too ugly in the post.

Thanks,

Tom
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      05-04-2004
TomG wrote:

> Hi All,
>
> I am replacing a Netscape Application Server appLogic with a servlet.
> The servlet is supposed to take a URL and stream the file to the
> user's browser. I checked a number of examples from post in this news
> group to get examples of how to do this (thanks for the good info).
> The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
> the current browser session), but always pops up the file download box
> in IE 6. In IE the file is always downloaded and opened. This
> happens even for html files. This is a problem, since the appLogic
> opens most files in the browser, Word docs get opened in a separate
> instance of MS Word. However, the user never sees the download box.


I don't think it's causing too many problems for you at the moment, but
you may find it useful in the future to get your head around the roles
of the server and the user agent (browser). In particular, the server
doesn't work "in" NetScape, and the appLogic doesn't open anything in
the client's browser. The client requests information from the server
via his user agent; the server responds with a message or messages; the
client accepts and processes the messages via his user agent.

If you have this clear then it is immediately evident that differing
user agent behavior must be in response to differing messages received
from the server. A reasonable debugging step, then, would be to capture
and examine the HTTP responses from the appLogic to the client and from
the servlet to the client.

> I tried modifiying the web.xml for this servelet to allow adding the
> filename to to be appended to the servlet name,
> GetContentItem/filename.jpg, but that didn't make any difference. The


Not surprising.

> content type and filename appear appear in the download dialog.


[...]

> public void doGet(HttpServletRequest request, HttpServletResponse
> response)
> throws ServletException, IOException {


[...]

> stream = response.getOutputStream();
> response.setContentType(c.getContentType());
> response.setContentLength(c.getContentLength());
> response.setHeader("Content-disposition","attachment;filename=\"" +
> filename + "\"");


So what do you think you are accomplishing with that header? Is it
there for some specific reason? Have you considered that it's awfully
odd to declare content that you want displayed inline as an attachment
-- with a specified file name, even? I'd bet on that being a major
contributor to the problem. If you need a content disposition at all
then its value should probably be "inline".

>
> int bytes_read;
> int BUFSIZE = 8192;
> byte[] buffer = new byte[BUFSIZE];
> while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
> stream.write(buffer,0,bytes_read);
> }


[...]


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
TomG
Guest
Posts: n/a
 
      05-04-2004
"John C. Bollinger" <(E-Mail Removed)> wrote in message news:<c788gl$obe$(E-Mail Removed)>...
> TomG wrote:
>
> > Hi All,
> >
> > I am replacing a Netscape Application Server appLogic with a servlet.
> > The servlet is supposed to take a URL and stream the file to the
> > user's browser. I checked a number of examples from post in this news
> > group to get examples of how to do this (thanks for the good info).
> > The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
> > the current browser session), but always pops up the file download box
> > in IE 6. In IE the file is always downloaded and opened. This
> > happens even for html files. This is a problem, since the appLogic
> > opens most files in the browser, Word docs get opened in a separate
> > instance of MS Word. However, the user never sees the download box.

>
> I don't think it's causing too many problems for you at the moment, but
> you may find it useful in the future to get your head around the roles
> of the server and the user agent (browser). In particular, the server
> doesn't work "in" NetScape, and the appLogic doesn't open anything in
> the client's browser. The client requests information from the server
> via his user agent; the server responds with a message or messages; the
> client accepts and processes the messages via his user agent.
>
> If you have this clear then it is immediately evident that differing
> user agent behavior must be in response to differing messages received
> from the server. A reasonable debugging step, then, would be to capture
> and examine the HTTP responses from the appLogic to the client and from
> the servlet to the client.
>
> > I tried modifiying the web.xml for this servelet to allow adding the
> > filename to to be appended to the servlet name,
> > GetContentItem/filename.jpg, but that didn't make any difference. The

>
> Not surprising.
>
> > content type and filename appear appear in the download dialog.

>
> [...]
>
> > public void doGet(HttpServletRequest request, HttpServletResponse
> > response)
> > throws ServletException, IOException {

>
> [...]
>
> > stream = response.getOutputStream();
> > response.setContentType(c.getContentType());
> > response.setContentLength(c.getContentLength());
> > response.setHeader("Content-disposition","attachment;filename=\"" +
> > filename + "\"");

>
> So what do you think you are accomplishing with that header? Is it
> there for some specific reason? Have you considered that it's awfully
> odd to declare content that you want displayed inline as an attachment
> -- with a specified file name, even? I'd bet on that being a major
> contributor to the problem. If you need a content disposition at all
> then its value should probably be "inline".
>
> >
> > int bytes_read;
> > int BUFSIZE = 8192;
> > byte[] buffer = new byte[BUFSIZE];
> > while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
> > stream.write(buffer,0,bytes_read);
> > }

>
> [...]
>
>
> John Bollinger
> (E-Mail Removed)


John,

Thanks for the comments. I must admit that I am new to servlets. I
pasted the code together from some examples I found in different
posts. You nailed my problem; the content-dispostion. I changed the
setHeader to
("Content-disposition", ":inline;filename=\"" + filename + "\"") and
things started working in IE. I wasn't aware of the different content
dispositon types.

Thanks again,

Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subclass to manipulate ServletOutputStream casting error Edward Java 1 02-01-2006 12:18 AM
Using ServletOutputStream in Servlet Vs <img src="../images/abc.gif" /> ap Java 1 02-15-2005 12:51 PM
How to read from a ServletOutputstream? Timo Nentwig Java 6 08-22-2004 01:07 AM
ServletOutputStream & shtml files??? TomG Java 1 07-19-2004 07:32 AM
Redirect ServletOutputStream into a file? Mikito Harakiri Java 1 04-03-2004 01:06 AM



Advertisments