Title: Http Java Networking
1Http - Java Networking
2Introduction to Networking Http server
Http server static pages (HTML)?
Http server dynamic pages (CGI)?
Http server dynamic pages (SSI)?
Http server dynamic pages (SSI/Servlets)?
Http server dynamic pages (Pure Java Servlets)?
3Introduction to Networking Http Server
- HTTP Server Static html pages
- Allowed only text and graphics (and yes, the
annoying animated gifs)? - Basically nothing more than a file server that
displayed the html in a browser - Really a standard format for document
presentation - Worked as a pure iterative server
- Anything that worked as a presentation layer
display worked as html - Embedded graphics, formatting, more than word
- Other standards such as Microsoft Word, Adobe
PDF, RTF, LaTex, etc were binary representations,
HTML is XML text and binary mime typed graphics
4Introduction to Networking Http Server
- HTTP Server CGI need for dynamic pages
- Let other executables render the HTML pages
- CGI - common gateway interface allows any
programming language. - First iterations of CGI would allow only a
program to be execed out of the web server
process (dangerous)? - GET / Post methods
- 2 ways to get data to a process
- GET uses a plain text variables passed in
through the URL - POST CGI process receives data via standard in
- Ability to get form data as variables
5Introduction to Networking Example Server
Errors
WebBrowser
Web Server
Static
Dynamic CGI
Network
Database
Web Service
6Introduction to Networking Example Server
WebBrowser
Httpd
ReThread
HttpOutputStream
HttpInputStream
Network
HttpProcessor processRequest()?
main()?
HttpFile
HttpCGI
HttpClass
HttpException
HttpClassProcessor
RedirectException
CGI
Echo
7Introduction to Networking Example Server
- Class HTTP (HTTP.java)?
- Default values and static variables
- public static final String METHOD_GET "GET"
- public static final String METHOD_POST
"POST" - public static final String METHOD_HEAD
"HEAD" - public static final int STATUS_OKAY 200
- public static final int STATUS_NO_CONTENT
204 - public static final int STATUS_MOVED_PERMANENTLY
301 - public static final int STATUS_MOVED_TEMPORARILY
302 - public static final int STATUS_BAD_REQUEST
400 - public static final int STATUS_FORBIDDEN 403
- public static final int STATUS_NOT_FOUND 404
- public static final int STATUS_NOT_ALLOWED
405 - public static final int STATUS_INTERNAL_ERROR
500 - public static final int STATUS_NOT_IMPLEMENTED
501
8Introduction to Networking Example Server
- Class Httpd
- Main class
- public static void main (String args) throws
IOException - ServerSocket server new ServerSocket
(HTTP.PORT) - while (true)
- Socket client server.accept ()
- Httpd httpd new Httpd (client)
- ReThread reThread new ReThread (httpd)
- reThread.start ()
-
-
9Introduction to Networking Example Server
- Class HttpInputStream
- InputStream class
- public void readRequest () throws IOException
- String request readLine ()
- if (request null)?
- throw new HttpException (HTTP.STATUS_BAD_REQ
UEST, "Null query") - StringTokenizer parts new StringTokenizer
(request) - try
- parseMethod (parts.nextToken ())
- parseRequest (parts.nextToken ())
- catch (NoSuchElementException ex)
- throw new HttpException (HTTP.STATUS_BAD_REQ
UEST, request) -
- if (parts.hasMoreTokens ())?
- parseVersion (parts.nextToken ())
- else
- ..............
-
10Introduction to Networking Example Server
- Class HttpFile
- Class to deal with static files
- public HttpFile (HttpInputStream in) throws
IOException - if (in.getMethod () HTTP.METHOD_POST)?
- throw new HttpException (HTTP.STATUS_NOT_ALL
OWED, - "ltTTgt"
in.getMethod () " " in.getPath () "lt/TTgt") - file new File (HTTP.HTML_ROOT,
HTTP.translateFilename (in.getPath ())) - if (in.getPath ().endsWith ("/"))?
- file new File (file, HTTP.DEFAULT_INDEX)
- if (!file.exists ())?
- throw new HttpException (HTTP.STATUS_NOT_FOU
ND, - "File ltTTgt"
in.getPath () "lt/TTgt not found.") - if (file.isDirectory ())?
- throw new RedirectException
(HTTP.STATUS_MOVED_PERMANENTLY, - in.getPath ()
"/") - if (!file.isFile () !file.canRead ())?
- throw new HttpException (HTTP.STATUS_FORBIDD
EN, in.getPath ()) -
11Introduction to Networking Example Server
- Class HttpCGI
- Class to deal with dynamic requests
-
- public void processRequest (HttpOutputStream
out) throws IOException - ReThread drain null
- try
- if (in.getMethod () ! HTTP.METHOD_POST)
- cgi jvm.exec (cgiScript.getPath (),
env) - cgi.getOutputStream ().close ()
- out.write (cgi.getInputStream ())
- else
- cgi jvm.exec (cgiScript.getPath (),
env) - drain new ReThread (this)
- drain.start ()
- out.write (cgi.getInputStream ())
-
12Introduction to Networking Http Server - SSI
- HTTP Server SSI need for http to be
stateful - Keep in mind that http is stateless, how to we
create state? - CGI Allowed for dynamic passing of data
- Early state was passed as a key back and forth
and hidden in a non-visible variable (very
hackable)? - SSI Server Side Includes allowed for better
use of state information. Not as good as cookies,
but allowed the server to Execute code with
variables different than the CGI program - First uses of embeded code for future things
like ltphpgt and ltASPgt code - Required plugin capabilities
13Introduction to Networking Http Server -
SSI/Servlet
- HTTP Server SSI Servlets
- Let the servlet deal with state information
- Used as an object way to maintain state
- Can embed variables / etc.
- Still required plugin for different web servers
- Netscape NSAPI
- Microsoft (IIS) ISAPI
- Would only run SSI through plugin module
- ltservlet
- nameServletName
- codeServletClass
- ltparam namerequestParam1 valueval3.........
..
14Introduction to Networking Http Server -
SSI/Servlet
- HTTP Server SSI Servlets
- Used in URL as http//servernameport/base/Servle
tName - Ability to use given cookie variable to identify
client and use for state information - Can hold object for not only static variables,
but methods also - Problem with SSI Servlets processing costs..
- When getting past static pages, then the cost of
invoking a new process becomes important - CGI requires fork/exec for every iterative
call - SSI requires heavier httpd daemon to process
calls - Servlet ability to invoke thread and hold till
timeout
15Introduction to Networking Http Server - Servlet
- HTTP Server Pure Servlets
- If servlet container is already in a JVM, no
need to exec - Apache (Jakarta) Tomcat
- Example of a pure java servlet container
- Can be run stand alone, behind other http
server, and can talk to other tomcat servers (in
version 5.0 and greater) to pass state
information across for true load balancing - Take advantage of Java architecture to implement
java objects either through dynamic object method
invocation or through beans - PORTABLE
- Can take deployment archive (war file) and lay
on any pure java web container
16Introduction to Networking Example Server