|
 |
servlets thread: servlets
Message #1 by "srinivas" <cnuin@u...> on Fri, 5 Jan 2001 15:18:47 -0000
|
|
found it, here is some code that does applet to servlet communication: I got
it from chapter
11 of professional java server programming. . The servlet that accepts this
request is nothing unusual, it doesnt care whether a browser or an applet is
the consumer, it cant tell the difference.
try {
// first you must instantiate a URL object with the url of the
servlet.
URL url = new URL("http://localhost:8080/servlet/DbServlet");
// you then open the connection to the URL.
URLConnection uc = url.openConnection();
// set the connection to both output and input, cache off
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
// the content type is set to URL encoded - in the original
application this was
// a url encoded sql string. I guess it is probably easier to put
together the
// sql query at the applet side rather than passing code values.
uc.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
// open a stream to the connection object for writing to.
(dataoutput streams are
// for writing bytes so we cant send characters). Write the query
string to the // stream, then flush and close it. The connection is
still open at this point.
DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
// The output stage
dos.writeBytes(qry);
dos.flush();
dos.close();
// we now open a character reader for the return value from the
servlet. In this
// case the servlet returned string values.
InputStreamReader in = new InputStreamReader(uc.getInputStream());
// read one character at a time until the end of the line. I'm
not sure why it
// reads one at a time. Possibly to make sure that it doesnt lose
all the
// information if the internet connection fails. then close the
output stream
// I suppose that the connection should be closed as well when
you are finished // with it, although this might be automatic. If
you are interested check the
// java docs for URLConnection.
int chr = in.read();
while (chr != -1) {
taResults.append(String.valueOf((char) chr));
chr = in.read();
}
in.close();
} catch(MalformedURLException e) {
taResults.setText(e.toString());
} catch(IOException e) {
taResults.setText(e.toString());
}
}
-----Original Message-----
From: Jitesh Kumar [mailto:jitesh@c...]
Sent: 09 January 2001 06:15
To: Servlets
Subject: [servlets] Re: servlets
Hi,
Applet to servlet communication can be done with HTTP Tunneling . Where as
servlet to servlet communication can be done with Servlet Chaining also
(Of'course depends on your requirement of application).
And of'course as suggested , you can start with a good book.
HTH
jitesh
At 03:38 PM 1/6/00 -0800, you wrote:
>the best book on servlet i found is "Professional java Server Programming"
>by
>WROX.
>
>----- Original Message -----
>From: srinivas <cnuin@u...>
>To: Servlets <servlets@p...>
>Sent: Friday, January 05, 2001 7:18 AM
>Subject: [servlets] servlets
>
>
> > hello friends
> >
> > please tell me about servlet to servlet communication
> > with an example and also applet to applet communication example.
> >
> > subscribe any good book for servlets and corba.
---
NEED TECHNICAL TIPS, TOOLS, AND INSIGHTS? Is FREE okay?
Visit EarthWeb for the latest in IT Management, Software Development,
Web Development, Networking & Communications, and Hardware & Systems.
Click on http://www.earthweb.com for FREE articles, tutorials,
and discussions from the experts.
---
You are currently subscribed to servlets as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-servlets-$subst('Recip.MemberIDChar')@p2p.wrox.com
|
|
 |