Tuesday, February 5, 2013

Displaying book details using XML and XSLT


Displaying book details using XML and XSLT


DISPLAYING BOOK DETAILS USING XML AND XSLT


Aim:
         To write XML program to display book details with XSL

Algorithm:
1. Create an XSL page
2. Set styles for an XML file and save the file with extension .xsl
3. Create another file and include XML tags
4. Write code to link the XSL file
5. Save the file with extension .xml
6. Run the XML file
7. Verify the result

Simple.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<books_menu>
<book>
<name>
Computer Architecture </name>
<price> 234.56
</price>
<description>
Computer Is an electronic device
</description>
<pages>456
</pages>
</book>
<book>
<name>
Computer Organisation </name>
<price> 234.56
</price>
<description>
Computer has input and output devices
</description>
<pages>456
</pages>
</book>
</books_menu>

simple.xsl:
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xlmns="http://www.w3.org/1999/xhtml">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="books_menu/book">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold">
<xsl:value-of select="name"/>
</span>
</div>
<div style="margin-left:20px;margin-bottom:1cm;font-size:10pt">
<xsl:value-of select="description"/>
<span style="font-style:italic">
<xsl:value-of select="pages"/>
(pages in book)
</span>
</div>
</xsl:for-each>
</body>
</html>






OUTPUT SCREENSHOTS



Displaying student marklist using Java Server Pages


Displaying student marklist using Java Server Pages



DISPLAYING STUDENT MARK LIST USING JSP

AIM:
      To create a three tier application for displaying student mark list using JSP and database.

ALGORITHM:
1.       Design the HTML page (stud.html) with the following
a)   Create a form to get the input (Register Number) from the user.
b)   Set the URL of the server (marklist.jsp) as the value of the action attribute.
c)   Use submit button to invoke the server and send the form data to the server.
2.       Create the JSP file with the following
a)    Read the parameter value (Register Number) from the form by using the method getParameter().
b)    Server retrieves the details from the database table with respect to the form input.
c)    Server displays the mark list to the client as the response.

marklist.jsp:

<%@ page contentType="text/html" language="java" import="java.sql.*"%>
<html>
<head>
<title>Three Tier Application</title>
<style type="text/css">
  body{color:blue;font-family:courier;text-align:center}
</style>
</head>
<body>
<h2>EXAMINATION RESULT</h2><hr/>
<%
String str=request.getParameter("regno");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:markDS");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT*FROM markTab WHERE rno="+str);
while(rs.next())
{
%>
Register No:<%=rs.getObject(1)%><br/>
Name:<%=rs.getObject(2)%><br/>
<table border="1">
<th>SUBJECT</th><th>Mark</th>
<tr><td>Network Programming and Management</td><td><%=rs.getObject(3)%></td></tr>
<tr><td>Object Oriented Analysis and Design</td><td><%=rs.getObject(4)%></td></tr>
<tr><td>Cryptography and Network Security</td><td><%=rs.getObject(5)%></td></tr>
<tr><td>Embedded Systems</td><td><%=rs.getObject(6)%></td></tr>
<tr><td>Web Technology</td><td><%=rs.getObject(7)%></td></tr>
<tr><td>Software Requirement and Engineering</td><td><%=rs.getObject(8)%></td></tr>
</table>
<%
}
%>
<br/>
<a href="stud.html">Back</a>
</body>
</html>

stud.HTML:

<html>
<head>
<title>Three Tier Application</title>
<style type="text/css">
  body{color:blue;font-family:courier;text-align:center}
</style>
</head>
<body>
<h2>EXAMINATION RESULT</h2><hr/>
<form name="f1" method"GET" action="marklist.jsp">
Enter Your Reg.No:
<input type="text" name="regno"/><br/><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="SUBMIT"/>
</form>
</body>
<html>


DATABASE






OUTPUT SCREENSHOTS



Develop a scientific calculator using even-driven programming paradigm of Java


Develop a scientific calculator using even-driven programming paradigm of Java


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class scientific extends JFrame implements ActionListener
{
JTextField jtx;
double temp,temp1,result,a;
static double m1,m2;
int k=1,x=0,y=0,z=0;
char ch;
JButton
one,two,three,four,five,six,seven,eight,nine,zero,clr,pow2,pow3;
JButton
plus,min,div,lg,rec,mul,eq,plmi,poin,mr,mc,mp,mm,sqrt,sin,cos,tan;
Container cont;
JPanel textPanel,buttonpanel;
scientific()
{
cont=getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel=new JPanel();
Font font=new Font("Arial",Font.PLAIN,18);
jtx=new JTextField(25);
jtx.setFont(font);
jtx.setHorizontalAlignment(SwingConstants.RIGHT);
jtx.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent keyevent)
{
char c=keyevent.getKeyChar();
if(c>='0' && c<='9')
{
}
else
{
keyevent.consume();
}
}
});
textpanel.add(jtx);
buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(5,6));
boolean t=true;

sin=new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
mr=new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
seven=new JButton("7");
buttonpanel.add(seven);
seven.addActionListener(this);
eight=new JButton("8");
buttonpanel.add(eight);
eight.addActionListener(this);
nine=new JButton("9");
buttonpanel.add(nine);
nine.addActionListener(this);
clr=new JButton("AC");
buttonpanel.add(clr);
clr.addActionListener(this);

cos=new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
mc=new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
four=new JButton("4");
buttonpanel.add(four);
four.addActionListener(this);
five=new JButton("5");
buttonpanel.add(five);
five.addActionListener(this);
six=new JButton("6");
buttonpanel.add(six);
six.addActionListener(this);
mul=new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);

tan=new JButton("TAN");
buttonpanel.add(tan);
tan.addActionListener(this);
mp=new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
one=new JButton("1");
buttonpanel.add(one);
one.addActionListener(this);
two=new JButton("2");
buttonpanel.add(two);
two.addActionListener(this);
three=new JButton("3");
buttonpanel.add(three);
three.addActionListener(this);
min=new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);

pow2=new JButton("x^2");
buttonpanel.add(pow2);
pow2.addActionListener(this);
mm=new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
zero=new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);
plmi=new JButton("+/-");
buttonpanel.add(plmi);
plmi.addActionListener(this);
poin=new JButton(".");
buttonpanel.add(poin);
poin.addActionListener(this);
plus=new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);

pow3=new JButton("x^3");
buttonpanel.add(pow3);
pow3.addActionListener(this);
rec=new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
sqrt=new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
lg=new JButton("log");
buttonpanel.add(lg);
lg.addActionListener(this);
div=new JButton("/");
div.addActionListener(this);
buttonpanel.add(div);
eq=new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);


cont.add("Center",buttonpanel);
cont.add("North",textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();

if(s.equals("1"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"1");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"2");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"3");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"3");
z=0;
}
}
if(s.equals("4"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"4");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"4");
z=0;
}
}
if(s.equals("5"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"5");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"5");
z=0;
}
}
if(s.equals("6"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"6");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"6");
z=0;
}
}
if(s.equals("7"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"7");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"7");
z=0;
}
}
if(s.equals("8"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"8");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"8");
z=0;
}
}
if(s.equals("9"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"9");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"9");
z=0;
}
}
if(s.equals("0"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"0");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"0");
z=0;
}
}
if(s.equals("AC"))
{
jtx.setText("");
x=0;
y=0;
z=0;
}
if(s.equals("log"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.log(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("1/x"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=1/Double.parseDouble(jtx.getText());
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("x^2"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),2);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("x^3"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),3);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("+/-"))
{
if(x==0)
{
jtx.setText("-"+jtx.getText());
x=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("."))
{
if(y==0)
{
jtx.setText(jtx.getText()+".");
y=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("+"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='+';
}
else
{
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='+';
y=0;
x=0;
}
jtx.requestFocus();
}
if(s.equals("-"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='-';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='-';
}
jtx.requestFocus();
}
if(s.equals("/"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='/';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='/';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("*"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='*';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='*';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("MC"))
{
m1=0;
jtx.setText("");
}
if(s.equals("MR"))
{
jtx.setText("");
jtx.setText(jtx.getText() + m1);
}
if(s.equals("M+"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1+=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("M-"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1-=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("Sqrt"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sqrt(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("SIN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sin(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("COS"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.cos(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("TAN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.tan(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("="))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
temp1 = Double.parseDouble(jtx.getText());
switch(ch)
{
case '+':
result=temp+temp1;
break;
case '-':
result=temp-temp1;
break;
case '/':
result=temp/temp1;
break;
case '*':
result=temp*temp1;
break;
}
jtx.setText("");
jtx.setText(jtx.getText() + result);
z=1;
}
}
jtx.requestFocus();
}
public static void main(String args[])
{
scientific n=new scientific();
n.setTitle("CALCULATOR");
n.setSize(370,250);
n.setResizable(false);
n.setVisible(true);
}
}
output:

Crate a Popup Menu in Java


Crate a Popup Menu in Java




import javax.swing.*;
import java.awt.event.*;


public class PopUpMenu{
JPopupMenu Pmenu;
JMenuItem menuItem;
public static void main(String[] args) {
PopUpMenu p = new PopUpMenu();
}


public PopUpMenu(){
JFrame frame = new JFrame("Creating a Popup Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pmenu = new JPopupMenu();
menuItem = new JMenuItem("Cut");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Copy");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Paste");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Delete");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Undo");
Pmenu.add(menuItem);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){}
});
frame.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
if(me.isPopupTrigger()){
Pmenu.show(me.getComponent(), me.getX(), me.getY());
}
}
public void mouseReleased(MouseEvent Me){
if(Me.isPopupTrigger()){
Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
}
}
});
frame.setSize(400,400);
frame.setVisible(true);
}
}
output:

Create a ToolBar in Java


Create a ToolBar in Java


import javax.swing.*;
import java.awt.*;

public class CreateToolbar{
public static void main(String[] args) {
JFrame frame = new JFrame("Create a toolbar Which have three buttons Such as: Cut, Copy, Paste");
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton cutbutton = new JButton(new ImageIcon("cut.gif"));
toolbar.add(cutbutton);
JButton copybutton = new JButton(new ImageIcon("copy.gif"));
toolbar.add(copybutton);
JButton pastebutton = new JButton(new ImageIcon("paste.gif"));
toolbar.add(pastebutton);
frame.getContentPane().add(toolbar,BorderLayout.NORTH);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
}
}

Online Eamination using JSP and Databases


Online Eamination using JSP and Databases




Download Source code



Download Database file 


index.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Online Examination</title>
    </head>
    <body bgcolor="cyan">
        <form name="index" action="exam.jsp" method="post">
        <center><h1><span><font color="red">Welcome to Online Examination</font></span></h1>
        <br>
        <h2><u><span><font color="blue">Instructions to the Candidates</font></span></u></h2>
        <br><h3><ol><li>Fill the correct Registration number.</li>
        <br><li>Enter your name.</li>
        <br><li>Read the questions carefully.</li>
        <br><li>No negative marking.</li></ol></h3>
      
        <br>
        <b>Enter your Register number</b>
        <input type="text" name="txt_reg">
        <b>Enter your Name</b>
        <input type="text" name="txt_name"><br><br>
        <input name ="submit" value="Submit" type="submit"/>
        </center>
        </form>
    </body>
</html>


exam.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Examination Panel</title>
    </head>
    <body bgcolor="cyan">
        <%@ page language="java" %>
<%@ page import ="java.sql.*" %>
<%
String reg= request.getParameter("txt_reg");
String name = request.getParameter("txt_name");
out.println("<h2>Welcome"   name   "...Your Register number is "   reg   "!!</h2><br><br><br>");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sTable = "exam";
String sSql = "SELECT * FROM "   sTable   "";
String sDBQ = "C:/Users/A/Documents/NetBeansProjects/Online Examination/exam.mdb";

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="   sDBQ   ";DriverID=22;READONLY=true";
Connection cn = null;
Statement st = null;
ResultSet rs = null;
try {
cn = DriverManager.getConnection( database ,"","");
st = cn.createStatement();
rs = st.executeQuery( sSql );
ResultSetMetaData rsmd = rs.getMetaData();
String s1,s2,s3,s4;
int i=1;
while(rs.next())
{

out.println("<form name='exam' action='report.jsp' method='post'><b>" i " . "   rs.getString(1)   "</b><br><br>");
s1 = rs.getString(2);
s2 = rs.getString(3);
s3 = rs.getString(4);
s4 = rs.getString(5);
out.println("<input type=radio name=opt" i " value=" s1 " CHECKED>"  s1  " <br><br>");
out.println("<input type=radio name=opt" i " value=" s2 ">"   s2  "<br><br>");
out.println("<input type=radio name=opt" i " value=" s3 ">"   s3  "<br><br>");
out.println("<input type=radio name=opt" i " value=" s4 ">"   s4  "<br><br>");
i  ;
}
out.println("<input name ='submit' value='Submit' type='submit'/>");
/*int n = rsmd.getColumnCount();
out.println( "<table border=1 cellspacing=3><tr>" );
for( int i=1; i<=n; i   ) // Achtung: erste Spalte mit 1 statt 0
out.println( "<th>"   rsmd.getColumnName( i )   "</th>" );
while( rs.next() )
{
out.println( "</tr><tr>" );
for( int i=1; i<=n; i   ) // Achtung: erste Spalte mit 1 statt 0
out.println( "<td nowrap>"   rs.getString( i )   "</td>" );
}
out.println( "</tr></table>" );*/
}
  finally {
try { if( null != rs ) rs.close(); } catch( Exception ex ) {}
try { if( null != st ) st.close(); } catch( Exception ex ) {}
try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
}

%>
        
    </body>
</html>

report.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Exam Report</title>
    </head>
    <body bgcolor="cyan">
        <center><h1>Your Report Card</h1></center>
 <%@ page language="java" %>
<%@ page import ="java.sql.*" %>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sTable = "exam";
String sSql = "SELECT * FROM "   sTable   "";
String sDBQ = "C:/Users/A/Documents/NetBeansProjects/Online Examination/exam.mdb";

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="   sDBQ   ";DriverID=22;READONLY=true";
Connection cn = null;
Statement st = null;
ResultSet rs = null;
try {
cn = DriverManager.getConnection( database ,"","");
st = cn.createStatement();
rs = st.executeQuery( sSql );
ResultSetMetaData rsmd = rs.getMetaData();
String s1,s2,s3,s4;
int i=1;
int correct=0,incorrect=0,total=0;
out.println("<h2><br><br><center><table border=1 cellpadding=2 cellspacing=2><tr><th>Question</th><th>Your Answer</th><th>Correct Answer</th><th>Status</th></tr>");
while(rs.next())
{
total  ;
s1 = rs.getString(1);
s2 = request.getParameter("opt" i);
s3 = rs.getString(6);
if(s2.equals(s3))
{
    s4="Correct";
    correct  ;
}
else
{
    s4="Incorrect";
    incorrect  ;
}    
out.println("<tr><td>" s1 "</td><td>" s2 "</td><td>" s3 "</td><td>" s4 "</td></tr>");
i  ;
}
out.println("</table><br><br><table><b><tr><td>Correct Answers</td><td>" correct "</td></tr>");
out.println("<tr><td>Incorrect Answers</td><td>" incorrect "</td></tr>");
out.println("<tr><td>Total Questions</td><td>" total "</td></tr></table></b></center></h2>");

}
  finally {
try { if( null != rs ) rs.close(); } catch( Exception ex ) {}
try { if( null != st ) st.close(); } catch( Exception ex ) {}
try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
}

%>
      
    </body>
</html>





Screenshots


index.jsp


exam.jsp


report.jsp