Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object Serialization between an applet and a servlet

Posted on 2006-06-21
32
Medium Priority
?
552 Views
Last Modified: 2013-11-24
Greetings,

I am running Apahce Tomcat v5.5.

I am writing a Signature panel for use on a tablet PC. The signature is recorded by an applet(client side) and is stored in a BufferedImage class. I need to serialize this class and send it to a servlet so it can be recorded in a database via JDBC or saved to file as a jpeg image(if possible).

Any suggestions?
0
Comment
Question by:jtgraphic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 9
  • 4
  • +3
32 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16954479
Just write a POST request to the servlet using a URLConnection. ImageIO.write is what you need, as i mentioned in your previous

You can use a simpler version of this:

http://javaalmanac.com/egs/java.net/Post.html
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16954607
here you can find a helper class that simplifies http posting objects for you
http://homel.vsb.cz/~dvo25/reily/books/javaenterprise/servlet/ch10_02.htm#ch10-16733
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16954677
Correction to my earlier comment (in case it confuses you) - the example uses a Writer, which you shouldn't. All you should need in essence is:

// Send data
        URL url = new URL("xxxxxxxxxxxxxxxxxx");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStream out = conn.getOutputStream();
        ImageIO.write(image, "JPG", out);
        // Make sure 'out' is closed
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jtgraphic
ID: 16954759
How would you suggest I capture the object on the server side?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16954775
ObjectInputStream in = new ObjectInputStream(request.getInputStream())
0
 

Author Comment

by:jtgraphic
ID: 16954779
I think that's the part that's causing me the most frustration.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16954787
You just read the servlet's input stream. You can then do what you want with it
0
 

Author Comment

by:jtgraphic
ID: 16954907
I do that in the doPost function?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16954916
Yes
0
 

Author Comment

by:jtgraphic
ID: 16954993
For testing purposes, is there a simple way of writing the captured input to disk to verify that the code is working properly? All of my attempts at writing to disk, other than JDBC, have caused Tomcat to have a fit.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16955017
>>All of my attempts at writing to disk

Can you show how you were doing that? Writing to a file within the web root will be easier
0
 
LVL 92

Expert Comment

by:objects
ID: 16956144
in your servlet:

InputStream in = request.getInputStream();
FileOutputStream out = new FileOutputStream(filename);
byte[] buf = new byte[256];
int nread = 0;
while (-1!=(nread=in.read(buf))) {
   out.write(buf, 0, nread);
}
out.close();
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16956911
>> of my attempts at writing to disk,

The user-account under which the web-app is running might need permissions to write to the directory where you were trying to write.
0
 

Author Comment

by:jtgraphic
ID: 16959204
I'm not sure if the data isn't being received or if the code is incorrect. The end result is that test.jpg is not being created.

This is the ActionPerformed method in the applet from which I attempt to transmit the BufferedImage to the Servlet,

public void actionPerformed( ActionEvent e )
{
      if( e.getSource() == submitButton )
      {
            // Serialize BufferedImage and send to Servlet
            System.out.println("sending data...");
            
            try
            {
                  URL url = new URL( "http://localhost:8080/servlet/SigServ" );
                  URLConnection conn = url.openConnection();
                  conn.setDoOutput( true );
                  OutputStream out = conn.getOutputStream();
                  ImageIO.write( canvas.getImage(), "jpg", out );
                  out.close();
            }
            catch( Exception ex )
            {
                  ex.printStackTrace();
            }
      }
      else if( e.getSource() == clearButton )
      {
            Graphics2D gc = canvas.getImage().createGraphics();
            gc.setColor( Color.white );
            gc.fillRect( 0, 0, canvas.getWidth(), canvas.getHeight() );
            this.repaint();
      }
}

And this is my doPost method from the servlet,

public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
{
      File filename = new File( "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\test.jpg" );
      InputStream in = request.getInputStream();
      FileOutputStream out = new FileOutputStream( filename );

      byte[] buf = new byte[256];
      int nread = 0;

      while ( -1 != ( nread = in.read( buf ) ) )
      {
            out.write(buf, 0, nread);
      }

      out.close();
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16959273
What is the output? Try doing an out.flush () after out.write () though I don't really think its going to help....
0
 
LVL 92

Expert Comment

by:objects
ID: 16959275
add some logging to your servlet to get an idea whats happening.

btw, you probably want to send the image on a seperate thread as your gui will be blocked while it is being sent.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16959279
>> File filename = new File( "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\test.jpg" );

Why not try it like:

File fileName = new File ( "/test.jpg" ) ;
0
 

Author Comment

by:jtgraphic
ID: 16959358
>> add some logging to your servlet to get an idea whats happening.

Pardon my ignorance but... How?

>> btw, you probably want to send the image on a seperate thread as your gui will be blocked while it is being sent.

Yeah, that would be nice. However, I'd like to get something working before I start complicating matters.
0
 
LVL 92

Expert Comment

by:objects
ID: 16959393
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
{
     System.out.println("doPost called");
     File filename = new File( "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\test.jpg" );
     InputStream in = request.getInputStream();
     FileOutputStream out = new FileOutputStream( filename );

     byte[] buf = new byte[256];
     int nread = 0;

     while ( -1 != ( nread = in.read( buf ) ) )
     {
         System.out.println("bytes read "+nread);
          out.write(buf, 0, nread);
     }

     out.close();
}


and check the results in your containers log
0
 

Author Comment

by:jtgraphic
ID: 16959539
There are 17 different log files in, "C:\Program Files\Apache Software Foundation\Tomcat 5.5\logs" but none of them contain the logging.
0
 

Author Comment

by:jtgraphic
ID: 16959542
Is there another location log files are stored?
0
 

Author Comment

by:jtgraphic
ID: 16959719
I think the problem is that doPost is not being invoked. How can I get around this?
0
 
LVL 28

Expert Comment

by:rrz
ID: 16959761
Did you you check the console ?
Also, did you see
>         System.out.println("sending data...");
from the applet ?
0
 

Author Comment

by:jtgraphic
ID: 16959841
Yes, that prints tot he console perfectly.

I found the correct logfile by writing to stdout in doGet. Since writing to stdout from the start of  doPost does not write anything to the file, I must assume that doPost is not being called.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16960585
>>I found the correct logfile by writing to stdout in doGet.

Actually the code in question shouldn't be causing doGet to be called
0
 

Author Comment

by:jtgraphic
ID: 16960801
I know. I did it deliberately to find the correct log file. From looking at some PAQ's I think doPost is the wrong function to place code in anyway. Using,

public void service( HttpServletRequest request, HttpServletResponse response )

I have been able to get the applet and the servlet to talk to each other, at last. I'm able to write to disk as well. I'm having one last problem though. That is that when I use ImageIO.write to write the BufferedImage to the ObjectOutputStream I am not quite sure how to accurately reconstruct it on the server side. I tried using ObjectOutputStream.writeObject but tomcat wouldn't even write to stdout. I'm assuming that BufferedImage doesn't implement Serializable but that's just a guess.

Using object's file IO code, posted above, I get a file of the correct size, in the correct location, with the correct name and file type but the file format is messed up. None of my graphics programs recognize it. Here's the code,

-- In the applet's actionPerformed method --

try
{
      URL url=new URL( "http://localhost:8080/servlet/SigServ" );
      URLConnection con= url.openConnection();

      con.setDoInput ( true );
      con.setDoOutput( true );

      OutputStream out=con.getOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream( out );

      ImageIO.write( canvas.getImage(), "jpg", oos );
      //oos.writeObject( canvas.getImage() );

      InputStream in = con.getInputStream();

      oos.close();
}
catch( Exception ex )
{
      ex.printStackTrace();
}

-- In the servlet's service method --

try
{
      System.out.println("service called");
      File filename = new File( "C:\\test.jpg" );
      InputStream in = request.getInputStream();
      FileOutputStream out = new FileOutputStream( filename );

      PrintWriter outy = response.getWriter();
      outy.println("this is not a test");

      byte[] buf = new byte[256];
      int nread = 0, total_read = 0;

      while ( -1 != ( nread = in.read( buf ) ) )
      {
            total_read += nread;
            out.write( buf, 0, nread );
      }
      System.out.println( "bytes read " + total_read );
      out.close();
}
catch( Exception e )
{
      System.out.println( "try block failed in service method" );
      e.printStackTrace();
}

Any suggestions?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16960889
>>I'm assuming that BufferedImage doesn't implement Serializable but that's just a guess.

You're correct. Use the code i mentioned. doPost should work, but service should work too
0
 

Author Comment

by:jtgraphic
ID: 16961020
Well that may be true in theory but I tried a dozen different ways and none of the code above called doPost. Perhaps it's a Tomcat 5.5 issue?

At any rate, regardless of which method is used, if BufferedImage isn't serializable, I'm left with the same problem I outlined in my previous post.

Perhaps there's a way to construct a BufferedImage class server side with the data sent by the applet? Any thoughts?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16961321
>>Perhaps there's a way to construct a BufferedImage class server side with the data sent by the applet? Any thoughts?

Use ImageIO.read
0
 

Author Comment

by:jtgraphic
ID: 16962538
As I'm sure you've noticed, I don't work with Java very often.

Thanks for the help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16962558
:-)

Your method, whether doPost, or service can be the following btw

public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
{
    image = ImageIO.read(request.getInputStream());
}

How easy is that? ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 16964378
you were helped by more than one person on this question, why did u not split the points?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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