ShowImageServlet.java Sample

This topic inludes the source code for the ShowImageServlet.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebApp/handlingData/binaryFlow/servlets/

Sample Source Code


01 package handlingData.binaryFlow.servlets; 
02 
03 import java.io.IOException;
04 import java.io.OutputStream;
05 import javax.servlet.ServletException;
06 import javax.servlet.http.HttpServlet;
07 import javax.servlet.http.HttpServletRequest;
08 import javax.servlet.http.HttpServletResponse;
09 
10 /*
11  * This servlet reads image data (in the form of a byte[]) from the request object.
12  * It outputs the byte[] as a visible image.
13  */
14 public class ShowImageServlet extends HttpServlet
15 
16     protected void service(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException
17     {
18         byte[] rgb = (byte[]) request.getAttribute("byArr");
19         if (rgb != null)
20         {
21             response.setContentType("image/gif");
22             OutputStream stream = response.getOutputStream();
23             stream.write(rgb);
24         }
25         else
26         {
27             response.setContentType("text");
28             response.getWriter().write("attribute byArr not found");
29         }
30     }
31