2016-08-18

Java program to handle http request

Handle http request from web browser.  Creating own server like tomcat, No need to deploy the code .Program will run with out any server.
Getting url parameter and sending response.




  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStream;
  5. import java.net.InetSocketAddress;

  6. import com.sun.net.httpserver.HttpExchange;
  7. import com.sun.net.httpserver.HttpHandler;
  8. import com.sun.net.httpserver.HttpServer;

  9. public class Test {

  10.     public static void main(String[] args) throws Exception {
  11.         HttpServer server = HttpServer.create(new InetSocketAddress(8002), 0);
  12.         server.createContext("/test", new MyHandler());
  13.         server.setExecutor(null); // creates a default executor
  14.         server.start();
  15.         
  16.     }

2016-08-08

Java Thread

Understanding Threads

A thread is a single sequence of executable code within a larger program. All the programs shown so far in this book have used just one thread — the main thread that starts automatically when you run the program. However, Java lets you create programs that start additional threads to perform specific tasks.

Understanding the Thread class

The Thread class lets you create an object that can be run as a thread in a multi-threaded Java application. The Thread class has quite a few constructors and methods, but for most applications you only need to use the ones listed in Table.

 Constructors and Methods of the Thread Class
Constructor                        Explanation
Thread()                          The basic Thread constructor without
                                                 parameters. This constructor simply creates
                                                 an instance of the Thread class.

Thread(String name)         Creates a Thread object and assigns
                                                the specified name to the thread.

Thread(Runnable target) A more advanced constructor that lets
                                                 you turn any object that implements an
                                                API interface called Runnable into a
                                                thread. You see how this constructor is
                                                used later in this chapter.

Thread(Runnable target, Creates a thread from any object that
                  String name)   implements Runnable and assigns the
                                                specified name to the thread.

static int activeCount()         Returns the number of active threads.

static int enumerate         Fills the specified array with a copy of
(Thread[] t)                         each active thread. The return value is
                                                the number of threads added to the array.

String getName()                Returns the name of the thread.

int getPriority()        Returns the thread’s priority.

void interrupt()        Interrupts this thread.

boolean isInterrupted()       Checks to see if the thread has been
                                                interrupted.

2016-08-06

Write a Programs to Generate Maximum non-empty subarray of an array

Given Array A of size N  find those non-empty subarrays  (sequence of consecutive elements)

Sample:
Array    [ 1 ,1 ,3 ]

Output:
1
1
3
1  1
1  3
1  1  3

Solution:

  1. class TestClass {
  2.     public static void main(String args[] ) throws Exception {
  3.      int arr[] = {1 ,1 3};
  4.      subArray();
  5.     }
  6.     public static void subArray(int arr[], int n)

2016-08-02

Java program to parse xml file

Similar question:

  • How to read XML file in Java
  • java dom parser
  • java sax parser
  • listing all the files in a directory in java
Solution:

Config.xml
 <note>

  <id>1</id>
  <description>Files</description>
  <path>C://x</path>
  
    <id>2</id>
  <description>Files</description>
  <path>C://y</path>
</note>