Link to home
Start Free TrialLog in
Avatar of Alaska Cowboy
Alaska CowboyFlag for United States of America

asked on

URL to open a Word document works on IE-7 but not on FF-4 or Chrome 10.0

I have a JSP page that has this link:

file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc

This is on a corporate intranet and everyone has access to this directory.

The file opens as expected in IE-7, but it won't open in FF-4 or Chrome 10.0. On FF and Chrome, it looks like a link but nothing happens when I click on it.
Avatar of Alaska Cowboy
Alaska Cowboy
Flag of United States of America image

ASKER

When I enter the URL above in a browser (FF and Chrome), the link works, as expected.

But the link doesn't work when it's embedded in a JSP page running on Tomcat.

I have verified the links are the same (by copying them out), but it doesn't work when running from the JSP rendered page.

file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc - works from browser a test HTML page
file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc - doesn't work from JSP / Tomcat
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rrz, ok, thank you, I will give it a try.

rrz, it looks like you wrote what I need, but not sure how to wire everything.

I'm running Tomcat 5.5.

What you wrote looks like it might do the trick, but I have a few questions:

- where does this code go ?
- how is it called ? I would have to pass in "R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc"

Thanks.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I needed the web.xml info, thanks.

Still need to know where the code goes and how to pass the file name.

I will increase the points when I'm back on the computer, I don't think I can do it in mobile.
>Still need to know where the code goes
You have to compile the Servlet and put the class file into your web app's classes folder. For example
  WEB-INF/classes/rrz/DownloadServlet.class  
>how to pass the file name.  
For testing just use the JSP I posted.  
Where is the file name coming from ? Are you going to have a list of them for the user to choose from ?

Ok, thanks. The file name comes from Oracle.

I will get back to this Monday.
increasing points.
rrz, in your servlet code, is this line needed -->String filename = "test.doc";                

I'm assuming that's left over and now shouldn't be there.
where should I put the servlet ?

in my java components, I have
- dao
- domain
- web

I'm guessing web, but is there a standard place ?
well, it's set up but not working properly, I get this: "Error 404: SRVE0190E: File not found: /getDoc"

I removed the "test.doc" reference, and I double-verified the file actually exists and can be opened.

here's the link: http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

and this opens from FF
file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc
/********* Call getDoc *********/

<c:otherwise><a href="getDoc?filename=<c:out value="${hist.cdrReportLoc}"/>"><c:out value="${hist.procDescription}"/></a></c:otherwise>	

/******** Servlet, only thing I changed was removing "test.doc" ********/
package com.carefirst.edw.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         InputStream in = null;
         OutputStream out = null;
         String fileName = request.getParameter("fileName");
         if(fileName != null){
             try {
                  File file = new File(fileName);
                  // String filename = "test.doc"; 
                  FileInputStream fis = new FileInputStream(file);
                  response.setContentType("application/msword");
                  out = new BufferedOutputStream(response.getOutputStream());
                  in = new BufferedInputStream(fis);
                  response.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
                  int c; 
                  while( ( c=in.read() ) != -1 ) out.write( c );
             }
             catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND);
             }
             finally {
               if( in != null ) try { in.close(); } catch( Exception e ) {}
               if( out != null ) try { out.close(); } catch( Exception e ) {}
             }
         } else response.sendError( HttpServletResponse.SC_BAD_REQUEST); 
    }
}

Open in new window

>in your servlet code, is this line needed -->String filename = "test.doc";  
Yes, it is needed because that is the file name that  is used in "Content-Disposition". It will appear in the dialog box that asks the user to open MS Word.  You can name it anything you want to name it. You could also parse the file name from the url that came from the database and use that.
>where should I put the servlet ?
I don't know your development environment. Are you using a IDE ?  Anyway the Servlet needs to be compiled and put into its package folder and that should placed into your web app's classes folder.
WEB-INF/classes/com/carefirst/edw/web/DownloadServlet.class  
 Or if it is being put into a jar that should be placed into your web app's lib folder.
>well, it's set up but not working properly,
Please post how you mapped the Servlet in your web app's web.xml file ?
>I get this: "Error 404: SRVE0190E: File not found: /getDoc"  
This is telling us that the Servlet is not being found.  
For testing, begin with a Simple Servlet. Try using the Servlet below here. Try browsing to  
http://localhost:9080/edw/getDoc
or use  
<a href="getDoc"></a>  
in a JSP that is in your web app's root folder.
The init method was included for debugging. It can be removed later. But for now you should see the init message in your console when the Servlet is loaded.
package com.carefirst.edw.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/plain");
        response.getWriter().write("This is a test!);
    }
}

Open in new window

oh, I'm pretty sure I didn't map this in the web.xml file.

so everything is looking good so far, hopefully this will fix it. Unfortunately, I won't be able to get to this until later today or possibly not until tomorrow.
You never told me which web server you are using. If you using Servlet 3.0 API then you could use annotations instead of mapping.  If you using anything less than Servlet 3.0  then you will have to use mapping.
I'm using Tomcat 5.5.

How would I know if I'm using Servlet 3.9 API or not ?

also, another question, what does this mean: "@Override" - - - I've never seen anything like that.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok on mapping and determining what I am using.

also ok on override, so I'm learning a variety of things here, thanks.

Still tied up though, won't be able to try and fix this until later today or tomorrow morning.
Ok, I got the web.xml updated, here:
   <servlet>
        <servlet-name>fileServlet</servlet-name>
        <servlet-class>com.carefirst.edw.web.DownloadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>fileServlet</servlet-name>
        <url-pattern>/getDoc</url-pattern>
    </servlet-mapping>

but now I get the non-informative message: "Error 400: Error reported: 400"
well, I modified the classpath to this:

<servlet-class>WEB-INF.classes.com.carefirst.edw.web.DownloadServlet</servlet-class>

and got this error message:

Error 404: SRVE0203E: Servlet [fileServlet]: WEB-INF.classes.com.carefirst.edw.web.DownloadServlet was found, but is missing another required class. SRVE0206E: This error typically implies that the servlet was originally compiled with classes which cannot be located by the server. SRVE0187E: Check your classpath to ensure that all classes required by the servlet are present.SRVE0210I: This problem can be debugged by recompiling the servlet using only the classes in the application's runtime classpath SRVE0234I: Application classpath=
>"Error 400: Error reported: 400"
That is telling us that the request was bad (syntactically incorrect).
What did you request ?  
Are you using the simple test Servlet  in my last post ?  
Did you browse to  
http://localhost:9080/edw/getDoc 
?
Is edw the name of your web app ?
><servlet-class>WEB-INF.classes.com.carefirst.edw.web.DownloadServlet</servlet-class>
That is wrong. You had it right before. Use
<servlet-class>com.carefirst.edw.web.DownloadServlet</servlet-class>  
The classes folder is in Tomcat's  classpath.  
Is the file  DownloadServlet.class  in  
<yourTomcatHome>/webapps/edw/WEB-INF/classes.com/carefirst/edw/web  
?  
Is the first line of your Servlet
package com.carefirst.edw.web;
?
sorry, here's the request:
http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

the application edw works great, just trying to get this servlet to work.

>Are you using the simple test Servlet  in my last post ?  
- yes, see below, pretty much as is

>Use <servlet-class>com.carefirst.edw.web.DownloadServlet</servlet-class>  
 -ok

>Is the file  DownloadServlet.class  in  <yourTomcatHome>/webapps/edw/WEB-INF/classes.com/carefirst/edw/web
- yes

now getting this again: "Error 400: Error reported: 400 "

We can do this tomorrow if that's better.
package com.carefirst.edw.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         InputStream in = null;
         OutputStream out = null;
         String fileName = request.getParameter("fileName");
         if(fileName != null){
             try {
                  File file = new File(fileName);
                  String filename = "cdr_report.doc"; 
                  FileInputStream fis = new FileInputStream(file);
                  response.setContentType("application/msword");
                  out = new BufferedOutputStream(response.getOutputStream());
                  in = new BufferedInputStream(fis);
                  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                  int c; 
                  while( ( c=in.read() ) != -1 ) out.write( c );
             }
             catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND);
             }
             finally {
               if( in != null ) try { in.close(); } catch( Exception e ) {}
               if( out != null ) try { out.close(); } catch( Exception e ) {}
             }
         } else response.sendError( HttpServletResponse.SC_BAD_REQUEST); 
    }
}

Open in new window

Ok.  Tomorrow(morning only) is good for me.
I see the problem.  In the code we have

if(fileName != null){
...
} else response.sendError( HttpServletResponse.SC_BAD_REQUEST);

fileName was null  therefore the SC_BAD_REQUEST was sent .
You used  
  http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 
But the Servlet is looking for the parameter "fileName"  
Try using
http://localhost:9080/edw/getDoc?fileName=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 
Java is case sensitive.  
I don't get what you are saying . . . fileName should be populated from "request.getParameter("fileName");"

Here's how I call the servlet in the jsp:
<c:otherwise><a href="getDoc?filename=<c:out value="${hist.cdrReportLoc}"/>"><c:out value="${hist.procDescription}"/></a></c:otherwise>                                    

and it gets formed properly as
http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 


got it ! woo-hoo !

I had to get the parameter filename in the model, then I had to fiddle with the case and finally got it, yea . . .
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should have named the form parameter something else so it would not have been confusing.  
One more thing that might be a problem later.
You should really encode the parameter's value when inserting into the query string.  The value you posted worked without doing so but you might change it later and it might break the code.  
<a href="<c:url value="getDoc"> 
             <c:param name="fileName" value="${hist.cdrReportLoc}" />
       </c:url>">${hist.procDescription}</a> 

Open in new window

This way the <c:url> and <c:param> tags will encode any special characters. As you can see I didn't use <c:out> tag around your EL values. That should not be necessary.
rrz, ok, thanks again, the users will love it :-)
shucks, I got this error when I moved it out of my local development . . .

HTTP Status 404 -

type Status report

message

description The requested resource () is not available.
Apache Tomcat/5.5.31


The URL should work . . .
http://xpc-120268/edwdev/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

same as local development
http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc
>description The requested resource () is not available.  
This message tells us that the file could not be found. In the code  
catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND);
             }

Open in new window

 I guess we could improve this by using  
catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND,"The file " + fileName + " could not be found.");
             }

Open in new window

Or if you changed the variable to filename, then use
catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND,"The file " + filename + " could not be found.");
             }

Open in new window

This will send the message along with error.
ok, but it's the same data (development), just on a different server.  My local development environment and then the regular public dev environment both point to the same database . . .

I'll put the error message in as you describe.

I can open another question for this . . .  
>I can open another question for this . . .  
That is not necessary for me. But I will be going offline soon. So if you want someone else to help later today then go ahead.
>ok, but it's the same data (development), just on a different server.
There must some difference. Recheck the file location.
Add the debug code
System.out.println("file path is " + fileName);
right after the line that gets the parameter from the request.
ok, I will debug some more but will open another question if I can't fix it. thanks a lot.
this is the error message on local development when the file name is bad:
Error 404: The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_XXX_17_02.doc could not be found.

it shows up just as written above on a white screen inside a "thick-box" (I forced it to be bad with "XXX")

on the Tomcat server, I get this, also inside the thick-box
HTTP Status 404 - The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc could not be found.

type Status report

message The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc could not be found.

description The requested resource (The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc could not be found.) is not available.
Apache Tomcat/5.5.31


something's obviously different, I'll open another question
I'm on my out, but I did see your other question.  Your url
http://xpc-120268/edwdev/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 
still has
filename
when the Servlet is looking for
fileName  
Also you should add the <c:url> and <c:param> tags that I posted.
but why would it work on WebSphere and not Tomcat ? I thought I had resolved the fileNname thing since it worked in development.

Ok, I'll add the <c:url> and <c:parm> and move to the new question, thanks again.
first servlet, aw . . .