Use of split function

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package WordSplit;

import java.util.Arrays;

/**
 *
 * @author SHASHI
 */
public class WordsSplit {
   
   
    public static void main(String[] args) {
       
        String sentences=  "Generates HTML. Building HTML from a helper class is\n" +
"probably not really worth it for real projects (JSP is\n" +
"better), but we haven’t covered logic in servlets yet. But\n" +
"the general principle still holds: if you are doing the same\n" +
"thing in several servlets, move the code into shared class";
        String[] words = sentences.split("[\\n .(),]");      // as many punctuation we put we receive as many seperation point on the sentences.
       
        System.out.println(" In string form"+Arrays.toString(words)  );
       
        for (int i = 0; i < words.length; i++) {
            System.out.println(" "+words[i]);
           
           
        }
       
       
       
    }
   
   
}


#########################OUTPUT##############################################

run:
 In string form[Generates, HTML, , Building, HTML, from, a, helper, class, is, probably, not, really, worth, it, for, real, projects, , JSP, is, better, , , but, we, haven’t, covered, logic, in, servlets, yet, , But, the, general, principle, still, holds:, if, you, are, doing, the, same, thing, in, several, servlets, , move, the, code, into, shared, class]
 Generates
 HTML

 Building
 HTML
 from
 a
 helper
 class
 is
 probably
 not
 really
 worth
 it
 for
 real
 projects

 JSP
 is
 better


 but
 we
 haven’t
 covered
 logic
 in
 servlets
 yet

 But
 the
 general
 principle
 still
 holds:
 if
 you
 are
 doing
 the
 same
 thing
 in
 several
 servlets

 move
 the
 code
 into
 shared
 class
BUILD SUCCESSFUL (total time: 0 seconds)















Use of Static keyword


package dice;
import java.util.Random;

public class Dice {
static int no_of_sides = 6;
int face_value = 0;
int roll() {
Random r = new Random();
this.face_value = r.nextInt(no_of_sides) + 1;
return this.face_value;
}
static class FirstDice {
static String color = "red";
}
static class SecondDice {
static String color = "black";
}
public static void main(String[] args) {
Dice d = new Dice();
System.out.println("The first die has color " + FirstDice.color + " and has face value after rolling as " + d.roll());
System.out.println("The second die has color " + SecondDice.color + " and has face value after rolling as " + d.roll());
}
}


/**    comments
Here, we have used the static keyword to call the variable(colour ) directly in main method.
We made the inner class(firstDice) static and then the variable colour static to display the colour of  first dice in main method.
Also, in the main method which is static, any variable which needs to be accessed should be static and for the non-static, the variable, function or method, which needs to be  accessed should be accessed by creating an instance of the class, which we have done for calling the function roll in the main method.

**/

About HTTP/2

About HTTP/2 HTTP/2 is an advance version of HTTP/1.x . It has lots of advantages over its previous versions.. It supports multiplexing, binary message framing,server push, header compression and decompression, prioritization, re-prioritization of request, flow control, congestion control and many more... Initially, between client and server, a connection is established by sending a header frame which contains "headers", "h2"or "h2c". Where h2 refers to connection security(mainly of TLS/SSL) i.e, a negotiation between TLS-ALPN extension. In this negotiation, a list of protocol is sent from client to server .Now, server select the protocol and send the selected protocol certificate, server key and certification request.
From the client side it accept these things and send client key, confirmation of certificate verification. After that the handshake is complete and both endpoint negotiate to exchange data on verified protocol. In "h2c" it checks that the client request is cleartext or not. After header frame, the settings franme, containing 64 bit octet is sent from client . Finally, a connection preface frame is sent from one endpoint and client start sending headers frames before waiting from other endpoint the connection preface to come . This way connection is established in HTTP/2.

Two way interaction of client server in java

// here from both ends the message will be sent and read


//client code

public class TwoWayInteractionClient {

   
    public static void main(String[] args) throws IOException {
       
        Socket obj = new Socket("localhost",8844);
        DataOutputStream dout = new DataOutputStream(obj.getOutputStream());
        dout.writeUTF("This message is from client side");
        dout.flush();
       
       
        //ServerSocket rec = new ServerSocket(8134);
        DataInputStream din = new DataInputStream(obj.getInputStream());
        String newdata = (String)din.readUTF();
        System.out.println(" the new msg is " +newdata);
       
    }
   
   
}

output
the new msg is This msg is send from server


//server code

public class TwoWayInteractionServer {
     public static void main(String[] args) throws IOException {
       
    ServerSocket obj1 = new ServerSocket(8844);
        Socket obj = obj1.accept();
        DataInputStream din = new DataInputStream(obj.getInputStream());
        String str = (String)din.readUTF();
        System.out.println("The message from client is  " +str);
       
       // while two way interaction no need to create new port
        DataOutputStream dout =  new DataOutputStream(obj.getOutputStream());
        dout.writeUTF("This msg is send from server");
        dout.flush();
     }
   
   
}

output
The message from client is  This message is from client side











Client server callback of message

//code for client

public class MyClient {
 
 
    public static void main(String[] args) throws IOException {
     
        Socket obj = new Socket("localhost",8040);
        DataOutputStream dout = new DataOutputStream(obj.getOutputStream());
        dout.writeUTF("This message is from client side");
        dout.flush();
     
     
    }
}





// code for server

public class MyServer {
    public static void main(String[] args) throws IOException {
     
        ServerSocket obj1 = new ServerSocket(8040);
        Socket obj = obj1.accept();
        DataInputStream din = new DataInputStream(obj.getInputStream());
        String str = (String)din.readUTF();
        System.out.println("The message from client is  " +str);

     
    }
 
 
 
}

output (in server console)
This message is from client side



//here first we have to run server code then client code ..
//the message sent by the client will be readed in server concole
// the message are delivered through port no which is listened at sockets