This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet failed with IOException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet failed with IOException" Watch "Servlet failed with IOException" New topic
Author

Servlet failed with IOException

Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
I have a JSP Page whcih display files, i want that when i click on the files, it should dispaly in its format, for example if it is word doc, it should open in word document.
This is my code in JSP Page, it is displaying the filenames
for(int i =0;i<filesdisplay;i++)
{
out.write( "<a href='docs/servlet/viewAttachment" + filenames[i] + "'>" + filenames[i] + "</a>" );
}
This is my viewAttachment servlet
package servlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class viewAttachment extends HttpServlet
{

public void doGet (HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
ResourceBundle rb = ResourceBundle.getBundle("Project");
String rootdir = rb.getString("projectDocRootDirectory");
String uri = request.getRequestURI();
int slash = uri.lastIndexOf("/docs/") + 6;
int dot = uri.lastIndexOf('.') + 1;
String fpath = uri.substring(slash);
String ftype = uri.substring(dot);
String fullpath = rootdir + fpath;
String ctype = "text/plain";

if ( ftype.equals("html") )
{
ctype = "text/html";
}
else if ( ftype.equals("xml") )
{
ctype = "text/xml";
}
else if ( ftype.equals("ppt") )
{
ctype = "application/ppt";
}
else if ( ftype.equals("xls") )
{
ctype = "application/xls";
}
else if ( ftype.equals("doc") )
{
ctype = "application/doc";
}
else if ( ftype.equals("pdf") )
{
ctype = "application/pdf";
}
response.setContentType( ctype );
FileInputStream fis = new FileInputStream(fullpath);
ServletOutputStream out = response.getOutputStream();
for( int c=fis.read(); c > -1; c=fis.read() )
{
out.write(c);
}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
}
When i try to click on the file to open it gives me this error:
Servlet failed with IOException
java.io.FileNotFoundException: //erddata01/Group/ITARCH/Project_Documentationservlet/viewAttachment
(The system cannot find the path specified)
Please help , i have to finish it as soon as i can.
Mark Latham
Greenhorn

Joined: May 27, 2003
Posts: 22
It appears it's not even finding your servlet. What URL is your JSP actually generating for the link? Is that where the servlet is? Are you able to directly browse to the Servlet (I know it'll give you an error, but you need to be sure your Servlet is deployed correctly). If it's not, read the Tomcat FAQ. Even if you're not using Tomcat it should get you going in the right direction.
If I've misunderstood the error you're getting, please correct me.
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
I have done the servlet declaration in my web.xml file

<servlet>
<servlet-name>viewAttachment</servlet-name>
<description>viewAttachment.</description>
<servlet-class>servlet.viewAttachment</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>viewAttachment</servlet-name>
<url-pattern>/docs/*</url-pattern>
</servlet-mapping>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
If this was my problem I would first create a plain HTML page with links to address the servlet and get that working right. Then all you have to do is make the JSP generate the same HTML.
Bill
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You've mapped /docs/* to your 'view' servlet.

So why do you need the servlet in the path?

how about a link like:

out.write( "<a href='docs/" + filenames[i] + "'>" + filenames[i] + "</a>" );

This will also make parsing of your filename much simpler. Just retrieve the PathInfo() and that's your filename.

Also, the path you've constructed has a few things funny-looking.

You're trying to find:
//erddata01/Group/ITARCH/Project_Documentationservlet/viewAttachment

1) // is off, unless you're using Windows UNC filepaths.
2) Isn't there a slash missing between 'Project_Documentation' and 'servlet'
3) What's "servlet/viewAttachment" doing as part of your filepath?
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
I have made some changes, my servelet code is same , i have changed in JSP page
My JSP looks like this
for(int i =0;i<filesdisplay;i++)
{
out.write( "<a href='/docs/projectname/eventtype" + "/" + filenames[i] + "'>" + filenames[i] + "</a>" );
}
My Web.xml file looks like this for this servlet
<servlet>
<servlet-name>viewAttachment</servlet-name>
<description>viewAttachment.</description>
<servlet-class>servlet.viewAttachment</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>viewAttachment</servlet-name>
<url-pattern>/docs/*</url-pattern>
</servlet-mapping>
Problem:
Now when i click on the file i don't get Filenotfound exception , , Server gives me error404 whch states
10.4.5 404 Not Found
The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.
My URL at this time of error is :
http://localhost/docs/projectname/eventtype/Book1.xls
it is not taking the value of projectname and eventtype. so i think now problem is calling from this JSP page.
plz plz help.
Mark Latham
Greenhorn

Joined: May 27, 2003
Posts: 22
I reviewed some of your other posts and it appears you may be programming by coincidence. Instead of trying different approaches until one works you should first understand how the HTTP request is mapped to the servlet. Otherwise, you'll eventually get it to work but won't really understand why it works (and the others didn't).
Ask yourself the question, "How is the application server supposed to map the request http://localhost/docs/projectname/eventtype/Book1.xls to the Java class servlet.viewAttachment?". The Tomcat FAQ I recommended in a previous post in this thread can help you find and understand the answer. And when you're working on Servlet mapping, you can just type the URL in your browser (you don't need to create a link on a page).
Next, recognize that the filename you're extracting from the URL is a variable for your Servlet. It is non-standard to extract a variable from a URL in this manner. Instead, try using HTTP Get to pass in the variable. For example, http://localhost/WebAppDir/docs/viewAttachment?file=Book1.xls. Then you can use the standard servlet methods for retrieving it's value.
Once you address these 2 areas, the rest of your application should fall into place.
[ December 10, 2003: Message edited by: Mark Latham ]
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
I understood the concept of servlet mapping, but now for displaying the files, I have a scenari, were i have projectname and eventtype, so when u select both of these, some information is displayed on the page and also all the files which this eventtype has also diesplays, so all the files are being dispalyed but when you try to open the fil it should open its contents in its own format, so i have to go through page, i can't go directly though URL, so after reading, i think there is problem in calling from my JSP page.
Any sugesstions will be appreciated.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

you said: so all the files are being dispalyed but when you try to open the fil it should open its contents in its own format, so i have to go through page, i can't go directly though URL

Who says??

If you browser doesn't understand how to open a file type, then it should ask you what application to use to open it. Normally, for many common file types, the browser will either recognize the type, or have a plugin registered to 'help' view that filetype (for example.. have you never clicked a link ending in .pdf and had the acrobat plugin load?)

Different browsers (read: Internet Explorer) can sometimes be a bit stupid about how they try to figure out what the file type is... but you can help yourself by understanding how to configure mime types in your web server. There are also some very specific IE hacks you *sometimes* must perform to get IE to download a file appropriately.

Also.. just turn off 'friendly error messages' in IE. I can tell it's on, because from your description of the 404.. it's being "friendly". You're a developer, so you'd rather just see the exception, thanks.
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
I understand some of your concept.
ok here is the problem-- In my JSP Page i am calling method which gets the filenames from the directory and those filenames get displayed on JSP Page, to open the filename i have servelt which open the files after reading its type. So that's why i go through page because files are dispayed on JSP Page. I know my servet code is fine but the problem i think i am having is how to call from JSP Page, in my ther replies, i wrote the code of my servlet, web.xml and that piece of JSP which will call that servlet. I have to open visio , Microsoft Project ext files.
can anyone tell me what i am doing wrong.
plz help, its very important for me to finish.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

This is where I want to focus attention:

... to open the filename i have servelt which open the files after reading its type. So that's why i go through page ...

I'm maybe missing something, but *this* part is what I'm trying to say is not needed. Once you have a list of files on the server, why do you have to retrieve it through a servlet? (Your answer seems to be: 'to know what type it is'). THIS is *not needed*.

You should just be able to produce directly browsable links in your page, so that when a user clicks on them, they get that file from the web server's file system (it is served just like an HTML page, with no other processing).

Web servers are commonly configured to handle various MIME types of files. The web server will then recognize the type, by extension, and tell the browser what type of file it is sending. It's a crap-shoot if your browser will listen.

One way to help out is to tell your users to 'right click' and 'save as', or a similar message.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet failed with IOException