Nidhi Chaudhary
Nidhi Chaudhary

Reputation: 1

display image on jsp outside from webapps

I am working on a project, on the project home page there will be the logo of all the application i have added, all the logo file is stored on the folder "c:/apps/myap".

I am using Struts2 and hibernate, if I use a processor who process image download then my project will be very slow, I don't want to use any program to just simply display a logo image on <img src='..' />.

If i use file.xml under catalina/localhost

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/logo" docBase="c:/apps/myap/" /> 

nothing happens I am not able to see my image.

I have also added these context to server.xml that also not working.

Any one have any idea how to do it in simple way because i have searched and done a lot and found nothing. Please give me a simple way.

thanks.

Upvotes: 0

Views: 1353

Answers (1)

NickJ
NickJ

Reputation: 9559

Have the location of your images set as, e.g. an Init Param in web.xml:

<init-param>
    <param-name>imageLocation</param-name>
    <param-value>c:/apps/myap/</param-value>
</init-param>

The access them directly from your Servlet.

EDIT: The Images can then be served to the client from the servlet using ImageIO, assuming support for JPEG only. You'll need to adjust for other image types. It's not tested but should get you started:

public void doGet(HttpServletRequest request, HttpServletResponse response) {

  //What image is being requested?
  String imageName = request.getPathInfo();

  // Open image File
  File imageDirectory = new File(getServletContext().getInitParameter(“imageLocation”)); 
  File imageFile = new File(imageDirectory, imageName);

  //Read image
  BufferedImage image = ImageIO.read(new FileInputStream(imageFile));

  //Send image in response
  ImageIO.write(image, "jpeg", response.getOutputStream());
}

Upvotes: 1

Related Questions