Wednesday, 15 May 2013

Result Page

Displaying Page After Clicking a link


<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import ="java.lang.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String path=request.getParameter("file");
File f=new File(path);
BufferedReader br=new BufferedReader(new FileReader(f));
String line="";
                        out.println("<br><br>");
while((line=br.readLine())!=null)
{
out.println(line+"<br>");

}
%>

Monday, 13 May 2013

Search Page for simple search engine academic project

Hello friends here is my search page for the AUEP Search engine you can see 2 kinds of searching here

1.Normal Search
2.AUEP Search (Searching based on the classification of regions)




















And here is the code

Search.jsp
---------------

<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%!ResultSet rs = null,rs1 = null;
%>
<%          String dbip="";
            String dbcon=(String)application.getAttribute("dbcon");
            String cip=java.net.Inet4Address.getLocalHost().getHostAddress();
            String str = "select * from regions where startip='";
            String curreg="";
            cip= "127.0.0.1";
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con =DriverManager.getConnection("jdbc:odbc:ndb");
                Statement st = con.createStatement();
                rs=st.executeQuery(str+cip+"'");
                while(rs.next())
                {
                    dbip=rs.getString("startip");
                    curreg=rs.getString("region");
                }
                if(cip.equalsIgnoreCase(dbip))
                {
                    application.setAttribute("yip",cip);
                    application.setAttribute("dbcountry",curreg);
                }
                else
                {
                   application.setAttribute("yip",dbip);
                   application.setAttribute("dbcon",dbcon);
                }
             }
            catch (Exception ex)
            {
                out.println("Unable to connect to database. " + ex);
            }
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Search Page</title>
         <style type="text/css">
            <!--
            .b:hover {background:url(images/templatemo_menu_hover.jpg);}
            .b { width: 78px;
                 background: url(images/templatemo_menu_hover.png) no-repeat 0 0;
                 height: 41px;
                 line-height: 29px;
                 padding: 0;
                 font-weight: bold;
                 padding-left: 11px;
                 color: #fff;
               }

            .l { width: 106px;
                 background: url(images/templatemo_menu_hover.jpg) no-repeat 0 0;
                 height: 44px;
                 font-weight: bold;
                 font-size: 25;
                 color: #fff;
               }
               .l:hover{background: url(images/templatemo_menu_hover.png) no-repeat 0 0;}

               .s {
                font-family:Courier, monospace;
                font-size: 16px;
                font-style: normal;
                line-height: normal;
                font-weight: bold;
                font-variant: normal;
                text-decoration: none;
                background-repeat: no-repeat;
                text-transform: capitalize;
                word-spacing: normal;
                cursor: auto;
            }
            -->
        </style>
        <script type="text/javascript">
            function log()
            {
                alert("Successfully Logout !");
                document.location.href="index.jsp";
            }
            </script>
    </head>

    <body>
        <input type="button" class="l" value="Logout" onclick="log()"/>
        <table width="1076" height="413" border="0" class="s">
            <tr>
                <td height="409"><form id="form1" name="form1" method="post" action="response.jsp">
                        <div align="center"><h1>Welcome </h1>
                            <%String un=(String)application.getAttribute("uname");
                             %>
                            <h2><font size="5" color="green"><%out.println(un);%><br/>
                                    <%String dbc=(String)application.getAttribute("dbcountry");
                                    out.println("Your ip is :"+cip+"<br/>"+"Your current location is "+dbc);%></font></h2>
                            <p><h1>Search Content</h1></p>
                            <p><img src="icon3.gif" width="140" height="121" /></p>
                            <p>
                                <label>&nbsp;
                                    <input type="radio" name="radio" id="rbnormal"  value="rbnormal" />
                                    Normal Search</label>
                            </p>
                            <p>
                                <label>
                                    <input type="radio" name="radio" id="rbauep" checked="checked" value="rbauep" />
                                    AUEP Search
                                </label>
                            </p>
                            <p>
                                <input type="text" name="tsearch" id="tsearch" size="75"/>&nbsp;&nbsp;&nbsp;&nbsp;
                                <input class="b" type="submit" name="bsearch" id="bsearch" value="Search"/>
                            </p>
                        </div>
                    </form></td>
            </tr>
        </table>
    </body>
</html>


It is just design part for the search page the actual code shown below...


NormalSearch.jsp
-------------------

<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import ="java.lang.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.StringTokenizer.*"%>
<%!ResultSet rs = null,rs1 = null;
    String results[];
    String fname = "";
    int i,j;%>
<%
            String sw = (String) application.getAttribute("sw");
            String swd = (String) application.getAttribute("swd");
            StringTokenizer tok=new StringTokenizer(sw," ");
            int c=tok.countTokens();
            int k=0;
            String sw1[]=new String[c];
            for(i=0;i<c;i++)
            {
                 sw1[i]=tok.nextToken();
            }
            String sw2[]=new String[c];
            String loc[] =new String[c];
            String result = "";
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con =DriverManager.getConnection("jdbc:odbc:ndb");
                Statement st = con.createStatement();
                rs1=st.executeQuery("select filename from floc");
                while(rs1.next())
                {
                    for(i=0;i<c;i++)
                    {
                        if(sw1[i].equalsIgnoreCase(rs1.getString("filename")))
                        {
                            sw2[i]=sw1[i];
                            k++;
                        }
                        
                    }
                }
                for(i=0;i<k;i++)
                {
                    rs = st.executeQuery("select location from floc where filename='" + sw2[i] + "'");
                    while (rs.next())
                    {
                        loc[i] = rs.getString("location");
                    }

                    File path = new File(loc[i]);
                    File files[] = path.listFiles();
                    if (files != null)
                    {
                        for (j = 0; j < files.length; j++)
                        {
                            if (files.length == 0)
                            {
                                out.println("");
                                
                            }
                            else
                            {
                                try
                                {
                                    fname = files[j].toString();
                                    result = result + fname + "#";
                                }
                                catch (Exception ee)
                                {
                                    ee.printStackTrace();
                                    response.sendRedirect("notfound.jsp");
                                }

                            }

                        }

                    }
                    else
                    {
                        response.sendRedirect("notfound.jsp");
                    }
                }
            } 
            catch (Exception ex)
            {
                out.println("Unable to connect to database. " + ex);
                response.sendRedirect("notfound.jsp");
            }
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Search Page</title>
        <style type="text/css">
            <!--
            .b:hover {background:url(images/templatemo_menu_hover.jpg);}
            .b { width: 78px;
                 background: url(images/templatemo_menu_hover.png) no-repeat 0 0;
                 height: 41px;
                 line-height: 29px;
                 padding: 0;
                 font-weight: bold;
                 padding-lef: 11px;
                 color: #fff
            }
            .s {
                font-family:Courier, monospace;
                font-size: 16px;
                font-style: normal;
                line-height: normal;
                font-weight: bold;
                font-variant: normal;
                text-decoration: none;
                background-repeat: no-repeat;
                text-transform: capitalize;
                word-spacing: normal;
                cursor: auto;
            }
            .l { width: 106px;
                 background: url(images/templatemo_menu_hover.jpg) no-repeat 0 0;
                 height: 44px;
                 font-weight: bold;
                 font-size: 25;
                 color: #fff;
               }
               .l:hover{background: url(images/templatemo_menu_hover.png) no-repeat 0 0;}

            -->
        </style>
        <script type="text/javascript">
            function log()
            {
                alert("Successfully Logout !");
                document.location.href="index.jsp";
            }
            </script>
        </head>
        <body>
            <input type="button" class="l" value="Logout" onclick="log()"/>
        <table width="1076" height="413" border="0" class="s">
            <tr>
                <td height="409"><form id="form1" name="form1" method="get" action="response.jsp">
                        <div align="center"><h1>Welcome</h1>
                            <%String un = (String) application.getAttribute("uname");
                              String ip = (String) application.getAttribute("yip");
                              String dbcountry=(String) application.getAttribute("dbcountry");%>
                              <h2><font size="5" color="green"><%out.println(un);%><br/>
                                <%out.println("Your ip is :" +ip+"<br/>"+"Your current location is "+dbcountry);%></font></h2>
                            <p><h1>Search Content</h1></p>
                            <p><img src="icon3.gif" width="140" height="121" /></p>
                            <p>
                                <label>&nbsp;
                                    <input type="radio" name="radio" id="rbnormal" checked="checked" value="rbnormal" />
                                    Normal Search</label>
                            </p>
                            <p>
                                <label>
                                    <input type="radio" name="radio" id="rbauep" value="rbauep" />
                                    AUEP Search
                                </label>
                            </p>
                            <p>
                                <input type="text" name="tsearch" id="tsearch" value="<%=swd%>" size="75"/>&nbsp;&nbsp;&nbsp;&nbsp;
                                <input class="b" type="submit" name="bsearch" id="bsearch" value="Search"/>
                            </p>
                        </div>
                        <%
                                    results = result.split("#");
                                    for (i = 0; i < results.length; i++)
                                    {
                                        File fn = new File(results[i]);
                                        String sfile = results[i].replace('\\', '&');
                                        String filename[] = sfile.split("&");
                                        try
                                        {
                                        BufferedReader br = new BufferedReader(new FileReader(fn));
                                        String line = "";
                                        String pages= filename[filename.length-1].replaceAll(".txt", "");
                                        String add = results[i].replaceAll(" ", "%20");
                                        out.println("<a href=result.jsp?file="+add+">"+pages+"<br></a>");
                                        String content = "";
                                        String address = "";
                                        while ((line = br.readLine()) != null)
                                        {
                                            content=content+line+"\t";
                                            address = line;
                                        }

                                        out.println(content.substring(0, 90)+"<br>");
                                        out.println("<link href=''>" + "<font color='green'>" + address + "</font></link><br><br>");
                                        }
                                       catch (Exception ee)
                                        {
                                               response.sendRedirect("notfound.jsp");
                                        }
                                     }
                        %>
                    </form></td>
            </tr>
        </table>
    </body>
</html>



AUEP Search.jsp
--------------------

<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import ="java.lang.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.StringTokenizer.*"%>
<%!ResultSet rs = null, rs1 = null, rs2 = null, filtrs = null;
    String dbfiles[] = new String[50];
    String content[] = new String[100];
    String fname[] = new String[50];
    String link[] = new String[50];
    String dbfiles1[] = new String[50];
    String content1[] = new String[100];
    String fname1[] = new String[50];
    String link1[] = new String[50];
    String flag = "";int count=0;%>
<%
            String sw = (String) application.getAttribute("sw");
            String swd = (String) application.getAttribute("swd");
            StringTokenizer tok = new StringTokenizer(sw, " ");
            String ipaddr = (String) application.getAttribute("yip");
            String str = "select region from regions where startip='";
            int c = tok.countTokens();
            int i = 0, j = 0, l = 0, l1 = 0, r = 0, r1 = 0, y = 0,f=0;
            String dbcon1 = (String) application.getAttribute("dbcon");
            String curreg1 = (String) application.getAttribute("dbcountry");
            String sw1[] = new String[c];
            String sw2[] = new String[100];
            String sw3 = sw + " ";
            String tem = new String();
            String dbreg = "", dbfname = "", dbfname1 = "";
            String dbloc = "", dbloc1 = "";
            String dbcontent = "", dblink = "", dbcontent1 = "", dblink1 = "";
            if (dbcon1.equalsIgnoreCase(curreg1))
            {
                flag = "0";
            } 
            else
            {
                flag = "1";
            }
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con =DriverManager.getConnection("jdbc:odbc:ndb");
                Statement st = con.createStatement();
                rs = st.executeQuery(str + ipaddr + "'");
                while (rs.next())
                {
                    dbreg = rs.getString("region");
                }
                application.setAttribute("dbreg", dbreg);
                filtrs=st.executeQuery("select region from regions");
                while(filtrs.next())
                {
                    if(sw.contains(filtrs.getString("region")))
                    {
                        if(sw.contains("american"))
                        {
                            sw=sw.replace("american","").trim();
                            dbreg="america".trim();
                        }
                        else if(sw.contains("indian"))
                        {
                             sw=sw.replace("indian","").trim();
                             dbreg="india".trim();
                        }
                        else
                        {
                            sw=sw.replace(filtrs.getString("region"),"").trim();
                            dbreg=filtrs.getString("region").trim();
                        }
                        break;
                    }
                }
                sw2[0] = "  "+sw;
                //out.println(sw2[0]);
                for (r = 1, r1 = 1; r <= 5; r++)
                {
                    filtrs = st.executeQuery("select * from filtering where level=" + r);
                    while (filtrs.next())
                    {
                        if (sw3.contains(filtrs.getString("keyword")))
                        {
                            tem = sw3.replace(" " + filtrs.getString("keyword") + " ", " ");
                            sw3 = " "+tem;
                        }

                    }
                    if (!sw2[r1 - 1].equalsIgnoreCase(tem.trim()))
                    {
                        if(sw3.trim().equalsIgnoreCase(sw2[0].trim()))
                            continue;
                        sw2[r1] = tem.trim();
                        //out.println(sw2[r1]+"<br/>");
                        r1++;
                    }
                    
                }

                i = 0;
                j = 0;
                for (y = 0; y < r1; y++)
                {
                    rs1 = st.executeQuery("select location,content,filename,link from" + " " + dbreg + " " + "where keyword='" + sw2[y].trim() + "'" + " " + "order by counter desc");
                    while (rs1.next())
                    {
                        dbloc = rs1.getString("location");
                        dbcontent = rs1.getString("content");
                        dbfname = rs1.getString("filename");
                        dblink = rs1.getString("link");
                        dbfiles[i] = dbloc;
                        content[i] = dbcontent;
                        fname[i] = dbfname.replace(".txt", "");
                        link[i] = dblink;
                        i++;
                        count=count+1;
                    }
                    l = i;
                    rs2 = st.executeQuery("select location,content,filename,link from" + " " + dbcon1 + " " + "where keyword='" + sw2[y].trim() + "'" + " " + "order by counter desc");
                    while (rs2.next())
                    {
                        dbloc1 = rs2.getString("location");
                        dbcontent1 = rs2.getString("content");
                        dbfname1 = rs2.getString("filename");
                        dblink1 = rs2.getString("link");
                        dbfiles1[j] = dbloc1;
                        content1[j] = dbcontent1;
                        fname1[j] = dbfname1.replace(".txt", "");
                        link1[j] = dblink1;
                        j++;
                        count=count+1;
                    }
                    l1 = j;
                }
                if (count == 0) {
                                    count=0;
                                    response.sendRedirect("notfound.jsp");
                                }
            } 
            catch (Exception ex)
            {
                out.println("Unable to connect to database. " + ex);
                response.sendRedirect("notfound.jsp");
            }
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Search Page</title>
        <style type="text/css">
            <!--
            .b:hover {background:url(images/templatemo_menu_hover.jpg);}
            .b { width: 78px;
                 background: url(images/templatemo_menu_hover.png) no-repeat 0 0;
                 height: 41px;
                 line-height: 29px;
                 padding: 0;
                 font-weight: bold;
                 padding-lef: 11px;
                 color: #fff
            }
            .s {
                font-family:Courier, monospace;
                font-size: 16px;
                font-style: normal;
                line-height: normal;
                font-weight: bold;
                font-variant: normal;
                text-decoration: none;
                background-repeat: no-repeat;
                text-transform: capitalize;
                word-spacing: normal;
                cursor: auto;
            }
            .l { width: 106px;
                 background: url(images/templatemo_menu_hover.jpg) no-repeat 0 0;
                 height: 44px;
                 font-weight: bold;
                 font-size: 25;
                 color: #fff;
            }
            .l:hover{background: url(images/templatemo_menu_hover.png) no-repeat 0 0;}

            -->
        </style>
        <script type="text/javascript">
            function log()
            {
                alert("Successfully Logout !");
                document.location.href="index.jsp";
            }
        </script>
    </head>
    <body>
        <input type="button" class="l" value="Logout" onclick="log()"/>
        <table width="1076" height="413" border="0" class="s">
            <tr>
                <td height="409"><form id="form1" name="form1" method="post" action="response.jsp">
                        <div align="center"><h1>Welcome</h1>
                            <%String un = (String) application.getAttribute("uname");
                                        String ip = (String) application.getAttribute("yip");
                                        String dbcountry = (String) application.getAttribute("dbcountry");%>
                            <h2><font size="5" color="green"><%out.println(un);%><br/>
                                    <%out.println("Your ip is :" + ip + "<br/>" + "Your current location is " + dbcountry);%></font></h2>
                            <p><h1>Search Content</h1></p>
                            <p><img src="icon3.gif" width="140" height="121" /></p>
                            <p>
                                <label>&nbsp;
                                    <input type="radio" name="radio" id="rbnormal"  value="rbnormal" />
                                    Normal Search</label>
                            </p>
                            <p>
                                <label>
                                    <input type="radio" name="radio" id="rbauep" checked="checked" value="rbauep" />
                                    AUEP Search
                                </label>
                            </p>
                            <p>
                                <input type="text" name="tsearch" id="tsearch" value="<%=swd%>" size="75"/>&nbsp;&nbsp;&nbsp;&nbsp;
                                <input class="b" type="submit" name="bsearch" id="bsearch" value="Search"/>
                            </p>
                        </div>
                        <%          
                                    
                                   
                                    for (i = 0; i < l; i++)
                                    {
                                        dbfiles[i] = dbfiles[i].replaceAll(" ", "%20");
                                        fname[i].replaceAll(" ", "%20");
                                        out.println("<a href=result1.jsp?file=" + dbfiles[i].replace("/", "\\") + "\\" + fname[i].replaceAll(" ", "%20") + ".txt>" + fname[i] + "<br/>" + "</a>");
                                        out.println("<link href=''>" + content[i] + "<br/>" + "<font color='green'>" + link[i] + "</font></link><br><br>");
                                        count=0;
                                    }
                                    if (flag.equalsIgnoreCase("1"))
                                    {

                                        for (j = 0; j < l1; j++)
                                        {
                                            dbfiles1[j] = dbfiles1[j].replaceAll(" ", "%20");
                                            fname1[j].replaceAll(" ", "%20");
                                            out.println("<a href=result1.jsp?file=" + dbfiles1[j].replace("/", "\\") + "\\" + fname1[j].replaceAll(" ", "%20") + ".txt>" + fname1[j] + "<br/>" + "</a>");
                                            out.println("<link href=''>" + content1[j] + "<br/>" + "<font color='green'>" + link1[j] + "</font></link><br><br>");
                                        }
                                        flag = "0";
                                    }

                        %>
                    </form></td>
            </tr>
        </table>
    </body>
</html>

You can see that the page has a similarity with google search engine and here i'm displaying the search page content from my local mysql database but the way of fetching the data is different there is the search engine actually works.That will be discussed later.

Wednesday, 8 May 2013

Simple Search Engine Academic Project

To day i'm going to show you a simple search engine project based on the web classification it was done during my final year semester....

Modules are
-------------
  1. Login Page 
  2. Search Page 
  • Normal Search
  • Searching Using Adanced Unconstrained Profiling Tool
3.Sign Up Page
4.Admin Page for automatic database loading

Login Page
--------------

I created a login page for our search engine for displaying contents based own users history....


index.jsp
-----------
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Home Page</title>
        <script type="text/javascript">
            function n()
            {
                document.location.href="new.jsp";
            }
            function check()
            {
                if(form1.username.value=="")
                {
                    alert("Please Enter Your User ID");
                    form1.username.focus();
                    return false;
                }

                if(form1.password.value=="")
                {
                    alert("Please Enter Your Password");
                    form1.password.focus();
                    return false;
                }
             
                return true;

            }
             
        </script>
        <style type="text/css">
            <!--
            .b:hover {background-image:url(images/templatemo_header.jpg);  }
            .b { width: 78px;
                 background: url(images/sign-button.gif) no-repeat 0 0;
                 height: 41px;
                 line-height: 29px;
                 padding: 0;
                 font-weight: bold;
                 padding-lef: 11px;
                 color: #fff;
            }
            .n:hover {background-image:url(images/signup_hover.jpg);  }
            .n { width: 100px;
                 background-image:url(images/signup.jpg);
                 background-repeat: no-repeat;
                 height: 41px;
                 font-weight: bold;
                 color: #fff;
            }

            .s {
                font-family: Courier New, Courier, monospace;
                font-size: 19px;
                font-style: normal;
                line-height: normal;
                font-weight: bold;
                font-variant: normal;
                color: #CCC;
                text-decoration: none;
                background-image: url(images/slider-bg.jpg);
                background-repeat: no-repeat;
                text-transform: capitalize;
                word-spacing: normal;
                cursor: auto;
            }
            .d {
                font-family: Courier New, Courier, monospace;
                font-size: 19px;
                font-style: normal;
                line-height: normal;
                font-weight: bold;
                font-variant: normal;
                color: #CCC;
                text-decoration: none;
                cursor: auto;
            }
            -->
        </style>
    </head>
    <body background="apple_wall_2-1680x1050.jpg">
        <table class="s" width="697" height="436" border="0" align="center">
            <tr>
                <td width="334" height="207"><form name="form1" method="post" action="login.jsp">
                        <div align="left"><br/><br/><br/>
                            <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="tablerightmemberslogin.gif" width="229" height="25" /></p>
                            <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                User Name :
                                <input type="text" name="username" id="username" />
                            </p>
                            <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                &nbsp;Password :
                                <input type="password" name="password" id="password" />
                            </p>
                            <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" class="n" value="Sign Up" onclick="n()"/>&nbsp;
                                <input class="b" type="submit" name="login" id="login" value="Login" onclick="return check()"/>
                                <input class="b" type="reset" name="clear" id="clear" value="Clear" />
                            </p>
                            <p>&nbsp;</p>
                        </div>
                    </form></td>
            </tr>
            <td align="left">
                   <pre class="d"><br/><br/>
<font color="orange">Project Done By :</font>
    1.Anson Francis
    2.Bibin Joseph
    3.Jubit Varghese Joy
    4.S Nijanthan
<font color="orange">Project Guided By :</font>
    1.S Gunasekaran
    2.A P V Raghavendra
                </pre>

            </td>
        </table>
    </body>
</html>

It is the designing part.....and the jsp code for login page  is

Login.jsp
------------

<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.StringTokenizer" %>
<%@ page import="java.lang.*" %>

<%!ResultSet rs = null;%>
<%
            String user = request.getParameter("username");
            String pass = request.getParameter("password");
            application.setAttribute("uname",user);
            int flag = 0;
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con =DriverManager.getConnection("jdbc:odbc:ndb");
                Statement st = con.createStatement();
                rs = st.executeQuery("select * from login");
                String dbuser = "";
                String dbpass = "";
                String dbcountry="";
                if (!con.isClosed())
                {
                    while (rs.next())
                    {
                        dbuser = rs.getString("username");
                        dbpass = rs.getString("password");
                        dbcountry=rs.getString("country");
                        application.setAttribute("upass",dbpass);
                        if (user.equalsIgnoreCase(dbuser) && pass.equalsIgnoreCase(dbpass))
                        {
                            flag = 1;
                            application.setAttribute("dbcon",dbcountry);
                        }
                    }
                    if (flag == 1)
                    {
                     
                        response.sendRedirect("search.jsp");
                    }
                    else
                    {

                        response.sendRedirect("error.jsp");
                    }

                }
                con.close();
            }
            catch (Exception ex)
            {
                out.println("Unable to connect to database. " + ex);
            }

%>



Tuesday, 7 May 2013

J2EE with Jquery and Ajax

Here i created a simple html application which used jQuery for displaying signup and login page with toggle effect and Side Effect.And also ajax for dynamic validation.

Lets see how we can create it through the below tutorial.........

Before you start jQuery you can use jQuery in two different ways

1.Downloading jQuery from http://jquery.com/download/ and refer it in <head> tag like this

<head>
<script src="jquery-1.9.1.min.js"></script>
</head>

2.Using google CDN or Microsoft CDN

Google CDN
-----------------

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>

Microsoft CDN

------------------

<head>

<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js">
</script>
</head>

If you are using the CDN means you need a working internet connection to perform jQuery.



Design a form

--------------


<body>
        <div id="main">
            <form method="post" action="login.jsp" name="home">
                <h2 id="l">Login</h2>

                <h2 id="s">Sign Up</h2>

                <div id="frm">
                    <h1 style="color: white;">Users Login</h1>
                    User Name :<input style="margin-left: 15px;" type="text" name="username" value="" /><br/><br/>
                    Password :<input style="margin-left: 27px;" type="password" name="password" value="" />
                    <input type="submit" class="b" value="Login" name="submit" onclick="return logfield()"/>
                </div>
            </form>
            <form method="get" action="signup.jsp" name="signup" onsubmit="return regfield()">
                <div id="frm1">
                    <h1 style="color: white;">Sign Up</h1>
                    User Name :<input style="margin-left: 15px;" type="text" name="susername" value="" /><br/><br/><div id="chk" style="color: #ff9999;font-size: 30px;"></div>
                    Password :<input style="margin-left: 27px;" type="password" name="spassword" value="" onfocus="user(susername.value)"/><br/><br/>
                    Re-password :<input style="margin-left: 5px;" type="password" name="repassword" value="" /><br/><br/>
                    E-mail :<input style="margin-left: 48px;" type="email" name="email" value="" /><br/><br/>
                    Mobile :<input style="margin-left: 47px;" type="text" name="mobile" /><br/><br/>
                    <input type="submit" class="b" value="Submit" name="submit"  />
                </div>
            </form>
        </div>
        <div style="color: white;font-size: 30px;">
        </div>
    </body>



In Scrip tag you can include the jQuery like this




<script>
            $(document).ready(function(){
                $("#l").click(function(){
                    $("#frm").toggle("slow");
                    $("#frm1").slideUp("slow");
                });
            });
            $(document).ready(function(){
                $("#s").click(function(){
                    $("#frm").slideUp("slow");
                    $("#frm1").slideDown("slow");
                });
            });
        </script>

'#l' is the reference of heading Login


Here '#frm' is used for naming the form that means it is a reference....



Ajax Code

----------------
<script type="text/javascript">


function user(str)
            {
                var xmlhttp;    
                if (str=="")
                {
                    document.getElementById("chk").innerHTML="";
                    return;
                }
                if (window.XMLHttpRequest)
                {
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        document.getElementById("chk").innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","user.jsp?q="+str,true);
                xmlhttp.send();
            }
        </script>




Here we are sending our text through url with the help of variable 'str'


and we are doing the database operation in 'user.jsp' for checking the availability.



user.jsp

------------


<%@page language="java" import="java.sql.*" %>
<%! ResultSet rs=null;
    Connection connection;
    Statement statement;
    int f=0;
%>

<%         String value = request.getParameter("q");

           try
           {
               Class.forName("com.mysql.jdbc.Driver").newInstance(); 
               connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","123");
               statement=connection.createStatement();
               rs=statement.executeQuery("select * from login");
               while(rs.next())
               {
                   if(value.equalsIgnoreCase(rs.getString("username")))
                   {
                       f=1;
                   }
                   else
                   {
                       f=0;
                   }
               }
               if(f==1)
               {
                   out.println("User Exist");
               }
               else
               {
                  out.println("User Available");
               }
           }
           catch(SQLDataException e)
           {
               out.println(e);
           }
%>