• No results found

20 Servers and Applets pdf

N/A
N/A
Protected

Academic year: 2020

Share "20 Servers and Applets pdf"

Copied!
11
0
0

Loading.... (view fulltext now)

Full text

(1)

© 2009 Marty Hall

Using Applets

g

pp

as Front Ends to

Server Side Programs

Server-Side Programs

Originals of Slides and Source Code for Examples:

http://courses coreservlets com/Course-Materials/java5 html

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

2

http://courses.coreservlets.com/Course Materials/java5.html

© 2009 Marty Hall

For live Java training, please see training courses at

http://courses.coreservlets.com/. Servlets, JSP, Struts

http://courses.coreservlets.com/. Servlets, JSP, Struts

Classic, Struts 2, JSF 1.x, JSF 2.0, Ajax (with jQuery,

Dojo, Prototype, Ext, etc.), GWT, Java 5, Java 6, Spring,

Hibernate/JPA and customized combinations of topics

Hibernate/JPA, and customized combinations of topics.

Taught by the author of Core Servlets and JSP, More

Servlets and JSP and this tutorial Available at public

Customized Java EE Training: http://courses.coreservlets.com/

Servlets, JSP, JSF 1.x& JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.

Servlets and JSP, and this tutorial. Available at public

venues, or customized versions can be held on-site at your

(2)

Agenda

Sending GET data and having the browser

di

l

h

l

display the results

Sending GET data and processing the

results within the applet (HTTP tunneling)

results within the applet (HTTP tunneling)

Using object serialization to exchange

high-level data structures between applets and

level data structures between applets and

servlets

Sending POST data and processing the

Sending POST data and processing the

results within the applet (HTTP tunneling)

Bypassing the HTTP server altogether

y

g

g

4

Sending GET Request and

Displaying Resultant Page

Displaying Resultant Page

Applet requests that browser display page –

h

D

showDocument

try {

URL programURL = p g

new URL(baseURL + "?" + someData);

getAppletContext().showDocument(programURL);

} catch(MalformedURLException mue) { ... }; } catch(MalformedURLException mue) { ... };

URL-encode the form data

String someData = String someData

name1 + "=" + URLEncoder.encode(val1) + "&" + name2 + "=" + URLEncoder.encode(val2) + "&" + ...

...

(3)

GET Request Example: Applet

public class SearchApplet extends Applet

implements ActionListener {p {

...

public void actionPerformed(ActionEvent event) { String query =

URLEncoder encode(queryField getText()); 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")); new URL(spec.makeURL(query, 10 ));

String frameName = "results" + i;

getAppletContext().showDocument(searchURL, frameName);

} catch(MalformedURLException mue) {} }

} }

6

GET Request Example:

Utility Class

Utility Class

public class SearchSpec {

private String name baseURL numResultsSuffix; private String name, baseURL, numResultsSuffix;

private static SearchSpec[] commonSpecs = { new SearchSpec("google"

{ new SearchSpec( google ,

"http://www.google.com/search?q=", "&num="),

... }; ... };

public String makeURL(String searchString, String numResults) { St g u esu ts) { return(baseURL + searchString +

numResultsSuffix + numResults); }

(4)

Get Request Example:

HTML File

HTML File

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">

<HTML> <HTML>

<HEAD>

<TITLE>Parallel Search Engine Results</TITLE>

</HEAD> </HEAD>

<FRAMESET ROWS="120,*">

<FRAME SRC="SearchAppletFrame.html" SCROLLING="NO">pp

<FRAMESET COLS="*,*,*">

<FRAME SRC="GoogleResultsFrame.html" NAME="results0">

<FRAME SRC="InfoseekResultsFrame.html" NAME="results1">

<FRAME SRC="LycosResultsFrame.html" NAME="results2">

</FRAMESET>

</FRAMESET>

8

(5)

GET Request:

Submission Result

Submission Result

10

HTTP Tunneling

Idea

Open a socket connection to port 80 on the server and

communicate through HTTP

Advantages

Advantages

Communicate through firewalls

Server-side programs only needs to return the data, not a

S

p g

y

,

complete HTML document

Disadvantages

Can only tunnel to server from which the applet was

loaded

Applet

Applet

, not

not

browser

browser

, receives the response

receives the response

(6)

HTTP Tunneling and GET

Requests

Requests

Create URL object referring to applet’s host

URL dataURL = new URL(…);

Create a URLConnection object

URLConnection connection = dataURL.openConnection(); URLConnection connection dataURL.openConnection();

Instruct browser not to cache URL data

connection.setUseCaches(false);

Set any desired HTTP headers

Create an input stream

C ll

i hi h

l

l

Call

connection.getInputStream

; wrap in higher-level

stream

Read data sent from server

E.g., call

readLine

on

BufferedReader

Close the input stream

12

HTTP Tunneling Template:

Client Side

Client Side

URL currentPage = getCodeBase();

String protocol = currentPage getProtocol(); String protocol currentPage.getProtocol(); String host = currentPage.getHost();

int port = currentPage.getPort();

String urlSuffix = "/servlet/SomeServlet";g

URL dataURL = new URL(protocol, host, port, urlSuffix); URLConnection connection = dataURL.getConnection(); connection.setUseCaches(false);

connection.setRequestProperty("header", "value");

BufferedReader in = new BufferedReader(

I tSt R d ( ti tI tSt ()))

new InputStreamReader(connection.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

doSomethingWith(line);

doSomethingWith(line); }

(7)

Using Object Serialization with

HTTP Tunneling

HTTP Tunneling

Idea

Server-side program (servlet) sends complete Java object

Client-side program (applet) reads it

Client side program (applet) template:

Client-side program (applet) template:

ObjectInputStream in = new ObjectInputStream(

connection.getInputStream());

SomeClass object = (SomeClass)in.readObject(); SomeClass object (SomeClass)in.readObject(); doSomethingWith(object);

14

Using Object Serialization with

HTTP Tunneling (Continued)

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);

(8)

Example: Live Scrolling Data

16

Sending POST Data to Server

Applet sends POST request to server

Processes the response directly

Url currentPage = getCodeBase();

String protocol = currentPage getProtocol(); String protocol = currentPage.getProtocol(); String host = currentPage.getHost();

int port = currentPage.getPort();

String urlSuffix = "/servlet/SomeServlet"; String urlSuffix = /servlet/SomeServlet ; URL dataURL = new URL(protocol, host, port,

urlSuffix); URLConnection connection =

(9)

Sending POST Data to Server

(Continued)

(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()));g y

connection.setRequestProperty( "Content-Type",

"application/x-www-form-urlencodedpp / ");); byteStream.writeTo(connection.getOutputStream());

18

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",g

String.valueOf(byteStream.size())); connection.setRequestProperty(

"Content-Type", yp ,

(10)

Sending POST Data: Example

Sends data

l

to a servlet

that returns

an HTML page

an HTML page

showing form

data it receives

Displays result

in an AWT

TextArea

TextArea

20

Bypassing the HTTP Server

If you are using applets, you don't have to

i

t

i HTTP

communicate via HTTP

JDBC

RMI

RMI

SOAP (perhaps via JAX-RPC)

Raw sockets

Advantages

Simpler

More efficient

More efficient

Disadvantages

Can only talk to server from which applet was loaded

Can only talk to server from which applet was loaded

Subject to firewall restrictions

(11)

Summary

Send data via GET and showDocument

C

URL

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

Applet can send simple data

Server can send complex data (including Java objects)

Send data via POST and URLConnection

C

l

URL

l t' h

h t

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

22

© 2009 Marty Hall

Questions?

Questions?

Customized Java EE Training: http://courses.coreservlets.com/

References

Related documents

Namely, generic elementary embeddings are used in order to characterize levels of supercompact- ness, almost huge cardinals and super almost huge cardinals through Neeman’s pure

By subtracting all the proteins that were identified as orthologs from the groups of paralogs and unique genes, we were left with only the protein pro- ducts of gene models that

Hipped roof, covered in red plain clay tiles, with red brick construction to walls and sandstone plinth, and brick dentil course to eaves. Small diamond-shaped aeration

Summing up, taking account of a feedback e¤ect of unemployment on the disutility of labor alters the welfare impact of an expansionary monetary policy shock in an open economy (and,

Off eastern Newfoundland, the depth-averaged ocean temperature ranged from a record low during 1991 (high NAO index in preceding winter), a near record high in 1996 (following

For this initial study, the JUMP lander core stages are constrained to a launch mass of under 16.8 tons, ensuring they can be launched by the Falcon Heavy or equivalent CLV..

designed to assist military operations with civil matters. 80 House Committee on Homeland Security, A Line in the Sand: Confronting the Threat at the Southwest Border, 22–23. 83

The runtime security data of the Java Card RE, like, for instance, the AIDs used to identify the installed applets, the currently selected applet, the current context of execution