Return to Table Of Contents in Java

Painting QR Code in Java Return to Table Of Contents
Return to Table Of Contents
Encode QR-Code In Java
Using Barcode creation for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications.
Copyright Keith W Ross and James F, Kurose 1996-2000
Make Bar Code In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
file:///D|/Downloads/Livros/computa o/Computer%20Netn%20Approach%20Featuring%20the%20Internet/udpDevhtml (8 of 8)20/11/2004 15:52:02
Decode Bar Code In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
Keith\book\applications\webserver
Quick Response Code Creation In Visual C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Quick Response Code image in .NET applications.
28 Building a Simple Web Server
Draw QR Code ISO/IEC18004 In VS .NET
Using Barcode printer for ASP.NET Control to generate, create QR image in ASP.NET applications.
Now that we have studied HTTP in some detail and have learned how to write client-server applications in Java, let us combine this new-found knowledge and build a simple Web server in Java We will see that the task is remarkably easy Our goal is to build a server that does the following:
Generate QR Code JIS X 0510 In Visual Studio .NET
Using Barcode printer for .NET Control to generate, create Quick Response Code image in VS .NET applications.
q q q q q
QR-Code Drawer In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Quick Response Code image in .NET applications.
Handles only one HTTP request Accepts and parses the HTTP request Gets the requested file from the server's file system Creates an HTTP response message consisting of the requested file preceded by header lines Sends the response directly to the client
Generating Data Matrix In Java
Using Barcode generation for Java Control to generate, create DataMatrix image in Java applications.
Let's try to make the code as simple as possible in order to shed insight on the networking concerns The code that we present will be far from bullet proof! For example, let's not worry about handling exceptions And let's assume that the client requests an object that is in server's file system
Making Bar Code In Java
Using Barcode generator for Java Control to generate, create barcode image in Java applications.
WebServerjava
Print Barcode In Java
Using Barcode encoder for Java Control to generate, create barcode image in Java applications.
Here is the code for a simple Web server:
Encoding Code39 In Java
Using Barcode maker for Java Control to generate, create Code39 image in Java applications.
import javaio*; import javanet*; import javautil*; class WebServer{ public static void main(String argv[]) throws Exception String requestMessageLine; String fileName; ServerSocket listenSocket = new ServerSocket(6789); Socket connectionSocket = listenSocketaccept(); BufferedReader inFromClient = {
Encoding Bar Code In Java
Using Barcode maker for Java Control to generate, create bar code image in Java applications.
file:///D|/Downloads/Livros/computa o/Computer%20Net20Approach%20Featuring%20the%20Internet/webserverhtm (1 of 6)20/11/2004 15:52:03
Leitcode Generator In Java
Using Barcode creation for Java Control to generate, create Leitcode image in Java applications.
Keith\book\applications\webserver
DataMatrix Creation In Visual Basic .NET
Using Barcode creation for VS .NET Control to generate, create Data Matrix ECC200 image in .NET applications.
new BufferedReader(new InputStreamReader(connectionSocket getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocketgetOutputStream()); requestMessageLine = inFromClientreadLine(); StringTokenizer tokenizedLine = new StringTokenizer(requestMessageLine); if (tokenizedLinenextToken()equals("GET")){ fileName = tokenizedLinenextToken(); if (fileNamestartsWith("/") == true ) fileName = fileNamesubstring(1); File file = new File(fileName); int numOfBytes = (int) filelength(); FileInputStream inFile = new FileInputStream (fileName);
ECC200 Generator In VS .NET
Using Barcode creation for .NET Control to generate, create Data Matrix ECC200 image in .NET framework applications.
byte[] fileInBytes = new byte[numOfBytes]; inFileread(fileInBytes); outToClientwriteBytes("HTTP/10 200 Document Follows\r\n"); if (fileNameendsWith("jpg")) outToClientwriteBytes("Content-Type: image/jpeg\r \n"); if (fileNameendsWith("gif")) outToClientwriteBytes("Content-Type: image/gif\r \n"); outToClientwriteBytes("Content-Length: " + numOfBytes + "\r \n"); outToClientwriteBytes("\r\n"); outToClientwrite(fileInBytes, 0, numOfBytes);
Printing Code39 In Visual Basic .NET
Using Barcode drawer for VS .NET Control to generate, create Code39 image in Visual Studio .NET applications.
file:///D|/Downloads/Livros/computa o/Computer%20Net20Approach%20Featuring%20the%20Internet/webserverhtm (2 of 6)20/11/2004 15:52:03
Barcode Drawer In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create bar code image in VS .NET applications.
Keith\book\applications\webserver
Scanning Code 128 Code Set A In VS .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
connectionSocketclose(); } else Systemoutprintln("Bad Request Message"); } }
EAN-13 Creator In Visual Studio .NET
Using Barcode encoder for VS .NET Control to generate, create European Article Number 13 image in VS .NET applications.
Let us now take a look at the code The first half the program is almost identical to TCPServerjava As with TCPServerjava, we import the javaio and javanet packages In addition to these two packages we also import the javautil package, which contains the StringTokenizer class, which is used for parsing HTTP request messages Looking now at the lines within the class WebServer, we define two string objects: String requestMessageLine; String fileName; The object requestMessageLine is a string that will contain the first line in the HTTP request message The object fileName is a string that will contain the file name of the requested file The next set of commands is identical to the corresponding set of commands in TCPServerjava ServerSocket listenSocket = new ServerSocket(6789); Socket connectionSocket = listenSocketaccept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocketgetOutputStream()); Two socket-like objects are created The first of these objects is listenSocket, which is of type ServerSocket The object listenSocket is created by the server program before receiving a request for a TCP connection from a client It listens at port 6789, and waits for a request from some client to establish a TCP connection When a request for a connection arrives, the accept() method of listenSocket creates a new object, connectionSocket, of type Socket Next two streams are created: the BufferedReader inFromClient and the DataOutputStream outToClient The HTTP request message comes from the network, through connectionSocket and into inFromClient; the HTTP response message goes into outToClient, through connectionSocket and into the network The remaining
Bar Code Generator In C#
Using Barcode generator for VS .NET Control to generate, create barcode image in VS .NET applications.
file:///D|/Downloads/Livros/computa o/Computer%20Net20Approach%20Featuring%20the%20Internet/webserverhtm (3 of 6)20/11/2004 15:52:03
Keith\book\applications\webserver
portion of the code differs significantly from TCPServerjava requestMessageLine = inFromClientreadLine(); The above command reads the first line of the HTTP request message This line is supposed to be of the form: GET file_name HTTP/10 Our server must now parse the line to extract the filename StringTokenizer tokenizedLine = new StringTokenizer (requestMessageLine); if (tokenizedLinenextToken()equals("GET")){ fileName = tokenizedLinenextToken(); if (fileNamestartsWith("/") == true ) fileName = fileNamesubstring( 1 ); The above commands parse the first line of the request message to obtain the requested filename The object tokenizedLine can be thought of as the original request line with each of the "words" GET, file_name and HTTP/10 placed in a separate place holder called a token The server knows from the HTTP RFC that the file name for the requested file is contained in the token that follows the token containing "GET" This file name is put in a string called fileName The purpose of the last if statement in the above code is to remove the backslash that may precede the filename FileInputStream inFile = new FileInputStream (fileName);
The above command attaches a stream, inFile, to the file fileName byte[] fileInBytes = new byte[numOfBytes]; inFileread(fileInBytes); The above commands determine the size of the file and construct an array of bytes of that size The name of the array is fileInBytes The last command reads from the stream inFile to the byte array fileInBytes The program must convert to bytes because the output stream outToClient may only be fed with bytes
file:///D|/Downloads/Livros/computa o/Computer%20Net20Approach%20Featuring%20the%20Internet/webserverhtm (4 of 6)20/11/2004 15:52:03