Sending objects works fine, you just need to specify the content type as
something *other than* application/x-www-form-urlencoded (application/x-binary
is a good choice, as I remember). You can serialize directly to the
URLConnection output stream, or if you're sending multiple objects may want to
serialize first to a memory buffer (via DataOutputStream using
ByteArrayOutputStream) and then send the entire resulting buffer in order to
avoid multiple blocks at the TCP/IP level.
URLEncode is fine if you really want to access values as named parameters for
the servlet.
- Dennis
Andy Bailey wrote:
>
> > Hi to all,
> >
> > I have a html file that there is an applet on it. I want when a user
> > click on a button on applet , then send object to Serlvet and after
> > that the Servlet write a html toward client.
> >
> > I wrote a sample program that sends object form applet to servlet and
> > vise versa but when I want to send object ot servlet and get html ,
> it
> > diesn't work. How can I solve my problem.
> >
> >
> > Any suggestion.
> >
> > Thanks a lot
> >
> > Mina
> >
> Simply put, you cannot send serialized Java objects to a servlet via an HTTP
> connection as
> a parameter.
>
> Because of the way the HTTP protocol expects parameter names and values to
> be encoded
> (Check out the docs for URLEncoder to see what I mean) and the strict
> conformance to this
> format imposed on you it is only practical to send parameter values which
> are either
>
> a) ASCII human readable or
> b) Base64Encoded so that they conform to the imposed format for parameters.
> (Note, there are other encoding schemes that will do this as well, notably
> URLEncode.encode(String))
>
> As it is difficult to get at the actual bytes of a serialized object (done
> by writing the object to a file and
> opening it as a FileInputStream to read bytes from) in an applet so you can
> encode them
> it is not a practical thing to attempt.
>
> Obviously, for some objects, it is possible to get at the internal structure
> for the above purpose, again
> though, really rather impractical.
>
> I see this problem being asked about almost daily and it nearly always
> involves sending String's
> as a serialized object which is totally unnecessary.
>
> Your content type should be the usual for a POST|GET method request.
> (application/x-www-form-urlencoded)
> you should use a DataOutputStream to send your URLEncode'd parameter string
> to the server
> via writeBytes(String params)
> flush and close the connection then open and read the response.
>
> Simple when you know how. It pays to be a lot better informed about what the
> HTTP protocol
> imposes on you and how that relates to using Java to connect to a server.
>
> I don't have the relevant links for the RFC's to hand however www.w3c.org is
> a good place to start.
>
> Andy Bailey
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html