Tuesday, April 30, 2013

Dynamically adding textbox and labels in jsp

1)ajax.jsp:


<%@page import="java.sql.*"%><html>
<head>
<script type="text/javascript">
function showData(value){ 

xmlHttp=GetXmlHttpObject()
var url="getdata.jsp";
url=url+"?name="+value;
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged() { if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var showdata = xmlHttp.responseText; 
    document.getElementById("lab").innerHTML="Address";
    document.getElementById("address").style.visibility="visible";
    document.getElementById("address").value= showdata;
        } }
function GetXmlHttpObject(){
var xmlHttp=null;
try {
  xmlHttp=new XMLHttpRequest();
 }
catch (e) {
 try  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee">
<br><br>
<pre>
<label>Name</label>     <input  type="text" name="name" id="name" onkeyup="showData(this.value);">
<label id="lab">       </label>  <input style="visibility:hidden" type="text" name="address" id="address">
</form>    </html>
 
 
--------------------------------------------------
 
 
2)getdata.jsp:

<%@ page import="java.sql.*" %> <%
String name = request.getParameter("name").toString();
System.out.println(name);
String data ="";
try{
           
Class.forName("com.mysql.jdbc.Driver");
           
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           
Statement st=con.createStatement();
           
ResultSet rs=st.executeQuery("select * from employee where name='"+name+"'");
while(rs.next())
{
data
=rs.getString("address");
}
out.println(data);
System.out.println(data);
}
catch (Exception e) {
System.out.println(e);
}
%>

How to create drop down list using JSP

how to display values from database into table using jsp


Code---->                                                             Link
---------------------------------------------------------------------------------
 
<%@ page language="java" %><%@ page import="java.sql.*" %><script>
function change(){
var cid=document.getElementById("book").selectedIndex;
var val = document.getElementById("book").options[cid].text;
window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?id="+cid+"&&value="+val);
}
function extract(){
var ide=document.getElementById("info").selectedIndex;
var bookname = document.getElementById("info").options[ide].text;
window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?book="+bookname);
}
</script>
<%!
Connection conn = null;
ResultSet rs =null;
Statement st=null;
String query="";

%><%
String value=request.getParameter("value");
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root";, "root");
st = conn.createStatement();
rs = st.executeQuery("select * from book");
%><select id="book" onchange="change();">
<option value="0">--Please Select--</option>
<% while(rs.next()){ %>
<option value="<%=rs.getString("books")%>"><%=rs.getString("books")%></option>
<% if(rs.getString("books").equals(value)){%>
<option value="<%=value%>" selected disabled><%=value%></option>
<%
}
}
%></select>
<select id="info" onchange="extract(this)">
<option value="0">--Please Select--</option>
<%
String id=request.getParameter("id");
rs=st.executeQuery("select * from bookInformation where bookid='"+id+"'");
while(rs.next()){
%>
<option value="<%=rs.getString("id")%>" ><%=rs.getString("booknames")%></option>
<%
}
%></select>
<%
String book=request.getParameter("book");
String author="";
String price="";
rs=st.executeQuery("select * from bookInformation where booknames='"+book+"'");
while(rs.next()){
author=rs.getString("writer");
price=rs.getString("price");
}
if((book!=null)&&(author!=null)&&(price!=null)){
%><table >
<tr><td>Book Name</td><td><input type="text" value="<%=book%>"></td></tr>
<tr><td>Author</td><td><input type="text" value="<%=author%>"></td></tr>
<tr><td>Price</td><td><input type="text" value="<%=price%>"></td></tr>
</table>
<%
}
%></body>
</html>
 
 
-------------------------------------------------------
Book
 
CREATE TABLE `book` (
`bookid` bigint(20) NOT NULL auto_increment,
`books` varchar(255) default NULL,
PRIMARY KEY (`bookid`)
);
 
------------------------------------------------------
  BookInformation

CREATE TABLE `bookinformation` (
`id` bigint(255) NOT NULL auto_increment,
`bookid` int(255) default NULL,
`booknames` varchar(255) default NULL,
`writer` varchar(255) default NULL,
`Price` double default NULL,
PRIMARY KEY (`id`)
);
 
 

Monday, April 29, 2013

CheckBox Example in jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show all names</title>
</head>
<body>


<form method="GET" action='Controller' name="showall">
<table>
  <tr>
    <td><input type="checkbox" name="id1" /></td>
    <td>Jim</td>
    <td>Knopf</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="id2" /></td>
    <td>Jim</td>
    <td>Bean</td>
  </tr>
</table>

<p><input type="submit" name="delete" value="delete" />&nbsp;
   <input type="submit" name="edit" value="edit" />&nbsp;
  <input type="reset"
  value="reset" /></p>
</form>



</body>
</html>

DoublyLinkedList Example in java


public class DoublyLinkedList {
  private Link first;

  private Link last;

  public DoublyLinkedList() {
    first = null;
    last = null;
  }

  public boolean isEmpty(){
    return first == null;
  }

  public void insertFirst(long dd){
    Link newLink = new Link(dd);

    if (isEmpty())
      last = newLink;
    else
      first.previous = newLink;
    newLink.next = first;
    first = newLink;
  }

  public void insertLast(long dd){
    Link newLink = new Link(dd);
    if (isEmpty())
      first = newLink;
    else {
      last.next = newLink;
      newLink.previous = last;
    }
    last = newLink;
  }

  public Link deleteFirst(){
    Link temp = first;
    if (first.next == null)
      last = null;
    else
      first.next.previous = null;
    first = first.next;
    return temp;
  }

  public Link deleteLast(){
    Link temp = last;
    if (first.next == null)
      first = null;
    else
      last.previous.next = null;
    last = last.previous;
    return temp;
  }

  public boolean insertAfter(long key, long dd) {
    Link current = first;
    while (current.dData != key){
      current = current.next;
      if (current == null)
        return false; // cannot find it
    }
    Link newLink = new Link(dd); // make new link

    if (current == last) // if last link,
    {
      newLink.next = null;
      last = newLink;
    } else // not last link,
    {
      newLink.next = current.next;
     
      current.next.previous = newLink;
    }
    newLink.previous = current;
    current.next = newLink;
    return true; // found it, insert
  }

  public Link deleteKey(long key){
    Link current = first;
    while (current.dData != key)
    {
      current = current.next;
      if (current == null)
        return null; // cannot find it
    }
    if (current == first) // found it; first item?
      first = current.next;
    else
      current.previous.next = current.next;

    if (current == last) // last item?
      last = current.previous;
    else
      // not last
      current.next.previous = current.previous;
    return current; // return value
  }

  public void displayForward() {
    System.out.print("List (first to last): ");
    Link current = first; // start at beginning
    while (current != null) // until end of list,
    {
      current.displayLink();
      current = current.next; // move to next link
    }
    System.out.println("");
  }

  public void displayBackward() {
    System.out.print("List : ");
    Link current = last;
    while (current != null){
      current.displayLink();
      current = current.previous;
    }
    System.out.println("");
  }

  public static void main(String[] args) {
    DoublyLinkedList theList = new DoublyLinkedList();

    theList.insertFirst(22);
    theList.insertFirst(44);
    theList.insertLast(33);
    theList.insertLast(55);

    theList.displayForward();
    theList.displayBackward();

    theList.deleteFirst();
    theList.deleteLast();
    theList.deleteKey(11);

    theList.displayForward();

    theList.insertAfter(22, 77); // insert 77 after 22
    theList.insertAfter(33, 88); // insert 88 after 33

    theList.displayForward();
  }

}

class Link {
  public long dData; // data item

  public Link next; // next link in list

  public Link previous; // previous link in list

  public Link(long d)
  {
    dData = d;
  }

  public void displayLink(){
    System.out.print(dData + " ");
  }

}

Link list example in java

class Link {
    public int data1;
    public double data2;
    public Link nextLink;

    //Link constructor
    public Link(int d1, double d2) {
        data1 = d1;
        data2 = d2;
    }

    //Print Link data
    public void printLink() {
        System.out.print("{" + data1 + ", " + data2 + "} ");
    }
}

class LinkList {
    private Link first;

    //LinkList constructor
    public LinkList() {
        first = null;
    }

    //Returns true if list is empty
    public boolean isEmpty() {
        return first == null;
    }

    //Inserts a new Link at the first of the list
    public void insert(int d1, double d2) {
        Link link = new Link(d1, d2);
        link.nextLink = first;
        first = link;
    }

    //Deletes the link at the first of the list
    public Link delete() {
        Link temp = first;
        first = first.nextLink;
        return temp;
    }

    //Prints list data
    public void printList() {
        Link currentLink = first;
        System.out.print("List: ");
        while(currentLink != null) {
            currentLink.printLink();
            currentLink = currentLink.nextLink;
        }
        System.out.println("");
    }
}  

class LinkListTest {
    public static void main(String[] args) {
        LinkList list = new LinkList();

        list.insert(1, 1.01);
        list.insert(2, 2.02);
        list.insert(3, 3.03);
        list.insert(4, 4.04);
        list.insert(5, 5.05);

        list.printList();

        while(!list.isEmpty()) {
            Link deletedLink = list.delete();
            System.out.print("deleted: ");
            deletedLink.printLink();
            System.out.println("");
        }
        list.printList();
    }
}

Thursday, April 25, 2013

Best Java IDE

  
NetBeans and Eclipse
                                                       NetBeans &Eclipse are certainly the two best free IDEs for Java. NetBeans is still slightly more Java-centric, even though it has been adding features for other languages lately. Eclipse has been doing this for years.
                  I like the GUI editor that ships built in to NetBeans (Eclipse has one available as
 a  plug-in), but other than that the two have a very similar feature set for Java development.

What is Java Design Pattern ?

What is Java Design Pattern ?

“Design patterns are recurring solutions  to design problems.”


  Patterns: According to commonly known practices, there are 23  design patterns in Java. These patterns are grouped under three heads:
1. Creational Patterns
2. Structural Patterns
3. Behavioral Patterns




  • Java Design Patterns

 Adopt Adapter
Software usually consists of a mixture of in-house and purchased software that must work together to produce a seamless user interface. But disparate software packages are not aware of each other's object models, so they can't work together—without adapters. Adapters let objects from unrelated software packages collaborate by adapting one interface to another. Learn how the Adapter design pattern can save you a lot of time and effort by combining disparate software systems.
David Geary, September 2003


 

Follow the Chain of Responsibility
The Chain of Responsibility (CoR) pattern decouples the sender and receiver of a request by interposing a chain of objects between them. In this installment of Java Design Patterns, David Geary discusses the CoR pattern and two implementations of that pattern in the Java APIs—one from client-side Java and the other from server-side Java.
David Geary, August 2003

 

Make your apps fly
Allocating numerous objects can be detrimental to your application's performance. In this installment of Java Design Patterns, David Geary shows you how to implement the Flyweight design pattern to greatly reduce the number of objects your application creates, which decreases your application's memory footprint and increases performance.
David Geary, July 2003
 
Façade clears complexity
The Façade design pattern simplifies complex APIs by providing a simplified interface to a complex subsystem. In this installment of Java Design Patterns, David Geary explores a built-in Swing façade for creating dialog boxes and a custom façade for getting a Swing application off the ground.
David Geary, May 2003
 
Simply Singleton
The Singleton pattern is deceptively simple, even and especially for Java developers. In this classic JavaWorld article, David Geary demonstrates how Java developers implement singletons, with code examples for multithreading, classloaders, and serialization. He concludes with a look at implementing singleton registries in order to specify singletons at runtime.
David Geary, April 2003

 

An inside view of Observer
The Observer pattern lets you build extensible software with pluggable objects by allowing communication between loosely coupled objects. In his latest Java Design Patterns column, David Geary explores the Observer pattern, how it's used throughout the Java 2 SDK, and how you can implement the pattern in your own code.
David Geary, March 2003

 

A look at the Composite design pattern
The Composite design pattern lets you treat primitive and composite objects exactly the same. In his latest Java Design Patterns column, David Geary explores how to implement the Composite pattern and how to use it with the Tiles tag library from the Apache Struts application framework.
David Geary, September 2002
 
Take command of your software
The Command pattern lets an application framework make requests of application-specific objects, without the framework knowing the objects' exact type or the application-specific behavior they implement. In his latest Java Design Patterns column, David Geary explores how to use the Command pattern both in client-side Java to attach application-specific behavior to Swing menu items and in server-side Java to implement application-specific behavior with the Apache Struts application framework.
David Geary, June 2002
 
Strategy for success
The Strategy design pattern embodies two fundamental tenets of object-oriented (OO) design: encapsulate the concept that varies and program to an interface, not an implementation. In this article, David Geary shows how to use the Strategy pattern to implement an extensible design.
David Geary, April 2002
 
Take control with the Proxy design pattern
The Proxy design pattern lets you substitute a proxy for an object. In that capacity, proxies prove useful in many situations, ranging from Web services to Swing icons. In this latest Java Design Patterns installment, David Geary explores the Proxy pattern.
David Geary, February 2002
 
Amaze your developer friends with design patterns
Design patterns are proven techniques for implementing robust, malleable, reusable, and extensible object-oriented software. To launch his Java Design Patterns column, David Geary introduces design patterns to Java developers and explores Strategy, Composite, and Decorator -- three common, yet powerful, design patterns employed throughout the JDK.
David Geary, October 2001