I am trying to write OutputStream or byte array directly to browser without using servlet.
Is there any Java API to do that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use a
ServerSocket(http://docs.oracle.com/javase/1.4.2/docs/api/java/net/ServerSocket.html) to listen on port 80.If a request comes in,
accept()it and use the resultingSocket‘sgetInputStream()andgetOutputStream()methods to grab stream objects to handle the data transfer with.Then just parse out the request coming in on the
InputStreamand send your reply (including HTTP headers!!!) through theOutputStream. Then, close the connection and you’re done.If you need to write a server that can actually handle many simultaneous requests without taking a performance hit, you should definitely check out Java’s NIO framework. Do not use one Thread per connection, if scalable performance is an issue.