• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling servlet from JSP

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i have a JSP which display filenames from a folder but i wrote a servlet which open the contents of file, how do i call that servelt from JSP, the name of the servelt is viewAttachment.
JSP Code is
<%
int filesdisplay = filenames.length;
for(int i =0;i<filesdisplay;i++)
{
out.write( "<a href='docs/" "i want to call viewAttachment servlet here" );
}
%>

My servlet code is:
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";
}
else if ( ftype.equals("vsd") )
{
ctype = "application/vsd";
}
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);
}

}
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if u using tomcat as your webserver,you must set the web.xml,eg
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>justnorth.ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>9</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Database Initialization Servlet (must load after ActionServlet) -->
<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

after you finished it ,
you can call the servelt like this
http://localhost:8080/yourappname/filedown.do?filename=hell.doc
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to call my servlet in that JSP File, the name of the servlet is viewAttachment.
for(int i =0;i<filesdisplay;i++)
{
out.write( "<a href='docs/" + filenames[i] + "'>" + filenames[i] + "</a>" );

}

%>
This JSP diplays the name of all of all the files in the folder but i want when i click on it, it should call that servlet and display the contents in that particular file, if it is Excel file, it should open Excle or Word document , then it should open Word, i wrote the servlet but i don't know how to call that servlet in the above code. the name of servelt is viewAttachment.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
Have you looked at <jsp:include/> and <jsp:forward/> ? These may nbe your answer. Please look at the JSP Specs for usage and purpose of these JSP tags.
Hope that helps !
Regards,
Sri
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, on second thought, you should probably just call the servlet in the A HREF tag. Usually a servlet is called using its URL in the web-server.
IP address/servlet context name/servlet/servlet-name.
-Sri
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sri
I was also thinking to invoke in href tag, i know to invoke servlet from Form or through URL but i don't know how should i do it from AHREF tag and my servlet name is viewAttachment.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On another thought have a form with action as the servlet and a hidden variable (form element type='hidden' ). On click of your hyperlinked file names, call a clent side function passing the file name as a parameter. Set the parameter to your hidden field and submit the form.
In servlet do a request.getParameter on the hidden field which will give you the file name. Based on the extention set your mime as you have done and BINGO... your pdf and excels will be there at the browser.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic