By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

Java Server programming Problem

P: n/a
Hi,

I have a simple java server program, i want to ask how can i output a
simple HTML page of error message, without calling a html file. That
means i want to type HTML codes in the program like
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n").
"outToClient.writeBytes" only write the message in the command prompt
but not the browser.
Best regards

Ricky

import java.io.*;
import java.net.*;
import java.util.*;

public class SimpleWebServer {

public static void main(String[] args) throws Exception {

String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(80);
while (true) {
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(
connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());

requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine = new
StringTokenizer(requestMessageLine);

if (tokenizedLine.nextToken().equals("GET") ) {
fileName = tokenizedLine.nextToken();
if (fileName.startsWith("/")) {

fileName = fileName.substring(1);
}

File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream(fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
outToClient.writeBytes("Content-length" + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else {
System.out.println("Bad request message");
}
}
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<ka*******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
Hi,

I have a simple java server program, i want to ask how can i output a
simple HTML page of error message, without calling a html file. That
means i want to type HTML codes in the program like
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n").
"outToClient.writeBytes" only write the message in the command prompt
but not the browser.
Best regards

Ricky

import java.io.*;
import java.net.*;
import java.util.*;

public class SimpleWebServer {

public static void main(String[] args) throws Exception {

String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(80);
while (true) {
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(
connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());

requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine = new
StringTokenizer(requestMessageLine);

if (tokenizedLine.nextToken().equals("GET") ) {
fileName = tokenizedLine.nextToken();
if (fileName.startsWith("/")) {

fileName = fileName.substring(1);
}

File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream(fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
outToClient.writeBytes("Content-length" + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else {
System.out.println("Bad request message");
}
}
}
}


You would write it to the OutputStream the same way you wrote the header.
But maybe I'm not understanding you properly. What do you mean about writing
to the command prompt and not the browser?
Jul 17 '05 #2

P: n/a
"Ryan Stewart" <zz********@gSPAMo.com> wrote in message news:<-s********************@texas.net>...
<ka*******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
Hi,

I have a simple java server program, i want to ask how can i output a
simple HTML page of error message, without calling a html file. That
means i want to type HTML codes in the program like
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n").
"outToClient.writeBytes" only write the message in the command prompt
but not the browser.
Best regards

Ricky

import java.io.*;
import java.net.*;
import java.util.*;

public class SimpleWebServer {

public static void main(String[] args) throws Exception {

String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(80);
while (true) {
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(
connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());

requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine = new
StringTokenizer(requestMessageLine);

if (tokenizedLine.nextToken().equals("GET") ) {
fileName = tokenizedLine.nextToken();
if (fileName.startsWith("/")) {

fileName = fileName.substring(1);
}

File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream(fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
outToClient.writeBytes("Content-length" + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else {
System.out.println("Bad request message");
}
}

}
}


You would write it to the OutputStream the same way you wrote the header.
But maybe I'm not understanding you properly. What do you mean about writing
to the command prompt and not the browser?


Hi,

As my work also need to use commard prompt(DOS screen) to type the
message(eg GET /index.htm),so it should give back the header message
to the DOS screen.But i also want it will give back a HTML error
message when the file not found. If i write

"outToClient.write(<html><body>file not found</body></html>);" in the
my coding.It doesn't work.What should i do?

Best regards

Ricky
Jul 17 '05 #3

P: n/a
<ka*******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
Hi,

I have a simple java server program, i want to ask how can i output a
simple HTML page of error message, without calling a html file. That
means i want to type HTML codes in the program like
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n").
"outToClient.writeBytes" only write the message in the command prompt
but not the browser.
Best regards

Ricky

import java.io.*;
import java.net.*;
import java.util.*;

public class SimpleWebServer {

public static void main(String[] args) throws Exception {

String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(80);
while (true) {
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(
connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());

requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine = new
StringTokenizer(requestMessageLine);

if (tokenizedLine.nextToken().equals("GET") ) {
fileName = tokenizedLine.nextToken();
if (fileName.startsWith("/")) {

fileName = fileName.substring(1);
}

File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream(fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fileInBytes);
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
outToClient.writeBytes("Content-length" + numOfBytes + "\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else {
System.out.println("Bad request message");
}
}
}
}


Use (or write your own) a reverse proxy server that intercepts and relays
HTTP traffic, log the outgoing traffic to the standard output stream before
relaying it back to the client.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Jul 17 '05 #4

P: n/a
If you mean that is DOS screen on client system, then write client
application accordingly. If you want to monitor ClientOut on the
server's DOS console, write it to System.err and configure server log
output accordingly.
Jul 17 '05 #5

P: n/a
ka*******@hotmail.com wrote:
"Ryan Stewart" <zz********@gSPAMo.com> wrote in message
news:<-s********************@texas.net>...
<ka*******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
> Hi,
>
> I have a simple java server program, i want to ask how can i output a
> simple HTML page of error message, without calling a html file. That
> means i want to type HTML codes in the program like
> outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n").
> "outToClient.writeBytes" only write the message in the command prompt
> but not the browser.
> Best regards
>
> Ricky
>
> import java.io.*;
> import java.net.*;
> import java.util.*;
>
> public class SimpleWebServer {
>
> public static void main(String[] args) throws Exception {
>
> String requestMessageLine;
> String fileName;
> ServerSocket listenSocket = new ServerSocket(80);
> while (true) {
> Socket connectionSocket = listenSocket.accept();
> BufferedReader inFromClient = new BufferedReader(
> new InputStreamReader(
> connectionSocket.getInputStream()));
> DataOutputStream outToClient = new DataOutputStream(
> connectionSocket.getOutputStream());
>
> requestMessageLine = inFromClient.readLine();
> StringTokenizer tokenizedLine = new
> StringTokenizer(requestMessageLine);
>
> if (tokenizedLine.nextToken().equals("GET") ) {
> fileName = tokenizedLine.nextToken();
> if (fileName.startsWith("/")) {
>
> fileName = fileName.substring(1);
> }
>
> File file = new File(fileName);
> int numOfBytes = (int) file.length();
> FileInputStream inFile = new FileInputStream(fileName);
> byte[] fileInBytes = new byte[numOfBytes];
> inFile.read(fileInBytes);
> outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
> outToClient.writeBytes("Content-length" + numOfBytes + "\r\n");
> outToClient.writeBytes("\r\n");
> outToClient.write(fileInBytes, 0, numOfBytes);
> connectionSocket.close();
> }
> else {
> System.out.println("Bad request message");
> }
> }

}
> }


You would write it to the OutputStream the same way you wrote the header.
But maybe I'm not understanding you properly. What do you mean about
writing to the command prompt and not the browser?


Hi,

As my work also need to use commard prompt(DOS screen) to type the
message(eg GET /index.htm),so it should give back the header message
to the DOS screen.But i also want it will give back a HTML error
message when the file not found. If i write

"outToClient.write(<html><body>file not found</body></html>);" in the
my coding.It doesn't work.What should i do?

Best regards

Ricky


One last desperate attempt (although no soul on earth can understand what
you mean by "write the message in the command prompt but not the browser"):
outToClient.write("404 File not found\r\n\r\n");

Now (1) forget DOS, get rid of it, this is 2004.
(2) either learn some HTTP or use Tomcat.

--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.