Showing posts with label web technology lab. Show all posts
Showing posts with label web technology lab. Show all posts

Wednesday, May 27, 2015

Parsing an XML document using SAX Parser

Aim:
            To create simple SAX parser to parse XML document

Algorithm:

  1. Create a Sax parser and parse the xml
  2. In the event handler create the employee object
  3. Print out the data

Program:

SAXParserExample.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserExample extends DefaultHandler{

            List myEmpls;
           
            private String tempVal;
           
            //to maintain context
            private Employee tempEmp;
           
           
            public SAXParserExample(){
                        myEmpls = new ArrayList();
            }
           
            public void runExample() {
                        parseDocument();
                        printData();
            }

            private void parseDocument() {
                       
                        //get a factory
                        SAXParserFactory spf = SAXParserFactory.newInstance();
                        try {
                       
                                    //get a new instance of parser
                                    SAXParser sp = spf.newSAXParser();
                                   
                                    //parse the file and also register this class for call backs
                                    sp.parse("employees.xml", this);
                                   
                        }catch(SAXException se) {
                                    se.printStackTrace();
                        }catch(ParserConfigurationException pce) {
                                    pce.printStackTrace();
                        }catch (IOException ie) {
                                    ie.printStackTrace();
                        }
            }

            /**
             * Iterate through the list and print
             * the contents
             */
            private void printData(){
                       
                        System.out.println("No of Employees '" + myEmpls.size() + "'.");
                       
                        Iterator it = myEmpls.iterator();
                        while(it.hasNext()) {
                                    System.out.println(it.next().toString());
                        }
            }
           

            //Event Handlers
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                        //reset
                        tempVal = "";
                        if(qName.equalsIgnoreCase("Employee")) {
                                    //create a new instance of employee
                                    tempEmp = new Employee();
                                    tempEmp.setType(attributes.getValue("type"));
                        }
            }
           

            public void characters(char[] ch, int start, int length) throws SAXException {
                        tempVal = new String(ch,start,length);
            }
           
            public void endElement(String uri, String localName, String qName) throws SAXException {

                        if(qName.equalsIgnoreCase("Employee")) {
                                    //add it to the list
                                    myEmpls.add(tempEmp);
                                   
                        }else if (qName.equalsIgnoreCase("Name")) {
                                    tempEmp.setName(tempVal);
                        }else if (qName.equalsIgnoreCase("Id")) {
                                    tempEmp.setId(Integer.parseInt(tempVal));
                        }else if (qName.equalsIgnoreCase("Age")) {
                                    tempEmp.setAge(Integer.parseInt(tempVal));
                        }
                       
            }
           
            public static void main(String[] args){
                        SAXParserExample spe = new SAXParserExample();
                        spe.runExample();
            }
           
}


Employee.java



public class Employee {

            private String name;

            private int age;
           
            private int id;

            private String type;
           
            public Employee(){
                       
            }
           
            public Employee(String name, int id, int age,String type) {
                        this.name = name;
                        this.age = age;
                        this.id  = id;
                        this.type = type;
                       
            }
            public int getAge() {
                        return age;
            }

            public void setAge(int age) {
                        this.age = age;
            }

            public int getId() {
                        return id;
            }

            public void setId(int id) {
                        this.id = id;
            }

            public String getName() {
                        return name;
            }

            public void setName(String name) {
                        this.name = name;
            }


            public String getType() {
                        return type;
            }

            public void setType(String type) {
                        this.type = type;
            }          
           

Tuesday, May 26, 2015

Parsing an XML document using DOM

Aim:
            To create simple DOM parser to parse XML document

Algorithm:

  1. Get a document builder using document builder factory and parse the xml file to create a DOM object
  2. Get a list of employee elements from the DOM
  3. For each employee element get the id, name, age and type. Create an employee value object and add it to the list
  4. At the end iterate through the list and print the employees to verify we parsed it right.

Program:

DomParserExample.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class DomParserExample {
//No generics
            List myEmpls;
            Document dom;


            public DomParserExample(){
                        //create a list to hold the employee objects
                        myEmpls = new ArrayList();
            }

            public void runExample() {
                       
                        //parse the xml file and get the dom object
                        parseXmlFile();
                       
                        //get each employee element and create a Employee object
                        parseDocument();
                       
                        //Iterate through the list and print the data
                        printData();
                       
            }
           
           
            private void parseXmlFile(){
                        //get the factory
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                       
                        try {
                                   
                                    //Using factory get an instance of document builder
                                    DocumentBuilder db = dbf.newDocumentBuilder();
                                   
                                    //parse using builder to get DOM representation of the XML file
                                    dom = db.parse("employees.xml");
                                   

                        }catch(ParserConfigurationException pce) {
                                    pce.printStackTrace();
                        }catch(SAXException se) {
                                    se.printStackTrace();
                        }catch(IOException ioe) {
                                    ioe.printStackTrace();

Monday, May 25, 2015

Simple XML Document with DTD

Aim:
            To write a DTD for a domain specific XML document to validate the XML file.

Algorithm:

  1. Create the XML document using  <?xml version="1.0" encoding="ISO-8859-1"?> tag as the initial tab.
  2. Create another CSS document which displays the xml document details into HTML format on the browser.
  3. Give appropriate style in the CSS document.
  4. Invoke xml file from your browser.


Program:

note.xml

<?xml version="1.0" ?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from    (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<message>Don't forget me this weekend!</message>
</note>



dom.xml

<html>
<body>
<h1>Mailam Engineering Collge</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest()
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
xhttp.open("GET","note.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>

</body>
</html>


Sunday, May 24, 2015

SIMPLE XML DOCUMENT FOR CD SHOP

Aim:
            To create a XML document for the CD-Store

Algorithm:


  1. Create the XML document using  <?xml version="1.0" encoding="ISO-8859-1"?> tag as the initial tab.
  2. Create another CSS document which displays the xml document details into HTML format on the browser.
  3. Give appropriate style in the CSS document.
  4. Invoke xml file from your browser.
Program:

cd_catalog_css.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>
            <CD>
                        <TITLE>Empire Burlesque</TITLE>
                        <ARTIST>Bob Dylan</ARTIST>
                        <COUNTRY>USA</COUNTRY>
                        <COMPANY>Columbia</COMPANY>
                        <PRICE>10.90</PRICE>
                        <YEAR>1985</YEAR>
            </CD>
            <CD>
                        <TITLE>Hide your heart</TITLE>
                        <ARTIST>Bonnie Tyler</ARTIST>
                        <COUNTRY>UK</COUNTRY>
                        <COMPANY>CBS Records</COMPANY>
                        <PRICE>9.90</PRICE>
                        <YEAR>1988</YEAR>
            </CD>
            <CD>
                        <TITLE>Greatest Hits</TITLE>
                        <ARTIST>Dolly Parton</ARTIST>
                        <COUNTRY>USA</COUNTRY>
                        <COMPANY>RCA</COMPANY>
                        <PRICE>9.90</PRICE>
                        <YEAR>1982</YEAR>
            </CD>
            <CD>
                        <TITLE>Still got the blues</TITLE>
                        <ARTIST>Gary Moore</ARTIST>
                        <COUNTRY>UK</COUNTRY>
                        <COMPANY>Virgin records</COMPANY>
                        <PRICE>10.20</PRICE>
                        <YEAR>1990</YEAR>
            </CD>
            <CD>
                        <TITLE>Eros</TITLE>
                        <ARTIST>Eros Ramazzotti</ARTIST>
                        <COUNTRY>EU</COUNTRY>
                        <COMPANY>BMG</COMPANY>
                        <PRICE>9.90</PRICE>
                        <YEAR>1997</YEAR>
            </CD>
            <CD>
                        <TITLE>One night only</TITLE>
                        <ARTIST>Bee Gees</ARTIST>
                        <COUNTRY>UK</COUNTRY>
                        <COMPANY>Polydor</COMPANY>
                        <PRICE>10.90</PRICE>
                        <YEAR>1998</YEAR>
            </CD>
            <CD>
                        <TITLE>Sylvias Mother</TITLE>
                        <ARTIST>Dr.Hook</ARTIST>
                        <COUNTRY>UK</COUNTRY>
                        <COMPANY>CBS</COMPANY>
                        <PRICE>8.10</PRICE>
                        <YEAR>1973</YEAR>
            </CD>
            <CD>
                        <TITLE>Maggie May</TITLE>
                        <ARTIST>Rod Stewart</ARTIST>
                        <COUNTRY>UK</COUNTRY>
                        <COMPANY>Pickwick</COMPANY>
                        <PRICE>8.50</PRICE>
                        <YEAR>1990</YEAR>
            </CD>
            <CD>
                        <TITLE>Romanza</TITLE>
                        <ARTIST>Andrea Bocelli</ARTIST>
                        <COUNTRY>EU</COUNTRY>
                        <COMPANY>Polydor</COMPANY>
                        <PRICE>10.80</PRICE>
                        <YEAR>1996</YEAR>
            </CD>
            <CD>
                        <TITLE>When a man loves a woman</TITLE>
                        <ARTIST>Percy Sledge</ARTIST>
                        <COUNTRY>USA</COUNTRY>
                        <COMPANY>Atlantic</COMPANY>
                        <PRICE>8.70</PRICE>
                        <YEAR>1987</YEAR>
            </CD>
            <CD>
                        <TITLE>Black angel</TITLE>
                        <ARTIST>Savage Rose</ARTIST>
                        <COUNTRY>EU</COUNTRY>
                        <COMPANY>Mega</COMPANY>
                        <PRICE>10.90</PRICE>
                        <YEAR>1995</YEAR>
            </CD>
            <CD>
                        <TITLE>1999 Grammy Nominees</TITLE>
                        <ARTIST>Many</ARTIST>
                        <COUNTRY>USA</COUNTRY>
                        <COMPANY>Grammy</COMPANY>
                        <PRICE>10.20</PRICE>
                        <YEAR>1999</YEAR>
            </CD>
            <CD>
                        <TITLE>For the good times</TITLE>
                        <ARTIST>Kenny Rogers</ARTIST>
                        <COUNTRY>UK</COUNTRY>
                        <COMPANY>Mucik Master</COMPANY>
                        <PRICE>8.70</PRICE>
                        <YEAR>1995</YEAR>
            </CD>

Saturday, May 23, 2015

ONLINE SHOPPING DATABASE APPLICATION

Aim:
            To create an online application using database access.

Algorithm:

  1. Create an ASP page which get details from the user.
  2. Create a database connection using ASP code.
  3. Compare the details of the user with the database.
  4. Get the amount from the user from payment gateway.
  5. Deliver the products the user to his/her address.




Program:


send.asp
<!--Copyright 2001 © by nima -->
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<style>
<!--
.lnk         { text-decoration: none; color: #FFFFFF; font-weight: bold }
-->
</style>
</head>

<body>
<%
 dim view
 view = request.querystring("view")
 if  view = "" then
    view = "view1.xsl"
 end if
%>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1" height="100">
  <tr>
    <td width="33%" colspan="2" height="81">
    <h1>XML Database</h1>
    </td>
    <td width="67%" colspan="2" height="81">&nbsp;</td>
  </tr>
  <tr>
    <td width="16%" align="center" dir="ltr" bgcolor="#336699" height="19">
    <a class="lnk" href="send.asp?view=view1.xsl">View1</a>
    </td>   
    <td width="17%" bgcolor="#336699" align="center" dir="ltr" height="19">
    <a class="lnk" href="send.asp?view=view2.xsl">View2</a></td>
    <td width="17%" bgcolor="#336699" align="center" height="19">&nbsp;</td>
    <td width="50%" bgcolor="#336699" align="center" height="19">&nbsp;</td>
  </tr>
</table>
<p>&nbsp;</p>

<%
    dim xmlhttp,xmlDom,xsldom,Query
    set xmlhttp=createobject("MSXML2.XMLHTTP")
    set xmlDom=createobject("MSXML2.domdocument")
    set xslDom=createobject("MSXML2.domdocument")
   
    xmldom.async=false
    xsldom.async=false

    query = "<main><sql>select * from product</sql></main>"

    xmlhttp.Open "POST","http://localhost/xmlDatabase/datasource.asp", false
    xmlhttp.send query
    if xmlhttp.status <> 200 then
       response.write "Error !!!"
   end if


   
    xmldom.load xmlhttp.responseXML
    xsldom.load server.mappath(".")+"/"+view
    response.write XMLDom.transformNode(XSLDom)
%>

<%
set xmlhttp = nothing
set xmldom= nothing
set xsldom = nothing
%>
</body>

</html>

Friday, May 22, 2015

SIMPLE SERVLET PROGRAM

Aim:
            To write a simple servlet program using HTTP in java.

Algorithm:

  1. Create a servlet program using http.
  2. Set classpath where servlet-api.jar file resides.
  3. Compile the servlet program using javac programname.java
  4. Place the class file …\Tomcat 5.5\webapps\ROOT\WEB-INF\classes\ folder.
  5. modify the web.xml file using your servletClassName.
  6. Invoke the class file using http://localhost:8080/servetClassName from your browser

 Program:

mailamHome.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class mailamHome extends HttpServlet
{
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                   throws ServletException,IOException
  {
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<html>");
    pw.println("<head><title>Hello World</title></title>");
    pw.println("<body>");
    pw.println("<h1>Mailam College of Engineering</h1>");
    pw.println("<h2>An ISO 9001:2000 Certified Institution</h2>");
    pw.println("<h3>Affliated to Anna University</h3>");
    pw.println("</body></html>");
  }
}

Thursday, May 21, 2015

ONLINE BOOK SHOPPING USING JSP OBJECTS

Aim:
            To write an online book shopping application using JSP objects.

Algorithm:

  1. Create home, login, registration, profile, catalog and order html pages.
  2. Create jsp pages which does all business works on the server.
  3. Use appropriate database to store the details of the books.
  4. Create tables to store login details and books details.
  5. Connect the database using odbc.jdbc driver.
  6. Make changes in the control settings to enable database on your local machine.



Program:

Main.html:

<html>
<body bgcolor=”pink”>
<br><br><br><br><br><br>
<h1 align=”center”>>U>ONLINE BOOK STORAGE</u></h1><br><br><br>
<h2 align=”center”><PRE>
<b> Welcome to online book storage.
         Press LOGIN if you are having id
              Otherwise press REGISTRATION
</b></PRE></h2>
<br><br><pre>
<div align=”center”><a href=”/tr/login.html”>LOGIN</a>
 href=”/tr/login.html”>REGISTRATION</a></div></pre>
 </body></html>

Login.html:

<html>
      <body bgcolor=”pink”><br><br><br>
      <form name="myform" method="post" action=/tr1/login.jsp">
      <div align="center"><pre>
      LOGIN ID : <input type="passwors" name="pwd"></pre><br><br>
      PASSWORD : <input type="password" name="pwd"></pre><br><br>
      </div>
      <br><br>
      <div align="center">
        <inputtype="submit"value="ok" onClick="validate()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear">
       </form>
      </body>
      </html>


Reg.html:

       <html>
       <body bgcolor="pink"><br><br>
       <form name="myform" method="post" action="/tr1/reg.jsp">
         <div align="center"><pre>
         NAME        :<input type="text" name="name"><br>
         ADDRESS  :<input type="text" name="addr"><br>

         CONTACT NUMBER  : <input type="text" name="phno"><br>
         LOGIN ID     : <input type="text" name="id"><br>
         PASSWORD : <input type="password" name="pwd"></pre><br><br>
         </div>
         <br><br>
         <div align="center">
           <inputtype="submit"value="ok" onClick="validate()">()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear">
         </form>
         </body>
         </html>   

Profile.html:

        <html>
       <body bgcolor="pink"><br><br>
       <form name="myform" method="post" action="/tr1/profile.jsp">
         <div align="center"><pre>
         LOGIN ID     : <input type="text" name="id"><br>
         </pre><br><br>
         </div>
         <br><br>
         <div align="center">
           <inputtype="submit"value="ok" onClick="validate()">()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear">
         </form>
         </body>
         </html>   

Catalog.html:

       <html>
       <body bgcolor="pink"><br><br><br>
       <form method="post" action="/tr1/catalog.jsp">
         <div align="center"><pre>
         BOOK TITLE : <input type="text" name="title"><br>
         </pre><br><br>
         </div>
         <br><br>
         <div align="center">
           <inputtype="submit"value="ok"                          name=”button1”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<inputtype="reset"value="clear"       name=”button2”>
       </form>
       </body>
       </html>   

Order.html:

       <html>
       <body bgcolor="pink"><br><br><br>
       <form method="post" action="/tr1/order.jsp">
         <div align="center"><pre>
         LOGIN ID         :<input type="text" name="id"><br>
         PASSWORD    : <input type="password" name="pwd"><br>
         TITLE               :<input type="text" name="title"><br>
         NO. OF BOOKS  : <input type="text" name="no"><br>
         DATE               : <input type="text" name="date"><br>
         CREDIT CARD NUMBER : <input type="password" name="cno"><br></pre><br><br>
         </div>
         <br><br>
         <div align="center">
           <input type="submit" value="ok" name=”button1”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear" name=”button2”>
         </form>
         </body>
         </html>   


Login.jsp:
      
       <%
 
          out.println(“<html><body bgcolor=\”pink\”>”);
          String id=request.getParameter(“id”);
          String pwd=request.getParameter(“pwd”);
          Driver d=new oracle.jdbc.driver.OracleDriver();
          DriverManager.registerDriver(d);
          Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
          Statement stmt=con.createStatement();
          String sqlstmt=”select id,password from login where id=”+id+” and password=”+pwd+””;
          ResultSet rs=stmt.executeQuery(sqlstmt);
          int flag=0;
          while(rs.next())
           {
             flag=1;
           }
           if(flag==0)
           {
               out.println(“SORRY INVALID ID TRY AGAIN ID<br><br>”);
               out.println(“ <a href=\”/tr1/login.html\”>press LOGIN to RETRY</a>”);
           }
           else
           {
               out.println(“VALID LOGIN ID<br><br>”);
               out.println(“<h3><ul>”);
               out.println(“<li><ahref=\”profile.html\”><fontcolor=\”black\”>USER PROFILE</font></a></li><br><br>”);

               out.println(“<li><ahref=\”catalog.html\”><fontcolor=\”black\”>BOOKS CATALOG</font></a></li><br><br>”);
               out.println(“<li><ahref=\”order.html\”><fontcolor=\”black\”>ORDER CONFIRMATION</font></a></li><br><br>”);
               out.println(“</ul>”);
            }
            out.println(“<body></html>”);
        %>
         

 

Reg.jsp:

    <%
    out.println(“<html><body bgcolor=\”pink\”>”);
    String name=request.getParameter(“name”);
    String addr=request.getParameter(“addr”);
    String phno=request.getParameter(“phno”);
     String id=request.getParameter(“id”);
     String pwd=request.getParameter(“pwd”);
     int no=Integer.parseInt(phno);
     Driver d=new oracle.jdbc.driver.OracleDriver();
     DriverManager.registerDriver(d);
           Connection con=
DriverManager.getConnection (“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
        Statement stmt=con.createStatement();
        String sqlstmt=”select id from login”;
        ResultSet rs=stmt.executeQuery(sqlstmt);
        int flag=0;
        while(rs.next())
{
       if(id.equals(rs.getString(1)))
          {
                  flag=1;
     }
}
if(flag==1)
{
      out.println(“SORRY LOGIN ID ALREADY EXISTS TRY AGAIN WITH NEW ID <br><br>”);     
      out.println(“<a href=\”/tr1/reg.html\”>press REGISTER to RETRY</a>”);
}
else
{
    Statement stmt1=con.createStatement ();
    stmt1.executeUpdate (“insert into login values (“+name+”,”+addr+”,”+no+”,”+id+”,”+pwd+”)”);
         out.println (“YOU DETAILS ARE ENTERED <br><br>”);
         out.println (“<a href =\”/tr1/login.html\”>press LOGIN to login</a>”);
}
        out.println (“</body></html>”);
%>