Java Server programming Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kaiwing18@hotmail.com

    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.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");
    }
    }
    }
    }
  • Ryan Stewart

    #2
    Re: Java Server programming Problem

    <kaiwing18@hotm ail.com> wrote in message
    news:d82f3f5c.0 402130340.1f28f [email protected] gle.com...[color=blue]
    > 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");
    > }
    > }
    > }
    > }[/color]

    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?


    Comment

    • kaiwing18@hotmail.com

      #3
      Re: Java Server programming Problem

      "Ryan Stewart" <zzanNOtozz@gSP AMo.com> wrote in message news:<-sWdnTx_tq6kWbHd [email protected] t>...[color=blue]
      > <kaiwing18@hotm ail.com> wrote in message
      > news:d82f3f5c.0 402130340.1f28f [email protected] gle.com...[color=green]
      > > 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");
      > > }
      > > }[/color]
      > }[color=green]
      > > }[/color]
      >
      > 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?[/color]

      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.wr ite(<html><body >file not found</body></html>);" in the
      my coding.It doesn't work.What should i do?

      Best regards

      Ricky

      Comment

      • Tony Morris

        #4
        Re: Java Server programming Problem

        <kaiwing18@hotm ail.com> wrote in message
        news:d82f3f5c.0 402130340.1f28f [email protected] gle.com...[color=blue]
        > 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");
        > }
        > }
        > }
        > }[/color]

        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)


        Comment

        • hiwa

          #5
          Re: Java Server programming Problem

          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.

          Comment

          • chris

            #6
            Re: Java Server programming Problem

            kaiwing18@hotma il.com wrote:
            [color=blue]
            > "Ryan Stewart" <zzanNOtozz@gSP AMo.com> wrote in message
            > news:<-sWdnTx_tq6kWbHd [email protected] t>...[color=green]
            >> <kaiwing18@hotm ail.com> wrote in message
            >> news:d82f3f5c.0 402130340.1f28f [email protected] gle.com...[color=darkred]
            >> > 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");
            >> > }
            >> > }[/color]
            >> }[color=darkred]
            >> > }[/color]
            >>
            >> 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?[/color]
            >
            > 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.wr ite(<html><body >file not found</body></html>);" in the
            > my coding.It doesn't work.What should i do?
            >
            > Best regards
            >
            > Ricky[/color]

            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.wri te("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 [email protected] net.be
            /k/ Embedded Java Solutions

            Comment

            Working...