Choose Your Language

Monday, 27 May 2013

passing multiple parameter to jsp page using ajax example code

<%--
    Document   : userregistration
    Created on : 17 May, 2013, 11:22:42 AM
    Author     : Aravind Sankaran
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>

<%!static Connection con;%>
<%!  static String url="jdbc:mysql://localhost:3306/dbname?";%>
<%! static String userName = "dbUserName"; %>
<%! static String password = "dbPassword";%>

 <%
         Class.forName("com.mysql.jdbc.Driver");
         con=(Connection)DriverManager.getConnection(url, userName, password);
         Statement s = (Statement)con.createStatement();
          ResultSet rs=s.executeQuery("SELECT * FROM usertype");
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;

var usertype = document.getElementById("utype");
var userType=
usertype .options[usertype .selectedIndex].value;
var uname=document.getElementById("username").value;
var urls="checkusername.jsp?un="+
uname+"&ut="+userType;

if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
        document.getElementById("err").innerHTML=xmlhttp.responseText;
     }
  }
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
</script>

    </head>
    <body>
<%

%>
        User Type<select name="utype" id="utype">
<%while(rs,next()){
 String userType=rs.getString("userTypeColumnName");
%>
                              <option  value="<%=userType%>"><%=userType%></option>
<%}%>
                        </select>
        User Name: <input type="text" name="username" id="username" onkeyup="loadXMLDoc()">
        <span id="err"> </span>

    </body>
</html>


checkusername.jsp


<%--
    Document   : checkusername
    Created on : 9 May, 2013, 12:42:25 PM
    Author     : Aravind Sankaran
--%>

<%@ page import="java.io.*,java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>

<% 
                    String
un=request.getParameter("un");                    String ut=request.getParameter("ut");
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","databaseusername","databasepassword");
                    Statement st=con.createStatement();
                    ResultSet rs = st.executeQuery("select * from table_name where username='"+un+"' AND usertype='"+ut+"' ");  // this is for name
                    if(rs.next())
                    {   
                        out.println("<font color=red>");
                        out.println("Name already taken");
                        out.println("</font>");

                    }else {
                        out.println("<font color=green>");
                        out.println("Available");
                        out.println("</font>");

                    }
rs.close();
st.close();
con.close();
%>

Saturday, 25 May 2013

Abstract class vs Interface

Abstract class vs Interface

         An abstract class can have shared state or functionality. An interface is only a promise to
provide the state or functionality. A good abstract class will reduce the amount of code that has
to be rewritten because it's functionality or state can be shared. The interface has no defined
information to be shared

        In java you can inherit from one (abstract) class to "provide" functionality and you can
implement many interfaces to "ensure" functionality

A simple three line concept

            Use an abstract class when you want to inherit only from the parent

            Use an interface when you want to inherit from multiple sources

            Use both when you want a basic behavior from the parent and extra features from other
                sources

Is there a performance overhead in using either an abstract class or an
interface?


         Abstract class is faster than interface.
       
example:

abstract class shape
                 {
                   int a,b,c;
                   String s1;
                   abstract area(int);
                   abstract area(int a, string s);
                 }

 Why we are? and when we are using Interfaces ?      

               Interfaces are useful when you do not want classes to inherit from unrelated classes just     
to get the required functionality. For example, let bird be a class with a method fly(). It will be
ridiculous for an aeroplane to inherit from bird class just because it has the fly() method. Rather
the fly() method should be defined as an interface and both bird and aeroplane should implement
that interface.

When should I use an interface instead of an abstract class

            Abstract class is an incomplete implementation of some concept. This incomplete implementation may be different in different context. Derived class implements the abstract class in its context.

            Interface defines contract or standard. Implementation of the interface has to follow the contract or standard. Interfaces are more used to set these types of standards or contracts.

   E.g. In an application there are different editors. There are different toolbars, which can be used on these editors like navigation tool bar. In order to use this tool bar editor should implement that functionality.

So there can be interfaces like - navigation - search – save and modify if an editor implements all three all toolbars will be enabled. if one then only one will be enable. and implementation of these interface differs from context to context.

In the same example myAbstractEditor can be a class, which implements basic functionality of the editor. Where setting different controls, getting values from them can be abstract, which will depend on editor to editor.
          

Thursday, 16 May 2013

Checking User Name Availability Using AJAX, JSP And MySQL

userregistration.jsp


<%--
    Document   : userregistration
    Created on : 17 May, 2013, 11:22:42 AM
    Author     : Aravind Sankaran
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var k=document.getElementById("username").value;
var urls="checkusername.jsp?ver="+k;

if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
        document.getElementById("err").innerHTML=xmlhttp.responseText;
     }
  }
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
</script>

    </head>
    <body>
        User Name: <input type="text" name="username" id="username" onkeyup="loadXMLDoc()">
        <span id="err"> </span>

    </body>
</html>


 checkusername.jsp


<%--
    Document   : checkusername
    Created on : 9 May, 2013, 12:42:25 PM
    Author     : Aravind Sankaran
--%>

<%@ page import="java.io.*,java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>

<% 
                    String sn=request.getParameter("ver");

                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","databaseusername","databasepassword");
                    Statement st=con.createStatement();
                    ResultSet rs = st.executeQuery("select * from table_name where username='"+sn+"'");  // this is for name
                    if(rs.next())
                    {   
                        out.println("<font color=red>");
                        out.println("Name already taken");
                        out.println("</font>");

                    }else {
                        out.println("<font color=green>");
                        out.println("Available");
                        out.println("</font>");

                    }
rs.close();
st.close();
con.close();
%>




web.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<welcome-file-list>
<welcome-file>userregistration.jsp</welcome-file>
</welcome-file-list>
</web-app>