This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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
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);
}

}
}
shi yongfei
Greenhorn

Joined: Nov 13, 2002
Posts: 7
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

Joined: Nov 19, 2003
Posts: 50
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.
Sri Basavanahally
Ranch Hand

Joined: Oct 07, 2003
Posts: 75
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


UP THE IRONS !
Sri Basavanahally
Ranch Hand

Joined: Oct 07, 2003
Posts: 75
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

Joined: Nov 19, 2003
Posts: 50
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.
Trijoy Saikia
Greenhorn

Joined: Nov 20, 2003
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling servlet from JSP