Solved

JSP's Applet has to pass an Buffered Image to Servlet to upload on web server

Posted on 2009-07-07
19
1,710 Views
Last Modified: 2012-05-07
Hi,

I have a JSP that invokes an applet. This applet creates an Buffered Image, I need to pass this buffered image to a servlet that is hosted on the same web server as the JSP, this servlet is supposed to create a file from the BufferedImage and then upload to a location on the same web server. I am facing a problem in the communication between the Applet and Servlet.  The servlet is not even getting called..!!

Could you please tell me what I am missing, following is the code snippets that I am using



JSP:
 

<body>

<applet code="Applet.class" archive="Applet.jar"width="300" height="300"/>

</body>
 

Applet:
 

 public void sendBufferedImage(String urlpath, BufferedImage image) {

        try {

            System.out.println("Sending the image data");

            URL url = new URL(urlpath);

            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);

            OutputStream out = conn.getOutputStream();

            ImageIO.write(image, "jpg", out);

            out.close();

            System.out.println("Sent the data to " + url);

        } catch (IOException ex) {

           ex.getMessage();

        }
 

    }
 

Servlet:
 

 System.out.println("getting the image from applet");

            File filename = new File(getServletContext().getContextPath() + "test.jpg");

            FileOutputStream outy = new FileOutputStream(filename);

            InputStream in = request.getInputStream();

            byte[] buf = new byte[256];

            int nread = 0, total_read = 0;
 

            while (-1 != (nread = in.read(buf))) {

                total_read += nread;

                outy.write(buf, 0, nread);

            }

            System.out.println("bytes read " + total_read);

            out.close();

Open in new window

0
Comment
Question by:esko_user
  • 9
  • 9
19 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 24792752
1)  What is your "urlpath" set to?
2) Does the servlet get called if you load the servlet URL into the browser (it will then crash, but you should see it getting called)
0
 
LVL 92

Expert Comment

by:objects
ID: 24792759
any errors in the java console of the applet?
what url are you using to load the page, and to post the image?
0
 

Author Comment

by:esko_user
ID: 24792818
"http://localhost:6080/WebApplet/DocumentUpload" is the URL that hosts the web application which contains the JSP and the servlet, the applet is a separate jar file that I am using in the JSP
0
 
LVL 35

Expert Comment

by:TimYates
ID: 24792848
Yeah...  But is that the URL that the applet is using when you call sendBufferedImage?

Can you print the URL out in that applet method and see what it is in the Java Console?

Also, does the servlet get fired when you load that URL in your browser?
0
 

Author Comment

by:esko_user
ID: 24792915
Yes, this is the URL I am using, and when I am using the same path in the browser, the servlet does get fired.. but does nothing
0
 
LVL 35

Expert Comment

by:TimYates
ID: 24792940
Any output in the Java Console window?
0
 

Author Comment

by:esko_user
ID: 24792957
Java Console that I launch with the applet shows the following message among others ... No Error Messages though

Sending the image data
network: Connecting http://localhost:6080/WebApplet/DocumentUpload with proxy=DIRECT
Sent the data to http://localhost:6080/WebApplet/DocumentUpload
0
 
LVL 35

Expert Comment

by:TimYates
ID: 24792983
And the servlet code is in a doGet block yeah?

And you are running the browser on the same machine as the webapp (tomcat?) is running?
0
 

Author Comment

by:esko_user
ID: 24792994
The Servlet code is in doPost block

and the Yes the browser is run on the same machine as the  webapp(tomcat 6.1)

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:TimYates
ID: 24792996
Can you move the servlet code to the get block?

I believe the applet is doing a GET not a post
0
 

Author Comment

by:esko_user
ID: 24793015
Yes I tried that too, but even this is not helping
0
 
LVL 35

Expert Comment

by:TimYates
ID: 24793052
Can you try this in the applet (and set your Sevlet to doPost):


URL url = new URL( urlpath );

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestMethod("POST");

conn.setUseCaches(false);

conn.setDefaultUseCaches(false);

conn.setDoOutput(true);

conn.setRequestProperty("Content-Type", "application/octet-stream");

urlConnection.connect();

OutputStream out = urlConnection.getOutputStream();

ImageIO.write( image, "jpg", out ) ;

out.flush();

out.close();

Open in new window

0
 

Author Comment

by:esko_user
ID: 24793092
what is the type "urlConnection" ?
0
 

Author Comment

by:esko_user
ID: 24793113
Sorry ... its not working
0
 

Author Comment

by:esko_user
ID: 24793162
Should I be sending this data back the to the JSP that invokes the applet and the use JSP to post it to the Servlet?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 24793412
What data?

I assume you have a jsp with an applet tag in it, and that applet is supposed to send an image?

Hmmm...

Try reading the inputStream from the URL connection in the applet...that may give you some clues as to the problem?
0
 

Author Comment

by:esko_user
ID: 24793478
Yes the Applet is supposed to send an image... and is within an applet tag in the jsp

The input to the URLConnection is BufferedImage  so, I am supposed to check the data before being written to the URLConnection and then after being written to the connection ..!!  if this does not make sense then I did not get your suggestion
0
 
LVL 35

Expert Comment

by:TimYates
ID: 24793635
URLCOnnection is bi-directional, you cas specify to use both input and output, and read from an inputstream after you have sent the image to the servlet (to see if the servlet responded with anything)

And you post your current applet sendBufferedImage method?

I can't see why this isn't working if everything is as you say it is

ACTUALLY:

Maybe if instead of doing nothing with the exception in:

        } catch (IOException ex) {
           ex.getMessage();
        }

you could print the stack trace?

        } catch (IOException ex) {
           ex.printStackTrace()
        }

Then you will see the error you are getting
0
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 24813574
Did it give you the error?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is stuck threads? 3 97
form submissions on a web page 2 143
constuctor calling other constructor 2 74
own marker interface 1 50
What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now