SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
1 © 2001-2003 Marty Hall, Larry Brown http://www.corewebprogramming.com
core
programming
Using Applets
as Front Ends to
Server-Side Programs
Applet Front Ends2 www.corewebprogramming.com
Agenda
• Sending GET data and having the browser
display the results
• Sending GET data and processing the
results within the applet (HTTP tunneling)
• Using object serialization to exchange high-
level data structures between applets and
servlets
• Sending POST data and processing the
results within the applet (HTTP tunneling)
• Bypassing the HTTP server altogether
Applet Front Ends3 www.corewebprogramming.com
Sending GET Request and
Displaying Resultant Page
• Applet requests that browser display page –
showDocument
try {
URL programURL =
new URL(baseURL + "?" + someData);
getAppletContext().showDocument(programURL);
} catch(MalformedURLException mue) { ... };
• URL-encode the form data
String someData =
name1 + "=" + URLEncoder.encode(val1) + "&" +
name2 + "=" + URLEncoder.encode(val2) + "&" +
...
nameN + "=" + URLEncoder.encode(valN);
Applet Front Ends4 www.corewebprogramming.com
GET Request Example: Applet
public class SearchApplet extends Applet
implements ActionListener {
...
public void actionPerformed(ActionEvent event) {
String query =
URLEncoder.encode(queryField.getText());
SearchSpec[] commonSpecs =
SearchSpec.getCommonSpecs();
for(int i=0; i<commonSpecs.length-1; i++) {
try {
SearchSpec spec = commonSpecs[i];
URL searchURL =
new URL(spec.makeURL(query, "10"));
String frameName = "results" + i;
getAppletContext().showDocument(searchURL,
frameName);
} catch(MalformedURLException mue) {}
}
}
}
Applet Front Ends5 www.corewebprogramming.com
GET Request Example:
Utility Class
public class SearchSpec {
private String name, baseURL, numResultsSuffix;
private static SearchSpec[] commonSpecs =
{ new SearchSpec("google",
"http://www.google.com/search?q=",
"&num="),
... };
public String makeURL(String searchString,
String numResults) {
return(baseURL + searchString +
numResultsSuffix + numResults);
}
...
}
Applet Front Ends6 www.corewebprogramming.com
Get Request Example:
HTML File
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">
<HTML>
<HEAD>
<TITLE>Parallel Search Engine Results</TITLE>
</HEAD>
<FRAMESET ROWS="120,*">
<FRAME SRC="SearchAppletFrame.html" SCROLLING="NO">
<FRAMESET COLS="*,*,*">
<FRAME SRC="GoogleResultsFrame.html" NAME="results0">
<FRAME SRC="InfoseekResultsFrame.html" NAME="results1">
<FRAME SRC="LycosResultsFrame.html" NAME="results2">
</FRAMESET>
</FRAMESET>
Applet Front Ends7 www.corewebprogramming.com
Get Request:
Initial Result
Applet Front Ends8 www.corewebprogramming.com
GET Request:
Submission Result
Applet Front Ends9 www.corewebprogramming.com
HTTP Tunneling
• Idea
– Open a socket connection to port 80 on the server and
communicate through HTTP
• Advantages
– Communicate through firewalls
– Server-side programs only needs to return the data, not a
complete HTML document
• Disadvantages
– Can only tunnel to server from which the applet was
loaded
– Applet, not browser, receives the response
• Cannot easily display HTML
Applet Front Ends10 www.corewebprogramming.com
HTTP Tunneling and GET
Requests
• Create URL object referring to applet’s host
URL dataURL = new URL(…);
• Create a URLConnection object
URLConnection connection = dataURL.openConnection();
• Instruct browser not to cache URL data
connection.setUseCaches(false);
• Set any desired HTTP headers
• Create an input stream
– Call connection.getInputStream; wrap in higher-level
stream
• Read data sent from server
– E.g., call readLine on BufferedReader
• Close the input stream
Applet Front Ends11 www.corewebprogramming.com
HTTP Tunneling Template:
Client Side
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);
URLConnection connection = dataURL.getConnection();
connection.setUseCaches(false);
connection.setRequestProperty("header", "value");
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
doSomethingWith(line);
}
in.close();
Applet Front Ends12 www.corewebprogramming.com
Using Object Serialization with
HTTP Tunneling
• Idea
– Server-side program (servlet) sends complete Java object
– Client-side program (applet) reads it
• Client-side program (applet) template:
ObjectInputStream in =
new ObjectInputStream(
connection.getInputStream());
SomeClass object = (SomeClass)in.readObject();
doSomethingWith(object);
Applet Front Ends13 www.corewebprogramming.com
Using Object Serialization with
HTTP Tunneling (Continued)
• Server-side program (servlet) template:
String contentType =
"application/x-java-serialized-object";
response.setContentType(contentType);
ObjectOutputStream out =
new ObjectOutputStream(
response.getOutputStream());
SomeClass object = new SomeClass(...);
out.writeObject(value);
out.flush();
Applet Front Ends14 www.corewebprogramming.com
Example: Live Scrolling Data
Applet Front Ends15 www.corewebprogramming.com
Sending POST Data to Server
• Applet sends POST request to server
• Processes the response directly
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);
URLConnection connection =
dataURL.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
Applet Front Ends16 www.corewebprogramming.com
Sending POST Data to Server
(Continued)
• Character or Binary Data
ByteArrayOutputStream byteStream =
new ByteArrayOutputStream(512);
PrintWriter out = new PrintWriter(byteStream, true);
out.print(data);
out.flush();
connection.setRequestProperty(
"Content-Length",
String.valueOf(byteStream.size()));
connection.setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded");
byteStream.writeTo(connection.getOutputStream());
Applet Front Ends17 www.corewebprogramming.com
Sending POST Data to Server
• Serialized Data
ByteArrayOutputStream byteStream =
new ByteArrayOutputStream(512);
ObjectOutputStream out =
new ObjectOutputStream(byteStream);
out.writeObject(data);
out.flush();
connection.setRequestProperty(
"Content-Length",
String.valueOf(byteStream.size()));
connection.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
byteStream.writeTo(connection.getOutputStream());
Applet Front Ends18 www.corewebprogramming.com
Sending POST Data: Example
• Sends data
to a servlet
that returns
an HTML page
showing form
data it receives
– Displays result
in an AWT
TextArea
Applet Front Ends19 www.corewebprogramming.com
Bypassing the HTTP Server
• If you are using applets, you don't have to
communicate via HTTP
– JDBC
– RMI
– SOAP (perhaps via JAX-RPC)
– Raw sockets
• Advantages
– Simpler
– More efficient
• Disadvantages
– Can only talk to server from which applet was loaded
– Subject to firewall restrictions
– You have to have a second server running
Applet Front Ends20 www.corewebprogramming.com
Summary
• Send data via GET and showDocument
– Can access any URL
– Only browser sees result
• Send data via GET and URLConnection
– Can only access URLs on applet's home host
– Applet sees results
– Applet can send simple data
– Server can send complex data (including Java objects)
• Send data via POST and URLConnection
– Can only access URLs on applet's home host
– Applet sees results
– Applet can send complex data (including Java objects)
– Server can send complex data (including Java objects)
• Bypass Web Server
21 © 2001-2003 Marty Hall, Larry Brown http://www.corewebprogramming.com
core
programming
Questions?

Mais conteúdo relacionado

Mais procurados

Apache server configuration & optimization
Apache server configuration & optimizationApache server configuration & optimization
Apache server configuration & optimization
Gokul Muralidharan
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
vikram singh
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarni
webhostingguy
 
Data Access Mobile Devices
Data Access Mobile DevicesData Access Mobile Devices
Data Access Mobile Devices
venkat987
 
Web Server(Apache),
Web Server(Apache), Web Server(Apache),
Web Server(Apache),
webhostingguy
 
Configuring the Apache Web Server
Configuring the Apache Web ServerConfiguring the Apache Web Server
Configuring the Apache Web Server
webhostingguy
 

Mais procurados (20)

Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Database Connection Pooling With c3p0
Database Connection Pooling With c3p0
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection api
 
Apache server configuration & optimization
Apache server configuration & optimizationApache server configuration & optimization
Apache server configuration & optimization
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
 
Get and post methods in php - phpgurukul
Get and post methods in php  - phpgurukulGet and post methods in php  - phpgurukul
Get and post methods in php - phpgurukul
 
Apache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya KulkarniApache Web Server Architecture Chaitanya Kulkarni
Apache Web Server Architecture Chaitanya Kulkarni
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programming
 
Data Access Mobile Devices
Data Access Mobile DevicesData Access Mobile Devices
Data Access Mobile Devices
 
Html intake 38 lect1
Html intake 38 lect1Html intake 38 lect1
Html intake 38 lect1
 
Common Gateway Interface
Common Gateway InterfaceCommon Gateway Interface
Common Gateway Interface
 
W3 C11
W3 C11W3 C11
W3 C11
 
Apache web service
Apache web serviceApache web service
Apache web service
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Web Server(Apache),
Web Server(Apache), Web Server(Apache),
Web Server(Apache),
 
SQL injection basics
SQL injection basicsSQL injection basics
SQL injection basics
 
Copy of cgi
Copy of cgiCopy of cgi
Copy of cgi
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
java networking
 java networking java networking
java networking
 
Configuring the Apache Web Server
Configuring the Apache Web ServerConfiguring the Apache Web Server
Configuring the Apache Web Server
 

Destaque (8)

Asif
AsifAsif
Asif
 
Applet intro
Applet introApplet intro
Applet intro
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 
Applet Vs Servlet
Applet Vs ServletApplet Vs Servlet
Applet Vs Servlet
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Java Applet
Java AppletJava Applet
Java Applet
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 

Semelhante a 21servers And Applets

Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
Aren Zomorodian
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
jobandesther
 
Networking
NetworkingNetworking
Networking
Tuan Ngo
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
Mousmi Pawar
 
0_Leksion_Web_Servers (1).pdf
0_Leksion_Web_Servers (1).pdf0_Leksion_Web_Servers (1).pdf
0_Leksion_Web_Servers (1).pdf
Zani10
 

Semelhante a 21servers And Applets (20)

T2
T2T2
T2
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Url programming
Url programmingUrl programming
Url programming
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
Chapter 27 Networking - Deitel & Deitel
Chapter 27 Networking - Deitel & DeitelChapter 27 Networking - Deitel & Deitel
Chapter 27 Networking - Deitel & Deitel
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
A.java
A.javaA.java
A.java
 
Cgi
CgiCgi
Cgi
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 
WebTalk - Implementing Web Services with a dedicated Java daemon
WebTalk - Implementing Web Services with a dedicated Java daemonWebTalk - Implementing Web Services with a dedicated Java daemon
WebTalk - Implementing Web Services with a dedicated Java daemon
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Performance #4 network
Performance #4  networkPerformance #4  network
Performance #4 network
 
Networking
NetworkingNetworking
Networking
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019
 
AJAX
AJAXAJAX
AJAX
 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
 
0_Leksion_Web_Servers (1).pdf
0_Leksion_Web_Servers (1).pdf0_Leksion_Web_Servers (1).pdf
0_Leksion_Web_Servers (1).pdf
 

Mais de Adil Jafri

Csajsp Chapter5
Csajsp Chapter5Csajsp Chapter5
Csajsp Chapter5
Adil Jafri
 
Programming Asp Net Bible
Programming Asp Net BibleProgramming Asp Net Bible
Programming Asp Net Bible
Adil Jafri
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
Adil Jafri
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
Adil Jafri
 
Csajsp Chapter10
Csajsp Chapter10Csajsp Chapter10
Csajsp Chapter10
Adil Jafri
 
Flashmx Tutorials
Flashmx TutorialsFlashmx Tutorials
Flashmx Tutorials
Adil Jafri
 
Java For The Web With Servlets%2cjsp%2cand Ejb
Java For The Web With Servlets%2cjsp%2cand EjbJava For The Web With Servlets%2cjsp%2cand Ejb
Java For The Web With Servlets%2cjsp%2cand Ejb
Adil Jafri
 
Csajsp Chapter12
Csajsp Chapter12Csajsp Chapter12
Csajsp Chapter12
Adil Jafri
 
Flash Tutorial
Flash TutorialFlash Tutorial
Flash Tutorial
Adil Jafri
 

Mais de Adil Jafri (20)

Csajsp Chapter5
Csajsp Chapter5Csajsp Chapter5
Csajsp Chapter5
 
Php How To
Php How ToPhp How To
Php How To
 
Php How To
Php How ToPhp How To
Php How To
 
Owl Clock
Owl ClockOwl Clock
Owl Clock
 
Phpcodebook
PhpcodebookPhpcodebook
Phpcodebook
 
Phpcodebook
PhpcodebookPhpcodebook
Phpcodebook
 
Programming Asp Net Bible
Programming Asp Net BibleProgramming Asp Net Bible
Programming Asp Net Bible
 
Tcpip Intro
Tcpip IntroTcpip Intro
Tcpip Intro
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
 
Jsp Tutorial
Jsp TutorialJsp Tutorial
Jsp Tutorial
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
 
Csajsp Chapter10
Csajsp Chapter10Csajsp Chapter10
Csajsp Chapter10
 
Javascript
JavascriptJavascript
Javascript
 
Flashmx Tutorials
Flashmx TutorialsFlashmx Tutorials
Flashmx Tutorials
 
Java For The Web With Servlets%2cjsp%2cand Ejb
Java For The Web With Servlets%2cjsp%2cand EjbJava For The Web With Servlets%2cjsp%2cand Ejb
Java For The Web With Servlets%2cjsp%2cand Ejb
 
Html Css
Html CssHtml Css
Html Css
 
Digwc
DigwcDigwc
Digwc
 
Csajsp Chapter12
Csajsp Chapter12Csajsp Chapter12
Csajsp Chapter12
 
Html Frames
Html FramesHtml Frames
Html Frames
 
Flash Tutorial
Flash TutorialFlash Tutorial
Flash Tutorial
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

21servers And Applets

  • 1. 1 © 2001-2003 Marty Hall, Larry Brown http://www.corewebprogramming.com core programming Using Applets as Front Ends to Server-Side Programs
  • 2. Applet Front Ends2 www.corewebprogramming.com Agenda • Sending GET data and having the browser display the results • Sending GET data and processing the results within the applet (HTTP tunneling) • Using object serialization to exchange high- level data structures between applets and servlets • Sending POST data and processing the results within the applet (HTTP tunneling) • Bypassing the HTTP server altogether
  • 3. Applet Front Ends3 www.corewebprogramming.com Sending GET Request and Displaying Resultant Page • Applet requests that browser display page – showDocument try { URL programURL = new URL(baseURL + "?" + someData); getAppletContext().showDocument(programURL); } catch(MalformedURLException mue) { ... }; • URL-encode the form data String someData = name1 + "=" + URLEncoder.encode(val1) + "&" + name2 + "=" + URLEncoder.encode(val2) + "&" + ... nameN + "=" + URLEncoder.encode(valN);
  • 4. Applet Front Ends4 www.corewebprogramming.com GET Request Example: Applet public class SearchApplet extends Applet implements ActionListener { ... public void actionPerformed(ActionEvent event) { String query = URLEncoder.encode(queryField.getText()); SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs(); for(int i=0; i<commonSpecs.length-1; i++) { try { SearchSpec spec = commonSpecs[i]; URL searchURL = new URL(spec.makeURL(query, "10")); String frameName = "results" + i; getAppletContext().showDocument(searchURL, frameName); } catch(MalformedURLException mue) {} } } }
  • 5. Applet Front Ends5 www.corewebprogramming.com GET Request Example: Utility Class public class SearchSpec { private String name, baseURL, numResultsSuffix; private static SearchSpec[] commonSpecs = { new SearchSpec("google", "http://www.google.com/search?q=", "&num="), ... }; public String makeURL(String searchString, String numResults) { return(baseURL + searchString + numResultsSuffix + numResults); } ... }
  • 6. Applet Front Ends6 www.corewebprogramming.com Get Request Example: HTML File <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN"> <HTML> <HEAD> <TITLE>Parallel Search Engine Results</TITLE> </HEAD> <FRAMESET ROWS="120,*"> <FRAME SRC="SearchAppletFrame.html" SCROLLING="NO"> <FRAMESET COLS="*,*,*"> <FRAME SRC="GoogleResultsFrame.html" NAME="results0"> <FRAME SRC="InfoseekResultsFrame.html" NAME="results1"> <FRAME SRC="LycosResultsFrame.html" NAME="results2"> </FRAMESET> </FRAMESET>
  • 7. Applet Front Ends7 www.corewebprogramming.com Get Request: Initial Result
  • 8. Applet Front Ends8 www.corewebprogramming.com GET Request: Submission Result
  • 9. Applet Front Ends9 www.corewebprogramming.com HTTP Tunneling • Idea – Open a socket connection to port 80 on the server and communicate through HTTP • Advantages – Communicate through firewalls – Server-side programs only needs to return the data, not a complete HTML document • Disadvantages – Can only tunnel to server from which the applet was loaded – Applet, not browser, receives the response • Cannot easily display HTML
  • 10. Applet Front Ends10 www.corewebprogramming.com HTTP Tunneling and GET Requests • Create URL object referring to applet’s host URL dataURL = new URL(…); • Create a URLConnection object URLConnection connection = dataURL.openConnection(); • Instruct browser not to cache URL data connection.setUseCaches(false); • Set any desired HTTP headers • Create an input stream – Call connection.getInputStream; wrap in higher-level stream • Read data sent from server – E.g., call readLine on BufferedReader • Close the input stream
  • 11. Applet Front Ends11 www.corewebprogramming.com HTTP Tunneling Template: Client Side 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); URLConnection connection = dataURL.getConnection(); connection.setUseCaches(false); connection.setRequestProperty("header", "value"); BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { doSomethingWith(line); } in.close();
  • 12. Applet Front Ends12 www.corewebprogramming.com Using Object Serialization with HTTP Tunneling • Idea – Server-side program (servlet) sends complete Java object – Client-side program (applet) reads it • Client-side program (applet) template: ObjectInputStream in = new ObjectInputStream( connection.getInputStream()); SomeClass object = (SomeClass)in.readObject(); doSomethingWith(object);
  • 13. Applet Front Ends13 www.corewebprogramming.com Using Object Serialization with HTTP Tunneling (Continued) • Server-side program (servlet) template: String contentType = "application/x-java-serialized-object"; response.setContentType(contentType); ObjectOutputStream out = new ObjectOutputStream( response.getOutputStream()); SomeClass object = new SomeClass(...); out.writeObject(value); out.flush();
  • 14. Applet Front Ends14 www.corewebprogramming.com Example: Live Scrolling Data
  • 15. Applet Front Ends15 www.corewebprogramming.com Sending POST Data to Server • Applet sends POST request to server • Processes the response directly 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); URLConnection connection = dataURL.openConnection(); connection.setUseCaches(false); connection.setDoOutput(true);
  • 16. Applet Front Ends16 www.corewebprogramming.com Sending POST Data to Server (Continued) • Character or Binary Data ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512); PrintWriter out = new PrintWriter(byteStream, true); out.print(data); out.flush(); connection.setRequestProperty( "Content-Length", String.valueOf(byteStream.size())); connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); byteStream.writeTo(connection.getOutputStream());
  • 17. Applet Front Ends17 www.corewebprogramming.com Sending POST Data to Server • Serialized Data ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512); ObjectOutputStream out = new ObjectOutputStream(byteStream); out.writeObject(data); out.flush(); connection.setRequestProperty( "Content-Length", String.valueOf(byteStream.size())); connection.setRequestProperty( "Content-Type", "application/x-java-serialized-object"); byteStream.writeTo(connection.getOutputStream());
  • 18. Applet Front Ends18 www.corewebprogramming.com Sending POST Data: Example • Sends data to a servlet that returns an HTML page showing form data it receives – Displays result in an AWT TextArea
  • 19. Applet Front Ends19 www.corewebprogramming.com Bypassing the HTTP Server • If you are using applets, you don't have to communicate via HTTP – JDBC – RMI – SOAP (perhaps via JAX-RPC) – Raw sockets • Advantages – Simpler – More efficient • Disadvantages – Can only talk to server from which applet was loaded – Subject to firewall restrictions – You have to have a second server running
  • 20. Applet Front Ends20 www.corewebprogramming.com Summary • Send data via GET and showDocument – Can access any URL – Only browser sees result • Send data via GET and URLConnection – Can only access URLs on applet's home host – Applet sees results – Applet can send simple data – Server can send complex data (including Java objects) • Send data via POST and URLConnection – Can only access URLs on applet's home host – Applet sees results – Applet can send complex data (including Java objects) – Server can send complex data (including Java objects) • Bypass Web Server
  • 21. 21 © 2001-2003 Marty Hall, Larry Brown http://www.corewebprogramming.com core programming Questions?