Sie sind auf Seite 1von 63

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

1) AIM: Write a program for stack using linked list PROGRAM: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<malloc.h> struct chainnode { int item; chainnode *next; }; class stacklist { private: struct chainnode *top; int listsize; public: stacklist() { listsize=0; } int asizeof() { return(listsize); } int push(int element) { struct chainnode *temp; temp=new chainnode ; if(!temp) return(0); temp->item=element; temp->next=top; top=temp; listsize++; return(1); } int pop() {
Page 1

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


if(top==NULL) { cout<<"\n stack emty"; return 0; } else { struct chainnode *temp; temp=top; top=top->next; temp->next=NULL; cout<<"\nthe poped element is:"<<temp->item; free(temp); listsize--; return(1); } } void display() { int i; struct chainnode *temp; temp=top; for(i=1;i<=listsize;i++) { cout<<"\n item : "<<temp->item; temp=temp->next; } return; } ~stacklist() { int i; struct chainnode *temp; for(i=1;i<=asizeof();i++) { temp=top; top=top->next; free(temp); } } }; void main() {
Page 2

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


stacklist t; char a; int i,p,q; clrscr(); do { cout<<"\n"<<"1.insert"<<"\n"<<"2.delete"<<"\n"<<"3.sizeof list"<<"\n"<<"4.display"<<"\n"; cout<<"enter number u want:"; cin>>i; switch(i) { case 1: cout<<"\nenter the item: "; cin>>q; if(t.push(q)) cout<<"\nsucess"; else cout<<"\nfailed"; break; case 2:if(t.pop()) cout<<"\nsucess"; else cout<<"\nfailed"; break; case 3:cout<<"\nthe size of list is :"<<t.asizeof(); break; case 4:t.display(); break; default:cout<<"\nwrong entry try again"; break; } cout<<"\ndo you want to continue:(y/n)"; cin>>a; }while(a=='y'); getch(); } OUTPUT: 1.insert 2.delete
Page 3

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


3.size of list 4.display enter number u want:1 enter the item: 23 sucess do you want to continue:(y/n)y 1.insert 2.delete 3.size of list 4.display enter number u want:1 enter the item: 40 sucess do you want to continue:(y/n)y 1.insert 2.delete 3.size of list 4.display enter number u want:2 the poped element is:40 sucess do you want to continue:(y/n)y 1.insert 2.delete 3.size of list 4.display enter number u want:3 the size of list is :1 do you want to continue:(y/n)n

Page 4

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

2) AIM: Write a program for queue using linked list PROGRAM: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<malloc.h> struct chainnode { int item; chainnode *next; }; class queuelist { private: struct chainnode *front,*rare; int listsize; public: queuelist() { listsize=0; } int asizeof() { return(listsize); } int insert(int element) { struct chainnode *temp;
Page 5

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


temp= new chainnode ; if(!temp) return(0); if(listsize==0) { temp->item=element; temp->next=NULL; front=temp; rare=temp; listsize++; } else { temp->item=element; temp->next=NULL; front->next=temp; front=temp; listsize++; return(1); } } int deletel() { if( rare==NULL) { cout<<"\n queue emty"; return 0; } else { struct chainnode *temp; temp=rare; rare=rare->next; temp->next=NULL cout<<"\nthe poped element is:"<<temp->item; free(temp); listsize--; return(1); } } void display() { int i; struct chainnode *temp;
Page 6

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


temp=rare; for(i=1;i<=listsize;i++) { cout<<"\n item : "<<temp->item; temp=temp->next; } return; } ~queuelist() { int i; struct chainnode *temp; for(i=1;i<=asizeof();i++) { temp=rare; rare=rare->next; free(temp); } } }; void main() { queuelist t; char a; int i,p,q; clrscr(); do { cout<<"\n"<<"1.insert"<<"\n"<<"2.delete"<<"\n"<<"3.size list"<<"\n"<<"4.display"<<"\n"; cout<<"enter number u want:"; cin>>i; switch(i) { case 1: cout<<"\nenter the item: "; cin>>q; if(t.insert(q)) cout<<"\nsucess"; else cout<<"\nfailed"; break; case 2:if(t.deletel()) cout<<"\nsucess";

of

Page 7

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


else cout<<"\nfailed"; break; case 3:cout<<"\nthe size of list is :"<<t.asizeof(); break; case 4:t.display(); break; default:cout<<"\nwrong entry try again"; break; } cout<<"\ndo you want to continue:(y/n)"; cin>>a; }while(a=='y'); getch(); } OUTPUT: 1.insert 2.delete 3.size of list 4.display enter number u want:1 enter the item: 32 sucess do you want to continue:(y/n)y 1.insert 2.delete 3.size of list 4.display enter number u want:1 enter the item: 67 sucess do you want to continue:(y/n)y 1.insert
Page 8

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


2.delete 3.size of list 4.display enter number u want:2 the poped element is:32 sucess do you want to continue:(y/n)y 1.insert 2.delete 3.size of list 4.display enter number u want:2 the poped element is:67 sucess do you want to continue:(y/n)n

3)AIM: Write a program for implementing Binary search. PROGRAM: #include<iostream.h> #include<conio.h> void main() { int a[20],n,mid,f,l,c,flag=0,x; clrscr(); cout<<"Enter the size of the array:"; cin>>n; cout<<"\nEnter the elements in ascending order:"; for(c=0;c<n;c++) cin>>a[c]; cout<<"\nEnter the searching element:\n"; cin>>x; c=0;f=0;l=n; while((flag==0)&&(c<n)) { mid=(f+l)/2;
Page 9

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


if(a[mid]>x) l=mid-1; else if(a[mid]<x) f=mid+1; else flag=1; c=c+1; } if(flag==1) cout<<"Element is found at "<<mid+1<<"place"; else cout<<"Element is not found......"; getch(); } OUTPUT: Enter the size of the array:5 Enter the elements in ascending order:2 34 56 78 98 Enter the searching element: 78 Element is found at 4 place

Page 10

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

4) AIM: Write a program for mergesort. PROGRAM: #include<iostream.h> #include<conio.h> int a[50]; void merge(int,int,int); void merge_sort(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; merge_sort(low,mid); merge_sort(mid+1,high); merge(low,mid,high); } } void merge(int low,int mid,int high) { int h,i,j,b[50],k; h=low; i=low; j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h++; } else { b[i]=a[j];
Page 11

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


j++; } i++; } if(h>mid) { for(k=j;k<=high;k++) { b[i]=a[k]; i++; } } else { for(k=h;k<=mid;k++) { b[i]=a[k]; i++; } } for(k=low;k<=high;k++) a[k]=b[k]; } void main() { int num,i; clrscr(); cout<<endl<<endl; cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl; cin>>num; cout<<endl; cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl; for(i=1;i<=num;i++) { cin>>a[i] ; } merge_sort(1,num); cout<<endl; cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl; cout<<endl<<endl;

Page 12

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


for(i=1;i<=num;i++) cout<<a[i]<<" "; cout<<endl<<endl<<endl<<endl; getch(); } OUTPUT:

Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]: 5 Now, Please Enter the ( 5 ) numbers (ELEMENTS) [THEN PRESS ENTER]: 23 56 11 9 30 So, the sorted list (using MERGE SORT) will be : 9 11 23 30 56

5) AIM: Write a program for Quick sort PROGRAM: #include<iostream.h> #include<conio.h> #include<process.h> class Qsort { int *a; int n; public: Qsort(int size)
Page 13

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


{ n=size; a=new int[n]; } void read(); void display(); void qsort(int left, int right); }; void Qsort::read() { cout<<"\nEnter elements"; for(int i=0;i<n;i++) cin>>a[i]; } void Qsort::display() { for(int i=0;i<n;i++) cout<<a[i]<<"\t"; } void Qsort::qsort(int left, int right) { int pivot=a[left]; int l=left,r=right; while(l<r) { while(a[r]>=pivot&&l<r)r--; if(l!=r)a[l]=a[r]; while(a[l]<=pivot&&l<r)l++; if(l!=r)a[r]=a[l]; a[l]=pivot; } pivot=l; if(left<pivot)qsort(left,pivot-1); if(right>pivot)qsort(pivot+1,right); } void main() { clrscr(); Qsort q(10); q.read(); cout<<"\nBefore sorting:\n";
Page 14

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


q.display(); q.qsort(0,9); cout<<"\nAfter sorting:\n"; q.display(); getch(); } OUTPUT: Enter elements90 10 80 20 40 50 1 2 3 5 Before sorting: 90 10 80 After sorting: 1 2 3 5 20 10 40 20 50 1 40 2 50 3 80 5 90

Page 15

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


6) AIM: Write a program for Selection sort PROGRAM: #include<iostream.h> #include<conio.h> void main() { int a[20],i,n; void selectionsort(int[],int) clrscr(); cout<<"enter number of elements:\n"; cin>>n; cout<<"enter elements"; for(i=0;i<n;i++) cin>>a[i]; selectionsort(a,n); cout<<"after sorting elements are\n"; for(i=0;i<n;i++) cout<<a[i]<<endl; getch(); } void selectionsort(int x[],int n) { int i,j,temp,mp; for(i=0;i<n-1;i++) { mp=i; for(j=i+1;j<n;j++) { if(a[j]<a[mp]) mp=j; } if(i!=mp) { temp=a[i]; a[i]=a[mp]; a[mp]=temp; } } } OUTPUT:

Page 16

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


Enter number of elements 5 Enter elements 16 12 5 47 2 After sorting elements are 2 5 12 16 47

Page 17

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


7) AIM: Write a program for insertion sort PROGRAM: #include<iostream.h> #include<conio.h> void main() { int i,n,j,p,q; int t[100]; clrscr(); cout<<"Enter how many numbers u want to sort \n"; cin>>n; p=0; cout<<"Enter the values \n"; for(i=0;i<n;i++) cin>>t[i]; for(i=1;i<n;i++) { q=t[i]; j=i-1; do { if(t[j]>q) { t[j+1]=t[j]; j=j-1; if(j<0) break; } else break; }while(1); t[j+1]=q; } cout<<"the sorted order is :\n"; for(i=0;i<n;i++) cout<<t[i]<<"\n"; getch(); } OUTPUT:
Page 18

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

Enter how many numbers u want to sort 5 Enter the values 21 11 90 45 9 the sorted order is : 9 11 21 45 90

8)AIM: Write a program for implementation of prims algorithm PROGRAM: #include<iostream.h> #include<conio.h> void main() { int min=0,pnear[10],n=7,j,p,i,k,l,min1,o; int c[100][100]; int t[100][2]; clrscr(); cout<<"Enter number of nodes:"; cin>>n;
Page 19

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


cout<<"\nEnter the cost for following edge/nIf edge is not there put:-1\n" ; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { cout<<"("<<i+1<<","<<j+1<<") :"; cin>>c[i][j]; c[j][i]=c[i][j]; cout<<"\n"; } t[i][0]=t[i][1]=c[i][i]= -1; } for(i=0;i<n;i++) pnear[i]=0; pnear[0]=-1; for(i=0;i<n-1;i++) { min1=999; for(l=0;l<n;l++) { if(pnear[l]==-1 || c[l][pnear[l]]==-1) { continue; } else { if(c[l][pnear[l]]<min1) { min1=c[l][pnear[l]]; t[i][0]=l; t[i][1]=pnear[l]; } } } min=min+min1; pnear[p=t[i][0]]=-1; for(j=0;j<n;j++) { o=pnear[j]; if(o!=-1 ) { if(c[j][o]==-1 || c[j][p]==-1) { if(c[j][o]==c[j][p]==-1)
Page 20

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


continue; if(c[j][o]==-1) pnear[j]=p; continue; } if(c[j][o]>c[j][p]) pnear[j]=p; } } } cout<<"\nMin cost is :"<<min; cout<<"\n the edges are:"; for(i=0;i<n;i++) { if(t[i][0]==-1) continue; cout<<"\n("<<t[i][0]+1<<","<<t[i][1]+1<<") "; } getch(); } OUTPUT: Enter number of nodes:4 Enter the cost for following edge/nIf edge is not there put:-1 (1,2) :21 (1,3) :-1 (1,4) :4 (2,3) :4 (2,4) :-1 (3,4) :5 Min cost is :13 the edges are: (4,1) (3,4) (2,3)
Page 21

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

9) AIM: Write a program for implementation of BFS (Breadth First Search) PROGRAM: #include<iostream.h> #include<conio.h> class bfs { int v,u,n,i,j,str; int gra[20][20],visited[20]; public: bfs(){} void bfs1(int); void bft(); void input(); int adj(int,int); }; void bfs::input() { cout<<"\nEnter the number of nodes\n"; cin>>n; cout<<"\nEnter the adjcency matrix\n"; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>gra[i][j]; } int bfs::adj(int a,int b) { if(gra[a][b]==1) return 1; else return 0; } void bfs::bft() {
Page 22

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


for(i=1;i<=n;i++) visited[i]=0; for(i=1;i<=n;i++) if(visited[i]==0) bfs1(i); } void bfs::bfs1(int v) { int q[20],front=0,rear=0; u=v; visited[v]=1; cout<<v<<"\t"; while(1) { for(int w=1;w<=n;w++) { if(adj(u,w)==1&&visited[w]==0) { q[rear]=w; rear++; cout<<w<<"\t"; visited[w]=1; } } if(front>rear) return; else { u=q[front]; front++; } } } void main() { clrscr(); bfs b; b.input(); cout<<"BFT is\n"; b.bft(); getch(); } OUTPUT:
Page 23

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

Enter the number of nodes 3 Enter the adjcency matrix 101 101 100 BFT is 1 3 2

Page 24

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


10) AIM: Write a program for implementation of DFS (Depth First Search) PROGRAM: #include<iostream.h> #include<conio.h> class dfs { int i,j,k,n; int gra[20][20],visited[20]; public: dfs(){} void dfs1(int); void dft(); void input(); int adj(int,int); }; void dfs::input() { cout<<"\nEnter the number of nodes\n"; cin>>n; cout<<"\nEnter the adjcency matrix\n"; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>gra[i][j]; } int dfs::adj(int a,int b) { if(gra[a][b]==1) return 1; else return 0; } void dfs::dft() { for(i=1;i<=n;i++) visited[i]=0; for(i=1;i<=n;i++) if(visited[i]==0) dfs1(i); } void dfs::dfs1(int v) { int w; visited[v]=1;
Page 25

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


cout<<v<<"\t"; for(w=1;w<=n;w++) { if(visited[w]==0&&adj(v,w)==1) { dfs1(w); } } } void main() { clrscr(); dfs d; d.input(); cout<<"DFT is\n"; d.dft(); getch(); } OUTPUT: Enter the number of nodes 4 Enter the adjcency matrix 1010 0101 1010 0001 DFT is 1 3 2 4 11)AIM : Write a HTML program to implement list PROGRAM: <html> <head> <title>Mega Shop</title> </head> <body> <h2>Two simple lists</h2> <h3>Products</h3> <ul> <li>Widgets, sizes 2 to 12</li> <li>ThingummyBobs for families and the single person</li>
Page 26

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


</ul> <h3>Deadlines</h3> <ol> <li>Place your orders before 4 pm for next day delivery</li> <li>Order by midnight for next new year</li> </ol> <h3>And a definition list</h3> <dl> <dt><a href="example2.html">Widget</a></dt> <dd>Provided in three sizes <i>small,medium,large</i>,and a range of colors.</dd> <dt><a href="example2.html">Thingummybobs</a></dt> <dd>Just what home needs. Now available in teal and cerise stripes for the new season.</dd> </dl> </body> </html>

Page 27

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

12) AIM: Write a HTML program to implement Timetable PROGRAM: <html> <head>
Page 28

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


<title> M.tech time table</title> </head> <body> <h1> <center>M.tech I year I sem time table </center></h1> <table border="1" align= "center"> <tr> <th></th> <th>9.40AM - 11.20AM</th> <th>11.20AM - 1.00PM</th> <th>1.00PM - 2.00PM</th> <th>2.00PM - 3.40PM</th> <th>3.40PM - 5.20PM</th> </tr> <tr> <th>MOnday</th> <td>HSN</td> <td>APS</td> <td rowspan="5"><center>Lunch</center></td> <td>CSD</td> </tr> <tr> <th>Tuesday</th> <td>IRS</td> <td>JAVA&WT</td> <td colspan="5"><center>JAVA&WT lab</center></td> </tr> <tr> <th>Wednesday</th> <td>IRS</td> <td>JAVA & WT</td> <td>CSD</td> </tr> <tr> <th>Thursday</th> <td colspan="2"><center>APS lab</center></td> <td>ADW</td> </tr> <tr> <th>Friday</th> <td>APS</td> <td>HSN</td> <td>ADM</td> </tr> </table>
Page 29

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


</body> </html>

Page 30

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

Page 31

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


13) AIM: Write a HTML program to implement form. PROGRAM: <html> <head> <title>form creation</title> </head> <body> <center> <form action="orderedlist.html " method="post"> <h1 align="center">Registration form</h1> Username:<input type="text" name="user"><br><br> Password:<input type="text" name="pwd"><br><br> Confirm password:<input type="text" name="pwd1"><br><br> Address:<textarea rows="2" cols="5"></textarea><br> <input type="radio" name="sex" value="male"/>Male <input type="radio" name="sex" value="female"/>Female<br> <input type="checkbox" name="vechile" value="bike"/>I have a bike<br> <input type="checkbox" name="vechile" value="car"/>I have a car<br> <input type="submit" name="submit" value="submit"> <input type="reset" name="submit" value="Reset"<br> </center> </form> </body> </html>

Page 32

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

Page 33

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


14) AIM: Write a HTML program to implement inline style sheets internal style sheets and external style sheets PROGRAM: <html> <head> <title>inlinestylesheets</title> </head> <body > <p style="color:blue;margin-left:20px ;font-family:sans-serif; fontweight:bold"> this passage has an effect of css rules supplied by p selector using style attribute</p> <br> <p>this passage is not under the influence of css rules specified</p> </body> </html>

Page 34

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

Page 35

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


internal style sheets PROGRAM: <html> <head> <title> internalstylesheets</title> <style type="text/css"> h1{font-family:sans-serif;color:green;text-align:center;font-size:30;} p{font-family;font-size:15;font-weight:bold;} body{background:yellow;} </style> </head> <body> welcome <p>this passage has an effect of css rules supplied by p selector in head region</p> <h1>hello</h1> </body> </html>

Page 36

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

Page 37

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


external style sheets PROGRAM: <html> <head> <title> external stylesheets</title> <link rel="stylesheets" type="text/css" href="C:\Documents and Settings\Desktop\labprograms\rules.css"> </head> <body> welcome <p>this passage has an effect of css rules supplied by p selector in head region</p> <h1>hello</h1> </body> </html>

Page 38

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

Page 39

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


15)AIM: Write a JAVASCRIPT program to implement calculator PROGRAM: <html> <head> <title> Calculator </title> <script type="text/javascript"> var a,b,c; function read() { a = parseInt(document.getElementById('num1').value); b = parseInt(document.getElementById('num2').value); verify(); } function verify() { if(a<0 || b<0) { alert("Please enter positive numbers only"); clear(); exit(); } } function add() { read(); c = a+b; document.getElementById('res').value=c; } function sub() { read(); c = a-b; document.getElementById('res').value=c; } function mul() { read(); c = a*b; document.getElementById('res').value=c; }
Page 40

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


function div() { read(); c = a/b; document.getElementById('res').value=c; } function mod() { read(); c = a%b; document.getElementById('res').value=c; } function clear() { document.getElementById('num1').value=""; document.getElementById('num2').value=""; document.getElementById('res').value=""; } </script> </head> <body onload="clear()"> <form method="post"> First Number : <input type='text' id='num1' size='5'/> <br> Second Number : <input type='text' id='num2' size='5'/> <br> Result : <input type='text' id='res' size='5' readonly /> <br> <input <input <input <input <input </form> </body> </html> type='button' type='button' type='button' type='button' type='button' onclick='add()' value='Add' /> onclick='sub()' value='Sub' /> onclick='mul()' value='Mul' /> onclick='div()' value='Div' /> onclick='mod()' value='Mod' />

Page 41

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

Page 42

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


16) AIM: Write a JAVASCRIPT program to check given number is prime or not PROGRAM: <html> <head> <title>Prime Number </title> <script type="text/javascript"> var n; function verify() { if(n<0) { alert("Please enter positive number only"); clear(); exit(); } } function prime() { n = parseInt(document.getElementById('num1').value); verify(); var count=0; for(i=2; i<n; i++) { if(n%i==0) count++; } if(count==0) alert("Given number is prime"); else alert("Given number is not prime"); } function clear() { document.getElementById('num1').value=""; document.getElementById('res').value=""; }
Page 43

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


</script> </head> <body onload="clear()"> <form method="post"> Number : <input type='text' id='num1' size='5'/> <br> <input type='button' onclick='prime()' value='Check Prime?' /> </form> </body> </html>

Page 44

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

17) AIM: Write a Servlet program to access details from a form PROGRAM:
Page 45

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

studentform.html <html> <head> <title>student form</title> </head> <body> <center> <form method="get" action="/staticform/student"> enter student name: <input type=text name=sname><br><br> enter student age: <input type=text name=sage><br><br> <input type="submit" name=send value="send"> </form> </center> </body> StudentEx.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class StudentEx extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter k = res.getWriter(); String name = req.getParameter("sname"); String age = req.getParameter("sage"); k.println("<body bgcolor = cyan>"); k.println("Hello: "+ name ); k.println("your age:"+age); k.println("</body>"); } } web.xml
Page 46

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

<web-app> <servlet> <servlet-name>first</servlet-name> <servlet-class>StudentEx</servlet-class> </servlet> <servlet-mapping> <servlet-name>first</servlet-name> <url-pattern>/student</url-pattern> </servlet-mapping> </web-app>

Page 47

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


OUTPUT:

Page 48

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

Page 49

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


18)AIM: JDBC Standalone application to interact with database and store the employee details. PROGRAM: import java.sql.*; import java.io.*; public class jdbc_1 { public static void main(String s[]) throws Exception{ /* Type 1 driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connecttion con=DriverManager.getConnection("jdbc:odbc:mydsn","scott","ti ger"); */ Class.forName("com.mysql.jdbc.Driver"); Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/kiran","root","pas sword"); String query="insert into employee values(?,?,?)"; // Taking input from console DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter Employee Name"); String ename=dis.readLine(); System.out.println("Enter Employee Number"); String eno=dis.readLine(); System.out.println("Enter Employee salary"); int sal=Integer.parseInt(dis.readLine()); // Getting a Statement PreparedStatement ps=con.prepareStatement(query); ps.setString(1,ename); ps.setString(2,eno); ps.setInt(3,sal); //Execute Query

Page 50

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


int i=ps.executeUpdate(); System.out.println("Query Executed Successfully into Employee Table... no of records inserted are "+i); con.close(); } } Data Base Script Drop table Employee; create table employee(ename varchar(20), eno varchar(20),sal varchar(10));

OUTPUT:

19)AIM:- The Web Application takes the user details from the user, interact with the database and checks with the database user details. If user details exists login to site else display login page again. PROGRAM: <html> <body> <center><h1>MY Company</h1><br><br><br> <form name="form1" method="post" action="login">
Page 51

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


<table> <tr> <td><strong>User Name</strong></td> <td><input type="text" name="uname"></td> </tr> <tr> <td><strong>Password</strong></td> <td><input type="Password" name="pass"></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="Submit" value="Login"> </td> <td align="center"> <input type="reset" name="Submit2" value="Reset"> </tr> </table> </form></center></body></html> import java.sql.*;

public class DBClass { public boolean validate(String uname,String pass){ try{ /* Type 1 driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connecttion con=DriverManager.getConnection("jdbc:odbc:myds n","scott","tiger"); */ Class.forName("com.mysql.jdbc.Driver"); Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/kiran","root","pas sword");
Page 52

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


Statement st= con.createStatement(); ResultSet rs=st.executeQuery("select * from userdetails where uname=\'" +uname+ "\'and pass=\'" +pass+ "\' "); return rs.next(); } catch(Exception e){ e.printStackTrace(); } return false; } }

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends HttpServlet { DBClass ud; public void init(){ud=new DBClass();} public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); String uname=request.getParameter("uname"); String pass=request.getParameter("pass"); ud=new DBClass(); if(ud.validate(uname,pass)){ out.println("<h3>Welcome, "+uname+"<h3><br/>"); out.println(" You have logged into the Site"); } else{ out.println("<h2>Your Details not found please login with valid details...</h2>"); out.println("</body></html>"); } } }
Page 53

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

Web.xml:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'> <web-app> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>Login.html</welcome-file> </welcome-file-list> </web-app>

Page 54

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

20)AIM: JSP Pages to take input from the User redirect to Another JSP to Print name and Date PROGRAM:
Page 55

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


<%@page import="java.util.*"%> <html> <head> <title>Welcome</title> </head> <body> <%=new Date().toString()%> <hr> <h2>Welcome</h2> <form action="show.jsp"> User Name <input type=text name="name"><br><br> City <input type=text name=city><br><br> <input type="submit" name= "submit "value="submit"> <input type="reset" name="Submit2" value="Reset"> </form> </center> </body> Show.jsp <%@page import="java.util.*"%> <html> <head> <title>Welcome</title> </head> <body> <%=new Date().toString()%> <hr>Hello<b><%=request.getParameter("name")%></b><br><br> Your City is <b><%=request.getParameter("city")%></b><br><br> <a href="user.jsp">back</a> </form> </center> </body>

Page 56

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

Page 57

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

21)AIM:Web Application take Student detail from the User and Displays the Class . if student marks are >=60 then First Class if >=50 second class if less then 50% then fail.
Page 58

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


PROGRAM: Student Form <html> <head> <title>Student Form</title> </head> <body> <center><h2>Enter Student Details</h2> <form method="post" action="student"> Student Name: <input type=text name=sname><br><br> Student Age: <input type=text name=sage><br><br> Sub1 marks <input type=text name=s1><br><br> Sub2 marks <input type=text name=s2><br><br> Sub3 marks <input type=text name=s3><br><br> <input type="submit" name= "submit "value="submit"> <input type="reset" name="Submit2" value="Reset"> </form> </center> </body> StudentEx.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class StudentEx extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); String name = req.getParameter("sname");
Page 59

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB


String age = req.getParameter("sage"); int percent=0; int s1 = Integer.parseInt(req.getParameter("s1")); int s2 = Integer.parseInt(req.getParameter("s2")); int s3 = Integer.parseInt(req.getParameter("s3")); int total=s1+s2+s3; percent=(total*100)/300; String msg=" "; if(percent>=100) msg="Enter Valid marks"; else if(percent>=60) msg="Good Work You Got First Class"; else if(percent>=50) msg=" You Got second Class"; else msg="Oh You Failed... Work Hard Next time.."; out.println("<html>"); out.println("<body bgcolor = cyan>"); out.println("<h1>Hello: "+ name+"</h1>" ); out.println("<h2>"+msg+"<h2>"); out.println("</body>"); out.println("</html>"); } } Web.xml <web-app> <servlet> <servlet-name>s1</servlet-name> <servlet-class>StudentEx</servlet-class> </servlet> <servlet-mapping> <servlet-name>s1</servlet-name> <url-pattern>/student</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>studentform.html</welcome-file> </welcome-file-list>
Page 60

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

</web-app>

Page 61

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

Page 62

ADVANCED PROBLEM SOLVING AND WEB TECHNOLOGIES LAB

Page 63

Das könnte Ihnen auch gefallen