?
Solved

Servlet works on WebSphere but not on Tomcat

Posted on 2011-05-04
11
Medium Priority
?
724 Views
Last Modified: 2012-05-11
I just worked through my first servlet on another EE post, and it was looking good on my local development machine (WebSphere server). So then I was saddened to see that it didn't work on Tomcat 5.5.

I am opening a Word document and it now works fine in WebSphere (local machine), with this URL:
http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

but when moving to Tomcat (to another machine that the organization can use), this doesn't work:
http://xpc-120268/edwdev/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

both environments are pointing to the same database, so it seems like it's a Tomcat thing.

I am sure the file is there as it opens from WebSphere.

Below is my servlet and web.xml.

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 more detailed message, 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



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,"The file " + fileName + " could not be 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); 
    }
}

/********* Web xml entry **********/
   <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>


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

Open in new window

0
Comment
Question by:Alaska Cowboy
  • 6
  • 3
  • 2
11 Comments
 
LVL 28

Assisted Solution

by:rrz
rrz earned 200 total points
ID: 35692101
You have filename in your url. But your Servlet is looking for fileName.  
Also add the <c:url> and <c:param> tags to your code. They will encode special characters.  
0
 
LVL 36

Accepted Solution

by:
Gary Patterson earned 800 total points
ID: 35692124
My guess is that the "R":" drive is a network drive that isn't mapped in the process that Tomcat is running in.  

http://blog.mattwoodward.com/accessing-a-network-drive-from-apache-and-tom

- Gary Patterson
0
 
LVL 28

Expert Comment

by:rrz
ID: 35692142
Does Tomcat have access to
R:/FTP/Highmark/data/test  
?  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 35692181
Gary, ok, probably so, reading the article now.

rrz - not sure if Tomcat has access to R:...
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 35692355
Gary, reading the article now, having to do a deep dive into this so I'm taking a big air-tank.

the JCIFS referenced in one comment looks good but it's pretty much over my head. I'll try and slog through the original article.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 35692619
Gary, I read the article, it says "You need to be running Apache and Tomcat under an account that has network access. "

So I went to the Tomcat service, properties, Logon tab, then entered corporate_server_name\bpeck, my password, and that all looked good.

But I'm having trouble accessing R:\ drive as myself from the web server, not sure why, the network account is good. I'll re-boot and see what's going on.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 35692728
Ok, as bpeck I have access to the R:\ drive from the webserver.

I set myself up as the logon user to Apache/Tomcat (stopped and re-started the service), but I still get the error "file cannot be found". I also tried to keep the local system account but checked "allow service to interact with desktop", but no dice.

The rest of the article seemed unnecessary for me, I was thinking the account think looked good, but didn't work.
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 35692928
got it ! I had to use the full UNC path . . .
0
 
LVL 36

Assisted Solution

by:Gary Patterson
Gary Patterson earned 800 total points
ID: 35693005
Use the UNC path to the file, not a mapped drive.  A mapped drive can be different from computer to computer and user to user.  It really isn't a good way to refer to network resources.  When you log into Windows, your drive mappings are either mapped as part of a sign-on program that is associate with your profile, or configured in group policy, or some other similar mechanism, or drive mappings that were created on this machine under your account as PERSISTENT are reloaded fromthe registry and mapped automatically when you log on interactively.

None of this happens when Tomcat is started as a service.  

You'll need, instead, to use an UNC name (//servername/sharename/path/file.ext to refer to your file.

Open a Command Prompt and type NET USE.  You'll see a list of all your drive mappings, including that "R:" drive.  Replace Drive:/path/file reference with the UNC reference, and you should be in business.  (Just like shown in the article).

You can test from the Windows START-RUN box to to verify that your UNC path is correct.  Just enter the entire URL \\server\share\path\file.doc and if you have it right Word will load with the proper document.

- Gary Patterson

0
 
LVL 36

Expert Comment

by:Gary Patterson
ID: 35693009
:-)

- Gary
0
 
LVL 1

Author Closing Comment

by:Alaska Cowboy
ID: 35693222
Excellent !
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question