This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Calling servlet from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling servlet from JSP" Watch "Calling servlet from JSP" New topic
Author

Calling servlet from JSP

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.
I don't know if i am passing servlet in JSP correctly.
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);
}
}

}
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
why not do:
out.write( "<a href='docs/servlet/viewAttachment?file=" + filenames[i] + "'>" + filenames[i] + "</a>" );
.....
String fpath = request.getParameter("file");


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling servlet from JSP
 
Similar Threads
Implementing download - XLS files - JSP scriptlet
save windows?
Servlet failed with IOException
calling servlet from JSP
Zipping files