Java Send HTML Over Socket

Posted on 2008-11-03
Last Modified: 2013-11-24
On a J2ME application, I have a socket server that listens for web browsers to connect.
I'd like to be able to send a <html><b>Hi</b></html> page, but it keeps showing up as text.

I don't know how to send the headers so the web page treats the server socket like a regular web server.  Anyone know the lines that need to be printed, and how to print them?  I'm using (and not working):

String responseWithHeader = "\nContent-type: text/html\n\n" + response;

Question by:ecuguru
    LVL 13

    Expert Comment

    Are you seeing the "Hi" text, or the "<html><b>Hi</b></html>" text? In the first case, you have already succeeded.

    Author Comment

    No, I'm seeing
    Content-type: text/html

    As text in the browser.. The Q is, how do I turn a socket that speaks .txt to browsers into one that sends html.

    Accepted Solution

                            String statusLine = "HTTP/1.0 200 OK" + CRLF;
                            String contentTypeLine = "Content-type: " + "text/html" + CRLF;
                            os.write("etc etc etc data here".getBytes());


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now