<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FTiny_Application%2FWeb_Server</id>
		<title>Java/Tiny Application/Web Server - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FTiny_Application%2FWeb_Server"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Tiny_Application/Web_Server&amp;action=history"/>
		<updated>2026-04-15T07:04:21Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/Tiny_Application/Web_Server&amp;diff=8837&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Tiny_Application/Web_Server&amp;diff=8837&amp;oldid=prev"/>
				<updated>2010-06-01T07:19:41Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 07:19, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/Tiny_Application/Web_Server&amp;diff=8836&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Tiny_Application/Web_Server&amp;diff=8836&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:47Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Http Server ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import java.io.BufferedReader;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.InputStreamReader;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
import java.net.ServerSocket;&lt;br /&gt;
import java.net.Socket;&lt;br /&gt;
import java.util.StringTokenizer;&lt;br /&gt;
public class httpServer {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    int port;&lt;br /&gt;
    ServerSocket server_socket;&lt;br /&gt;
    try {&lt;br /&gt;
      port = Integer.parseInt(args[0]);&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      port = 1500;&lt;br /&gt;
    }&lt;br /&gt;
    try {&lt;br /&gt;
      server_socket = new ServerSocket(port);&lt;br /&gt;
      System.out.println(&amp;quot;httpServer running on port &amp;quot;&lt;br /&gt;
          + server_socket.getLocalPort());&lt;br /&gt;
      // server infinite loop&lt;br /&gt;
      while (true) {&lt;br /&gt;
        Socket socket = server_socket.accept();&lt;br /&gt;
        System.out.println(&amp;quot;New connection accepted &amp;quot;&lt;br /&gt;
            + socket.getInetAddress() + &amp;quot;:&amp;quot; + socket.getPort());&lt;br /&gt;
        // Construct handler to process the HTTP request message.&lt;br /&gt;
        try {&lt;br /&gt;
          httpRequestHandler request = new httpRequestHandler(socket);&lt;br /&gt;
          // Create a new thread to process the request.&lt;br /&gt;
          Thread thread = new Thread(request);&lt;br /&gt;
          // Start the thread.&lt;br /&gt;
          thread.start();&lt;br /&gt;
        } catch (Exception e) {&lt;br /&gt;
          System.out.println(e);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      System.out.println(e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class httpRequestHandler implements Runnable {&lt;br /&gt;
  final static String CRLF = &amp;quot;\r\n&amp;quot;;&lt;br /&gt;
  Socket socket;&lt;br /&gt;
  InputStream input;&lt;br /&gt;
  OutputStream output;&lt;br /&gt;
  BufferedReader br;&lt;br /&gt;
  public httpRequestHandler(Socket socket) throws Exception {&lt;br /&gt;
    this.socket = socket;&lt;br /&gt;
    this.input = socket.getInputStream();&lt;br /&gt;
    this.output = socket.getOutputStream();&lt;br /&gt;
    this.br = new BufferedReader(new InputStreamReader(socket&lt;br /&gt;
        .getInputStream()));&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      processRequest();&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      System.out.println(e);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  private void processRequest() throws Exception {&lt;br /&gt;
    while (true) {&lt;br /&gt;
      String headerLine = br.readLine();&lt;br /&gt;
      System.out.println(headerLine);&lt;br /&gt;
      if (headerLine.equals(CRLF) || headerLine.equals(&amp;quot;&amp;quot;))&lt;br /&gt;
        break;&lt;br /&gt;
      StringTokenizer s = new StringTokenizer(headerLine);&lt;br /&gt;
      String temp = s.nextToken();&lt;br /&gt;
      if (temp.equals(&amp;quot;GET&amp;quot;)) {&lt;br /&gt;
        String fileName = s.nextToken();&lt;br /&gt;
        fileName = &amp;quot;.&amp;quot; + fileName;&lt;br /&gt;
        FileInputStream fis = null;&lt;br /&gt;
        boolean fileExists = true;&lt;br /&gt;
        try {&lt;br /&gt;
          fis = new FileInputStream(fileName);&lt;br /&gt;
        } catch (FileNotFoundException e) {&lt;br /&gt;
          fileExists = false;&lt;br /&gt;
        }&lt;br /&gt;
        String serverLine = &amp;quot;Server: Simple Java Http Server&amp;quot;;&lt;br /&gt;
        String statusLine = null;&lt;br /&gt;
        String contentTypeLine = null;&lt;br /&gt;
        String entityBody = null;&lt;br /&gt;
        String contentLengthLine = &amp;quot;error&amp;quot;;&lt;br /&gt;
        if (fileExists) {&lt;br /&gt;
          statusLine = &amp;quot;HTTP/1.0 200 OK&amp;quot; + CRLF;&lt;br /&gt;
          contentTypeLine = &amp;quot;Content-type: &amp;quot; + contentType(fileName)&lt;br /&gt;
              + CRLF;&lt;br /&gt;
          contentLengthLine = &amp;quot;Content-Length: &amp;quot;&lt;br /&gt;
              + (new Integer(fis.available())).toString() + CRLF;&lt;br /&gt;
        } else {&lt;br /&gt;
          statusLine = &amp;quot;HTTP/1.0 404 Not Found&amp;quot; + CRLF;&lt;br /&gt;
          contentTypeLine = &amp;quot;text/html&amp;quot;;&lt;br /&gt;
          entityBody = &amp;quot;&amp;lt;HTML&amp;gt;&amp;quot;&lt;br /&gt;
              + &amp;quot;&amp;lt;HEAD&amp;gt;&amp;lt;TITLE&amp;gt;404 Not Found&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;quot;&lt;br /&gt;
              + &amp;quot;&amp;lt;BODY&amp;gt;404 Not Found&amp;quot;&lt;br /&gt;
              + &amp;quot;&amp;lt;br&amp;gt;usage:http://yourHostName:port/&amp;quot;&lt;br /&gt;
              + &amp;quot;fileName.html&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
        // Send the status line.&lt;br /&gt;
        output.write(statusLine.getBytes());&lt;br /&gt;
        System.out.println(statusLine);&lt;br /&gt;
        // Send the server line.&lt;br /&gt;
        output.write(serverLine.getBytes());&lt;br /&gt;
        System.out.println(serverLine);&lt;br /&gt;
        // Send the content type line.&lt;br /&gt;
        output.write(contentTypeLine.getBytes());&lt;br /&gt;
        System.out.println(contentTypeLine);&lt;br /&gt;
        // Send the Content-Length&lt;br /&gt;
        output.write(contentLengthLine.getBytes());&lt;br /&gt;
        System.out.println(contentLengthLine);&lt;br /&gt;
        // Send a blank line to indicate the end of the header lines.&lt;br /&gt;
        output.write(CRLF.getBytes());&lt;br /&gt;
        System.out.println(CRLF);&lt;br /&gt;
        // Send the entity body.&lt;br /&gt;
        if (fileExists) {&lt;br /&gt;
          sendBytes(fis, output);&lt;br /&gt;
          fis.close();&lt;br /&gt;
        } else {&lt;br /&gt;
          output.write(entityBody.getBytes());&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    try {&lt;br /&gt;
      output.close();&lt;br /&gt;
      br.close();&lt;br /&gt;
      socket.close();&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  private static void sendBytes(FileInputStream fis, OutputStream os)&lt;br /&gt;
      throws Exception {&lt;br /&gt;
    byte[] buffer = new byte[1024];&lt;br /&gt;
    int bytes = 0;&lt;br /&gt;
    while ((bytes = fis.read(buffer)) != -1) {&lt;br /&gt;
      os.write(buffer, 0, bytes);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  private static String contentType(String fileName) {&lt;br /&gt;
    if (fileName.endsWith(&amp;quot;.htm&amp;quot;) || fileName.endsWith(&amp;quot;.html&amp;quot;)&lt;br /&gt;
        || fileName.endsWith(&amp;quot;.txt&amp;quot;)) {&lt;br /&gt;
      return &amp;quot;text/html&amp;quot;;&lt;br /&gt;
    } else if (fileName.endsWith(&amp;quot;.jpg&amp;quot;) || fileName.endsWith(&amp;quot;.jpeg&amp;quot;)) {&lt;br /&gt;
      return &amp;quot;image/jpeg&amp;quot;;&lt;br /&gt;
    } else if (fileName.endsWith(&amp;quot;.gif&amp;quot;)) {&lt;br /&gt;
      return &amp;quot;image/gif&amp;quot;;&lt;br /&gt;
    } else {&lt;br /&gt;
      return &amp;quot;application/octet-stream&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>