Learn Advanced JAVA at ASIT
Transcript of 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
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:
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);
}
}
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
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
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.
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
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)
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
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
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.
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