The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Re: Servlets

Posted by Brian Sanders on December 07, 2001 at 12:51 PM

I'm going to assume that you have some experience with HTTP in general. If not, that's OK; just let me know if I haven't explained myself fully enough.

You have a couple of good choices, depending on how much information you need to send between the two. If you're sending a small amount of information, you may just want to pass it as a request parameter to the servlet via an HTTP GET. From the applet, you would do something like:

String relativeServletPath = "/servlets/GuestBookServlet";
String message = URLEncoder.encode("I have a lovely bunch of coconuts");
URL servletURL = new URL(getCodeBase(), relativeServletPath + "?msg=" + message);
InputStream response = servletURL.openInputStream();

On the servlet side, you would call the method ServletRequest.getParameter("msg") to get the value passed from the applet.

If you're sending a larger amount of data, you will want to use the stream methods of URLConnection (on the applet side) along with ServletRequest.getInputStream().

Note that neither of the two techniques are appropriate if the data is sensitive. The transfered data is easily visible to others on the web.

If you're looking to get started with servlet developement, I highly recommend Java Servlet Programming by Jason Hunter and William Crawford. You can get it directly from O'Reilly.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us