Posts

Showing posts from August, 2016

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. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetSocketAddress; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class Test {     public static void main(String[] args) throws Exception {         HttpServer server = HttpServer.create(new InetSocketAddress(8002), 0);         server.createContext("/test", new MyHandler());         server.setExecutor(null); // creates a default executor         server.start();              }

Java Thread

Image
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                         ...

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: class TestClass {     public static void main(String args[] ) throws Exception {      int arr[] = {1 ,1 3};      subArray();     }     public static void subArray(int arr[], int n)

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>