File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to call a servlet from a controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to call a servlet from a controller" Watch "How to call a servlet from a controller" New topic

How to call a servlet from a controller

Giridhar Gajula

Joined: Oct 27, 2005
Posts: 1

I am new to Java development, In my project I am creating a profile for the user when they log-in to the system.

I get the general information like name, e-mail and browse (Picture), when they save the profile the detils are saved in the oracle table. But the picture is saved in the server file.

I have written a Servlet to retive the picture from the file system, I have no idea where should I written the code in the controller to retive the picture.

Here the controller code

if ("viewProfile".equals(mapping.getParameter()))
// Get Single user profile details
String pofileshotname = new String(request.getParameter("shotname"));

BVDirectoryDAO bvDirectoryDAO = new BVDirectoryDAO();
Profile bvDirectoryUserInfo = bvDirectoryDAO.getBVDirectoryUserInfo(pofileshotname);
if (bvDirectoryUserInfo != null)
request.setAttribute(Constants.BVDIRECTORY_USERINFO, bvDirectoryUserInfo);
return (mapping.findForward("SystemError"));

ProfileDAO virtualAcademyDAO = new ProfileDAO();
Profile virtualAcademy = virtualAcademyDAO.getVAUserProfileInfo(pofileshotname);
request.setAttribute(Constants.CURRENT_VIRTUALACADEMY_USERPROFILE, virtualAcademy);
catch (DAOException daoe)
System.err.println("Error in retriving Virtual Academy List");
return (mapping.findForward("SystemError"));

// Done
return (mapping.findForward("success"));


Servelt code

public class RetrieveProfileImage extends HttpServlet

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

// Get the absolute path of the image

String pofileshotname = new String(request.getParameter("shotname"));
[ October 27, 2005: Message edited by: Giridhar Gajula ]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Your controller should not be doing any retrieving of data. It should use the services of the model/delegate to the model to retrieve the data. This will be just a regular Java class that the controller will call, and set the appropriate variable. After which it will be made available to the JSP for display via forwarding by the controller.

So much trouble in the world -- Bob Marley
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
It also looks like you are using Struts. I would recommend learning how to develop with simple Servlets and JSPs before using a framework such as Struts. Otherwise it is kind of like trying to race a Formula 1 car without your driver's license.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If it's saved to the filesystem, just add an image link to the view.

If the image is not stored within your webapp, you will need to stream the image. See SimpleStream at for an example of how to do so.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Barun Saha

Joined: Nov 10, 2003
Posts: 24
Never Mind. I called session.invalidate() during login & that fixed the problem.

I agree. Here's the link:
subject: How to call a servlet from a controller
It's not a secret anymore!