Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

applet and servlet

  Asked By: Jason    Date: Sep 28    Category: Java    Views: 1074
  

how can i connect to a servlet through an applet and send it parameters as well?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Calvin Banks     Answered On: Sep 28

This is a piece of working code I used years ago to do this -
it may prove of some use to you (the comments may be of more use however)

db


/*------------------------------------------------------------------------------\
------------------
After opening the URL connection, we have to tell the runtime system
explicitly what we are going to be doing.
We want to both send  and receive data on this connection so we set both
the input and output flags.
We then turn off the use of caching since we want to make sure that the
data we read back from the server is
actually in response to the data that we send it.
Finally, we set the HTTP request's "content-type" field to be
"application/x-www-form-urlencoded."
That is necessary since some version of Netscape's browser has a bug
which does not give out this proper content type when doing POSTs.

GET
We use applet.getAppletContext().showDocument(url); to show the result
from the servlet  in a browser page.

POST
Now, after chanting all of those necessary magical incantations, we can
create and open an output stream for that URL connection.
We then write the data we want POSTed to the servlet script on the server.
It is very important that we close the output stream immediately after
we write the data.
If we don't close the stream then the servlet will sit waiting for more
data (and so will not yet be sending its response). */
//------------------------------------------------------------------------------\
------------------
public ByteArrayStream request( String reqstr, boolean getPage)
{
String Urls = new String(site+"servlet/"+servlet);
String reqstr = reqstr.URLEncode();

if(getPage) // DO GET
{
try
{
AppletContext context = getAppletContext();
URL url = new URL(Urls+"?"+reqstr);
URLConnection urlConn = url.openConnection();
urlConn.setDoOutput (true); // Let the
RTS know that we want to do output.
urlConn.setUseCaches (false); // No caching,
we want the real thing.
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded"); // Specify the content type.
context.showDocument(url);
return null;

}
catch( Exception x )
{
x.printStackTrace();
}
}
else // (DO POST)
{
try
{
URL url = new URL(Urls);
URLConnection urlConn = url.openConnection();
urlConn.setDoInput(true); // Let the
run-time system (RTS) know that we want input.
urlConn.setDoOutput (true); // Let the RTS
know that we want to do output.
urlConn.setUseCaches (false); // No caching, we
want the real thing.
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded"); // Specify the content type.

// Send POST output.
DataOutputStream printout = new DataOutputStream
(urlConn.getOutputStream ());

printout.writeBytes (reqstr);
printout.flush ();
printout.close ();

// Get response data.
int len = urlConn.getContentLength();
if(len<0) return null;

DataInputStream is = new
DataInputStream(urlConn.getInputStream());

// this bit is specific to what your servlet sends back
int ilen = is.readInt();
byte ibuf[] = new byte[ilen];
is.readFully(ibuf);
ByteArrayStream os = new ByteArrayStream(ibuf);
return os;
} catch (IOException ignored) {}
} // end else (DO POST)
return null;
}

 
Didn't find what you were looking for? Find more on applet and servlet Or get search suggestion and latest updates.




Tagged: