+ All Categories
Home > Education > Learn Advanced JAVA at ASIT

Learn Advanced JAVA at ASIT

Date post: 21-Jul-2015
Category:
Upload: asit
View: 65 times
Download: 4 times
Share this document with a friend
Popular Tags:
13
Transcript
Page 1: Learn Advanced JAVA at ASIT
Page 2: Learn Advanced JAVA at ASIT

Overview To create a simple HTTP server in Java

To use the implementation to illustrate a number of advanced Java features:

TCP/IP Sockets and Server Sockets

Interfaces

Software components (more from John later)

Multithreading

To show how to create executable server objects (using Sun’s Servlets API)

(Pseudo) requirements

Server must be able to process HTTP/1.0 file transfer requests and deliver files

Connections are to be made via TCP/IP

Must be efficient and prompt

Must be simple to understand and elegant in design

Page 3: Learn Advanced JAVA at ASIT

HTTP protocol Developed by Tim Berners-Lee at CERN

Like most Internet protocols it is described in an RFC (Request for Comment document): RFC1945

May be downloaded from the Internet Engineering Task Force’s web site: http://www.ietf.org

Server example Some of you may have covered this in the introductory Java course

Servers have a listener loop

Loop until the server is shutdown

Wait for a client to request a connection

Read the details of the client’s request

Provide the requested information to the client

Here’s the listener loop from our example:

Page 4: Learn Advanced JAVA at ASIT

HttpServerServerSocket socket = new ServerSocket(80, 5);

public void listen()

throws IllegalAccessException,

InstantiationException,

IOException

{

for (;;) {

System.err.println("HttpServer: waiting...");

Socket s = socket.accept();

FileServer f = createFileServer();

f.dispatch(s);

}

}

Page 5: Learn Advanced JAVA at ASIT

Interfaces benefit clients

Simplifies client implementation

Clients do not need to worry about the implementation details

Interfaces encapsulate state of different subsystems side effects reduced

Define clear boundaries between different teams of programmers

Clients can substitute alternative implementations: polymorphism

Clients can purchase off the shelf solutions: software components

Implementing FileServer Two flavours of FileServer have been provided using deferred instantiation

A simple one but with low performance: sea.server.SimpleFileServer

A server that uses multiple threads to increase performance: sea.server.ThreadedFileServer

A server which uses a pool of threads to achieve the maximum possible performance: sea.server.ThreadedServer2

Page 6: Learn Advanced JAVA at ASIT

Making the server programmable

Our example web server performs a very simple task

Accept a request from a client

Retrieve the appropriate document from disk

Return the document to the client

This is too limiting

How do we implement searches?

We need to be able to run programs within the server to process user requests

Accept a client request including arguments

Run a program on the arguments

Return results in the form of a document

Page 7: Learn Advanced JAVA at ASIT

Servlets

When we run small Java programs within a browser these are referred to as Applets. . .

so we run small Java programs within a server these are “Servlets”

A servlet is a program designed to process a client request (which requires interactivity).

It processes arguments and formats its results as a short liveddocument.

HTML servlets are becoming a popular mechanism for creating interactiveservers.

Page 8: Learn Advanced JAVA at ASIT

Import servlet methods

When the servlet is first loaded it makes a single call to the method

public void init(ServletConfig config)

This may optionally be overridden to initialise the state of the servlet (for example loading state information from a file).

When a servlet is finally unloaded it makes a single call to the method

public void destroy()

If you wish to save to servlet state to a file (or using JDBC) this is the method to override

Page 9: Learn Advanced JAVA at ASIT

A simple chat server

An web based chat room server

A number of users can connect to the servlet using browsers

Read a list of the previous messages

Optionally append new messages to the list

Messages are attributed to a specific author and are time stamped

Messages do not persist after the chat server is stopped (easy enough to rectify)

Page 10: Learn Advanced JAVA at ASIT

Performance

Servlets offer better performance than most of the previous CGI like technologies

But CGI/Servlets concentrate the load on the server

When designing high throughput servers only use servlets where you really need interactivity

Searches/Shopping carts

Data that is very short lived (stock quotes)

This also applies to low throughput servers that might need to scale later

Page 11: Learn Advanced JAVA at ASIT

Pull versus Push transports

How can a chat reader find out when a new message has been posted by another author?

Only by repeatedly hitting the Reload button!

HTTP (& TCP/IP services in general) transfer documents on the user’s request

To push updates automatically from the server you will need to:

Start a reverse server within each client

Use a multicast group

Use a remote procedure call system such as RMI or CORBA

Page 12: Learn Advanced JAVA at ASIT

Servlets and JSP

Java Server Pages is an extension to the servlets API.

With conventional servlets you embed the HTML that you need inside a Java program.

With JSP you embed your Java program within a HTML document (by using special tags).

Works rather like JavaScript but the JSP script runs on the server before the page is dispatched to the user’s browser.

Page 13: Learn Advanced JAVA at ASIT

we provide online and classroom training for AdvancedJAVA

For More Details www.asit.amcsquare.com

Wise Machines India Pvt Ltd#360, Sri Sai Padma Arcade,Varthur Main Road,Ramagondanahalli,Whitefiled ,Bangalore – 560066.

We also having Branches in Hyderabad & Chennai


Recommended