| 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.
|
 |
 |
|
|
subject: Calling servlet from JSP
|
|
|