Monday, May 20, 2013

switch case in jsp


<%    switch (tabNavigator)

     {

                            

      case QUALIFICATION:

                         %>

<jsp:include page="classmaster.jsp"></jsp:include>

                   <%

                        break;

                        case RACKSHELF:

                   %>

<jsp:include page="rackshelfmaster.jsp"></jsp:include>

                   <%

                        break;

                            case MEDIA:

                   %>

                   <jsp:include page="mediamaster.jsp"></jsp:include>

                   <%

                   break;

                             case MEMBER:

                   %>

                   <jsp:include page="membermaster.jsp"></jsp:include>

                   <%

                        break;

                             case PLAN:

                   %>

                   <jsp:include page="planmaster.jsp"></jsp:include>

                   <%

                        break;

                             case DEPARTMENT:

                   %>

                   <jsp:include page="departmentmaster.jsp"></jsp:include>

                        <%

                        break;

                             case VENDOR:

                   %>

                   <jsp:include page="vendormaster.jsp"></jsp:include>

                   <%

                        break;

             

                        default:

                   %>

                             <jsp:include page="index.jsp"></jsp:include>

                   <%

                        }

                             session.removeAttribute("NAVIGATOR_REQUEST");

                   }

                   %>



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.