Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 09:04 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
A Simple Web Server
This tip shows the implementation of a simple web server in Java.

Code:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class WebServer { /** * WebServer constructor. */ protected void start() { ServerSocket s; System.out.println("Webserver starting up on port 80"); System.out.println("(press ctrl-c to exit)"); try { // create the main server socket s = new ServerSocket(80); } catch (Exception e) { System.out.println("Error: " + e); return; } System.out.println("Waiting for connection"); for (;;) { try { // wait for a connection Socket remote = s.accept(); // remote is now the connected socket System.out.println("Connection, sending data."); BufferedReader in = new BufferedReader(new InputStreamReader( remote.getInputStream())); PrintWriter out = new PrintWriter(remote.getOutputStream()); // read the data sent. We basically ignore it, // stop reading once a blank line is hit. This // blank line signals the end of the client HTTP // headers. String str = "."; while (!str.equals("")) str = in.readLine(); // Send the response // Send the headers out.println("HTTP/1.0 200 OK"); out.println("Content-Type: text/html"); out.println("Server: Bot"); // this blank line signals the end of the headers out.println(""); // Send the HTML page out.println("<H1>Welcome to the Ultra Mini-WebServer</H2>"); out.flush(); remote.close(); } catch (Exception e) { System.out.println("Error: " + e); } } } /** * Start the application. * * @param args * Command line parameters are not used. */ public static void main(String args[]) { WebServer ws = new WebServer(); ws.start(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web-App server connection - How to hit the particular app server from the web server maruthi_s Enterprise JavaBeans 2 07-15-2008 07:11 PM
A simple multithreaded server Java Tip java.net 0 04-07-2008 09:15 PM
One server to another server redirection chaudhuri_abhi Java Servlet 1 02-11-2008 08:05 PM
[B]Simple Client connected to server but not exchanging messages[/B] JavaEmpires Networking 3 01-07-2008 08:01 AM
Simple example Client Server Application ferosh Networking 1 04-01-2007 11:36 AM


All times are GMT +3. The time now is 12:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org