Passing Images via servlet
Hi,
I need to pass a image from one servlet to another servlet.
I have tried the RequestDispatcher class, inside that forword method. But it didn't invoke the other servlet.
Can anybody tell me how to pass the data from one servlet to another servlet?
Thanks in advance.
[302 byte] By [
vinay07a] at [2007-11-27 6:19:41]

# 2
> I have tried the RequestDispatcher class, inside that forword method. But it didn't invoke the other servlet.
Well then you did it wrong, didn't you? Perhaps you forgot to let the resource start with a "/" ? If your servlet is mapped like this:
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
then your forward needs to be :
RequestDispatcher rd = request.getRequestDispatcher("/MyServlet");
rd.forward(request,response);
return;
Before you do the forward you can attach objects to the request like this:
request.setAttribute("myobject", myobject);
And in the servlet you forward to, you can retrieve the object again like this:
MyObject myobject = (MyObject) request.getAttribute("myobject");
# 3
Hi,
Thanks for ur reply.
Using that setAttribute() and getAttribute(), I have passed the image successfully between two servlets.
Actually I am passing the image stream from a J2ME application. In that how can I set the servlet attributes?
Is it possible to receive the image, which is send via the outputStream in servlets?
Thanks in advance.
# 4
> Actually I am passing the image stream from a J2ME application. In that how can I set the servlet attributes?
> Is it possible to receive the image, which is send via the outputStream in servlets?
I'm a little bit confused here. Are you sending the image TO the J2ME app, or are you uploading it FROM the J2ME app?
If you send it TO the J2ME app, then you use the outputstream to write the binary data (so open an outputstream, not a writer), and you need to set some response attributes, such as the content-length and the content-type. The HttpServletResponse has methods for this.