title: How to write on web server in java
body: <p>HI I am having problem in java related to write on the web server from the client. I am using following code please help me where I am wrong</p>
  
  <p>For Server class</p>
  
  <pre><code>import java.net.ServerSocket;
  import java.net.Socket;
  import java.io.InputStream;
  import java.util.Scanner;
  import java.io.InputStreamReader;
  import java.io.BufferedReader;
  
  public class Server
  {
      public static void main(String args[]){
          try{
              ServerSocket s = new ServerSocket(8080);
              System.out.println("ashish");
  
              Socket incoming = s.accept();
              System.out.println("the status of the socket="+incoming.isConnected());
              InputStream inStream = incoming.getInputStream();
  
              InputStreamReader isr=new InputStreamReader(inStream);
              BufferedReader br=new BufferedReader(isr);
              String str=null;
              while((str=br.readLine()).equals("exit")==false){
                  System.out.println(str);
              }
  
              System.out.println("System exit");
          }catch(Exception e){
              e.printStackTrace();
          }    
      }
  }
  </code></pre>
  
  <p>And for the client class which is actually applet have folowing code</p>
  
  <pre><code>import java.applet.Applet;
  import java.net.URL;
  import java.net.HttpURLConnection;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  
  public class testApplet extends Applet
  {
  
      public void init(){}
  
      public void start(){
          try{
              URL url = new URL("http://localhost:8080"); 
              HttpURLConnection httpCon = (HttpURLConnection)url.openConnection(); 
              httpCon.setDoOutput(true); 
              httpCon.setRequestMethod("PUT");
  
              PrintWriter out = new PrintWriter( httpCon.getOutputStream()); 
              out.print("Resource content\n");
              out.print("exit"); 
              out.close();
          }catch(Exception e){
              e.printStackTrace();
          } 
      }
  
      public void stop(){}
  
      public void destroy(){}
  }
  </code></pre>
  
  <p>Now when I run the server then the server display only the following </p>
  
  <pre><code>PUT / HTTP/1.1
  User-Agent: Mozilla/4.0 (Windows Vista 6.1) Java/1.6.0
  Host: localhost:8080
  Accept: text/html, image/gif. image/jpeg, *; q=.2, */*; q=.2
  Connection: keep-alive
  </code></pre>
  
  <p>But it did not write the content which I write by the out object of PrintWriter
  and next goes into wait state</p>
  
  <p>on the other hand the applet does not started just initiated. and when I close server program then applet gives following exception </p>
  
  <pre><code>java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at sun.net.NetworkClient.doConnect(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at testApplet.start(testApplet.java:19)
      at sun.applet.AppletPanel.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>Please tell what's wrong with me</p>