Tuesday, September 15, 2009

call servlet from java, call the URL using java stand alone program

1. MyJsp.jsp

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>THIS IS BINOD JSP PAGE<%
System.out.println("THIS IS OUTPUT FROM JSP PAGE");
%></body></html>

2. Stand alone Java Code
callURL.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class CallURL {
public static void main(String[] args) {
callURL();
}
public static void callURL(){
String urlName = "http://localhost:8080/TestServlet/MyJsp.jsp";
URL url;
try {
url = new URL(urlName);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println("Output from Server :: "+line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Output from Client
Output from Server :: <html>Output from Server :: <head>Output from Server :: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">Output from Server :: <title>Insert title here</title>Output from Server :: </head>Output from Server :: <body>Output from Server :: THIS IS BINOD JSP PAGEOutput from Server :: Output from Server :: </body>Output from Server :: </html>

Output from Server Console:
21:46:11,453 INFO [STDOUT] THIS IS OUTPUT FROM JSP PAGE

Friday, September 11, 2009

Overriding the equals and hashCode methods

Override the equals method when you want to specify the rules of logical equality of objects.
If these methods are not override then :
Person p1 = new Person("Binod",26);
Person p2 = new Person("Binod",26);
p1.equals(p2) -> Will return FALSE. But logically both object are same. Because equals method in Object class only check the references of object NOT containts.

So make the logical equals, Use override equals methods in Person class.

public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if (!(obj instanceof Person)) {
return false;
}
Person person = (Person)obj;
return age == person.getAge() && name.equals(person.getName());

}
After adding this method in Person class then
Person p1 = new Person("Binod",26);
Person p2 = new Person("Binod",26);
p1.equals(p2) -> Will return TRUE.

Why override hashcode() method.
When use the hash based collection in your code then only equals method will not work. For example. Now as above code p1 and p2 are logically equal objects. So,

Set set = new HashSet();
set.add(p1);
set.add(p2);
System.out.println("Size of Set :: "+set.size()); Output would be 2, but it should be 1, as both objects are logically same and Set keeps unique data. So, we have to override the hashCode() method.
In Person class, add one more method.


public int hashCode() {
int hashCode = 0;
final int multiplier = 23;
if (hashCode == 0) {
int code = 133;
code = multiplier * code + age;
code = multiplier * code + name.hashCode();
hashCode = code;
}
return hashCode;
}

after compilation check
Set set = new HashSet();
set.add(p1);
set.add(p2);
System.out.println("Size of Set :: "+set.size()); Output would be 1.

During add the p1 object into Set, it will call hashCode() method of p1, now adding p2 object then again it will call hashCode() method of p2. If both hashcode values are equals then it will call object equals() method to final check, if it return true then set will not add second p2 object. If equals() method does not override then it will add second p2 object. If hashcode values are not equal then set would not be call equals() method.

Tuesday, September 8, 2009

Java Data Structure, Java LinkList

public class Node {
protected Object obj;
protected Node node;
}


public class SingleLinkList {
Node linkList;
int count;
public SingleLinkList() {
linkList = null;
count = 0;
}
public void insert(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}else{
Node temp = new Node();
temp.obj = obj;
temp.node = null;
Node last = getLastNode();
last.node = temp;
}
}
public void insertFirst(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}
else{
Node temp = new Node();
temp.obj = obj;
temp.node = linkList;
linkList = temp;
}
}
public void removeFirst(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
System.out.println("Deleting from First :: "+linkList.obj.toString());
linkList = linkList.node;
}
}
public void remove(Object obj){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
if(linkList.obj == obj){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}else{
System.out.println("NOT FOUND");
}
}
else{
Node temp = new Node();
temp = linkList;
Node pre = temp;
while(temp.node!= null&&temp.obj!=obj){
pre = temp;
temp = temp.node;
}
if(temp.obj==obj){
System.out.println("Deleting data :: "+temp.obj.toString());
if(pre==temp){
linkList=temp.node;
}
else{
pre.node = temp.node;
}
}
else{
System.out.println("NOT FOUND");
}
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
// Get node before last node
Node temp = new Node();
temp = linkList;
while(temp.node.node!=null){
temp = temp.node;
}
System.out.println("Deleting data :: "+temp.node.obj.toString());
temp.node = null;
}
}
public Node getLastNode(){
Node temp = new Node();
temp = linkList;
while(temp.node!=null){
temp = temp.node;
}
return temp;
}
public void show(){
Node temp = new Node();
temp = linkList;
while(temp!=null){
System.out.println("Data :: "+temp.obj.toString());
temp = temp.node;
}
}
public static void main(String[] args) {
SingleLinkList singleLinkList = new SingleLinkList();
String str="Binod";
singleLinkList.insert(str);
String str1="MCA";
singleLinkList.insert(str1);
str1="SATYAM";
singleLinkList.insert(str1);
str1="Bangalore";
singleLinkList.insert(str1);
str1="India";
singleLinkList.insertFirst(str1);
str1="SATYAM2";
singleLinkList.insert(str1);
str1="USA";
singleLinkList.insertFirst(str1);
singleLinkList.show();
// singleLinkList.remove();
/*singleLinkList.remove();
singleLinkList.remove();*/
// singleLinkList.removeFirst();
// singleLinkList.show();
singleLinkList.remove("SATYAM2");
singleLinkList.show();
}
}

Java Structure, Java Doule LinkList

public class DoubleNode {
DoubleNode pre;
Object obj;
DoubleNode post;
}


public class DoubleLinkList {
DoubleNode linkList;
public DoubleLinkList(){
linkList = null;
}
public void insert(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr=curr.post;
}
temp.pre = curr;
curr.post=temp;
}
}
public void insertFirst(Object obj){
DoubleNode temp = new DoubleNode();
temp.pre = null;
temp.obj = obj;
temp.post = null;
if(linkList == null){
linkList = temp;
}
else{
temp.post = linkList;
linkList = temp;
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else{
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr.post!=null){
curr = curr.post;
}
System.out.println("Deleting Element :: "+curr.obj.toString());
if(curr == linkList){linkList = null;}
else{curr.pre.post = null;}
}
}
public void show(){
DoubleNode curr = new DoubleNode();
curr = linkList;
while(curr!=null){
System.out.println("Data :: "+curr.obj.toString());
curr = curr.post;
}
}
public static void main(String[] args) {
DoubleLinkList doubleLinkList = new DoubleLinkList();
doubleLinkList.insert("Binod");
doubleLinkList.insert("Suman");
doubleLinkList.insert("IGNOU");
doubleLinkList.insertFirst("India");
doubleLinkList.insert("SATYAM");
doubleLinkList.show();
doubleLinkList.remove();
doubleLinkList.remove();
doubleLinkList.show();
}
}