473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Java Server programming Problem

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
5 3851
<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
"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
<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: StealthMonkey | last post by:
Let me prefix this by saying that I know next to nothing about Java (so please try to keep explainations simple). I use PHP for my server-side web programming. Here is my dilemma: I need a...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
5
by: TZESENG | last post by:
DECEMBER 13, 2005 . Editions: N. America | Europe | Asia | Edition Preference News Analysis By Steve Hamm Source: http://www.businessweek.com/technology/content/dec2005/tc20051213_042973.htm...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
63
by: s0suk3 | last post by:
I've been programming Python for a couple of years now. Now I'm looking to move on to either C++ or Java, but I'm not sure which. Which one do you think will be a better transition for a Python...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.