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.wri teBytes("HTTP/1.0 200 Document Follows\r\n").
"outToClient.wr iteBytes" 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 requestMessageL ine;
String fileName;
ServerSocket listenSocket = new ServerSocket(80 );
while (true) {
Socket connectionSocke t = listenSocket.ac cept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamRead er(
connectionSocke t.getInputStrea m()));
DataOutputStrea m outToClient = new DataOutputStrea m(
connectionSocke t.getOutputStre am());
requestMessageL ine = inFromClient.re adLine();
StringTokenizer tokenizedLine = new
StringTokenizer (requestMessage Line);
if (tokenizedLine. nextToken().equ als("GET") ) {
fileName = tokenizedLine.n extToken();
if (fileName.start sWith("/")) {
fileName = fileName.substr ing(1);
}
File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream (fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fil eInBytes);
outToClient.wri teBytes("HTTP/1.0 200 Document Follows\r\n");
outToClient.wri teBytes("Conten t-length" + numOfBytes + "\r\n");
outToClient.wri teBytes("\r\n") ;
outToClient.wri te(fileInBytes, 0, numOfBytes);
connectionSocke t.close();
}
else {
System.out.prin tln("Bad request message");
}
}
}
}
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.wri teBytes("HTTP/1.0 200 Document Follows\r\n").
"outToClient.wr iteBytes" 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 requestMessageL ine;
String fileName;
ServerSocket listenSocket = new ServerSocket(80 );
while (true) {
Socket connectionSocke t = listenSocket.ac cept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamRead er(
connectionSocke t.getInputStrea m()));
DataOutputStrea m outToClient = new DataOutputStrea m(
connectionSocke t.getOutputStre am());
requestMessageL ine = inFromClient.re adLine();
StringTokenizer tokenizedLine = new
StringTokenizer (requestMessage Line);
if (tokenizedLine. nextToken().equ als("GET") ) {
fileName = tokenizedLine.n extToken();
if (fileName.start sWith("/")) {
fileName = fileName.substr ing(1);
}
File file = new File(fileName);
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream (fileName);
byte[] fileInBytes = new byte[numOfBytes];
inFile.read(fil eInBytes);
outToClient.wri teBytes("HTTP/1.0 200 Document Follows\r\n");
outToClient.wri teBytes("Conten t-length" + numOfBytes + "\r\n");
outToClient.wri teBytes("\r\n") ;
outToClient.wri te(fileInBytes, 0, numOfBytes);
connectionSocke t.close();
}
else {
System.out.prin tln("Bad request message");
}
}
}
}
Comment