Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
17.5 Sending Data by POST and Processing the Results Directly
451
results). These two options are discussed in Sections 17.1 and 17.3, respec 
tively. With 
POST
 data, however, only the second option is available since the
URL
 constructor has no method to let you associate 
POST
 data with it. Sending
POST
 data has some of the same advantages and disadvantages as when
applets send 
GET
 data. The two main disadvantages are that the server side
program must be on the host from which the applet was loaded, and that the
applet is required to display all the results itself: it cannot pass HTML to the
browser in a portable manner. On the plus side, the server side program can
be simpler (not needing to wrap the results in HTML) and the applet can
update its display without requiring the page to be reloaded. Furthermore,
applets that communicate using 
POST
 can use serialized data streams to send
data  to a servlet, in addition to reading serialized data from servlets. This is
quite an advantage, since serialized data simplifies communication and
HTTP tunneling lets you piggyback on existing connections through firewalls
even when direct socket connections are prohibited. Applets using 
GET
 can
read serialized data (see Section 17.4) but are unable to send it since it is not
legal to append arbitrary binary data to URLs.
Thirteen steps are required for the applet to send 
POST
 data to the server
and read the results, as shown below. Although there are many required
steps, each step is relatively simple. The code is slightly simplified by the
omission of 
try
/
catch
 blocks around the statements.
1. Create a 
URL
 object referring to the applet's home host. 
As before, since the URL must refer to the host the applet came 
from, it makes the most sense to specify a URL suffix and con 
struct the rest of the URL automatically.
URL currentPage = getCodeBase();
String protocol = currentPage.getProtocol();
String host = currentPage.getHost();
int port = currentPage.getPort();
String urlSuffix = "/servlet/SomeServlet";
URL dataURL = 
new URL(protocol, host, port, urlSuffix);
2. Create a 
URLConnection
 object. This object will be used to 
obtain input and output streams that connect to the server.
URLConnection connection = dataURL.openConnection();
3. Instruct the browser not to cache the results. 
connection.setUseCaches(false); 
4. Tell the system to permit you to send data, not just read it. 
connection.setDoOutput(true);
Home page for this book: www.coreservlets.com; Home page for sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved