Anda di halaman 1dari 31

# Example: <html> <head> <script language="javascript"> function pop(){ var major=parseInt(navigator.appVersion); var minor=parseFloat(navigator.appVersion); var agent=navigator.userAgent.toLowerCase(); document.

write("<h1>Details in popup:</h1>"); window.alert(agent+" } </script> </head> <body onLoad="pop()"> </body> </html> "+major);

## 1) Strong Numbers With in the Range

StrongNumber.html
<html> <head> <title>Strong Number</title> </head> <body> <script language="javascript"> var i,f,n,r,sum,temp,limit; limit=parseInt(window.prompt('Enter Limit Number:')); for(n=1;n<=limit;n++) { temp=n; sum=0; while(temp!=0) { i=1; f=1; r=temp%10; while(i<=r) { f=f*i; i++; } sum=sum+f; temp=temp/10;

## 2) Perfect Number upto the Range

PerfectNumber.html
<html> <head> <title>Perfect Numbers</title> </head> <body> <script type="text/javascript" language="javascript" > var i,j,n,s; i=parseInt(window.prompt('Enter starting Position')); n=parseInt(window.prompt('Enter Ending Position')); while(i<=n) { j=1; s=0; while(j<i) { if(i%j==0) { s=s+j; } if(s==i) { document.write(i+' j=0; } ');

Output:

## 3) Discrimination of amount deposit in the bank

Discrimination.html
<html> <head> <title> Discrimination of Amount Deposits in the Bank </title> </head> <body> <table border="1" bgcolor="pink" align="center"> <caption>Discrimination of Amount Deposit Form</caption> <tr> <td width=150 align="right">1000</td> <td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr> <tr> <td width=150 align="right">500</td> <td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr> <tr> <td width=150 align="right">100</td> <td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr> <tr> <td width=150 align="right">50</td>

<td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr><tr> <td width=150 align="right">20</td> <td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr><tr> <td width=150 align="right">10</td> <td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr><tr> <td width=150 align="right">5</td> <td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr><tr> <td width=150 align="right">2</td> <td width=50 align="center">x</td> <td width=200> </tr><tr> <td width=150 align="right">1</td> <td width=50 align="center">x</td> <td width=200>&nbsp</td> </tr> <tr> <td width=150 align="right" colspan="2">Total</td> <td width=200>&nbsp</td> </tr> </td>

Output:

## 4) Prime Numbers upto given Number:

<html> <head> <title>Prime Number</title> </head> <body> <script language="javascript"> var i,j,n,t; n=parseInt(window.prompt('Enter ending limit')); i=2; document.write('<h1>Prime Numbers are: </h1>'); while(i<=n) { j=2; temp=0; while(j<i) { if(i%j==0) { temp=1; } j++; } if(temp==0) document.write(i+' ');

## i++; } </script> </body> </html>

Output:

5) Bubble Sort:
BubbleSort.html
<html> <body> <script type="text/javascript" language="javascript"> { var a=new Array(20); var i,n,j,temp; n=parseInt(window.prompt('enter the range')); for(i=0;i<n;i++) { a[i]=parseInt(window.prompt('enter the number')); } document.write("<h2>Elements before sorting</h2>"); for(i=0;i<n;i++) { document.write(' } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { temp = a[j]; a[j] = a[j+1]; '+a[i]);

a[j+1] = temp; } } } document.write("<h2>elements after sorting</h2>"); for(i=0;i<n;i++) { document.write(' } } </script> </body> </html> '+a[i]);

Output:

## 6) Reads a Number and Prints in Words

Number.html
<html> <head> <title>Reads a Number and Dislays output in Words</title> </head> <body> <script language="javascript"> var a,num,g,w,b,z,y; num=parseInt(window.prompt('Enter a Number')); a=num/1000; if (a == 1) document.write("one thousand"); else if (a == 2) document.write("two thousand"); else if (a== 3) document.write("three thousand"); else if (a == 4) document.write("four thousand"); else if (a == 5) document.write("five thousand"); else if (a == 6) document.write("six thousand"); else if (a == 7) document.write("seven thousand"); else if (a == 8)

document.write("eight thousand"); else if (a == 9) document.write("nine thousand"); y=num % 1000; if (y >= 100 && y < 200) document.write(" one hundred"); else if (y >= 200 && y < 300) document.write(" two hundred"); else if (y >= 300 && y < 400) document.write(" three hundred"); else if (y >= 400 && y < 500) document.write(" four hundred"); else if (y >= 500 && y < 600) document.write(" five hundred"); else if (y >= 600 && y < 700) document.write(" six hundred"); else if (y >= 700 && y < 800) document.write(" seven hundred"); else if (y >= 800 && y < 900) document.write(" eight hundred"); else if (y >= 900 && y < 1000) document.write(" nine hundred"); z=num%100; if (z >= 20 && z < 30) document.write( " twenty"); else if (z >= 30 && z < 40) document.write( " thirty");

else if (z >= 40 && z < 50) document.write( " forty"); else if (z >= 50 && z < 60) document.write( " fifty"); else if (z >= 60 && z < 70) document.write( " sixty"); else if (z >= 70 && z < 80) document.write( " seventy"); else if (z >= 80 && z < 90) document.write( " eighty"); else if (z >= 90 && z < 100) document.write( " ninety"); else if (z >= 10 && z < 20) w = z; if (w == 10) document.write(" ten"); else if (w == 11) document.write(" eleven"); else if (w ==12) document.write(" twelve"); else if (w == 13) document.write(" thirteen"); else if (w == 14) document.write(" fourteen"); else if (w == 15) document.write(" fifteen"); else if (w == 16)

document.write(" sixteen"); else if (w == 17) document.write(" seventeen"); else if (w == 18) document.write(" eighteen"); else if (w == 19) document.write(" nineteen"); if (z < 10 || z >= 20) b = z % 10; if (b == 1) document.write(" one"); else if (b == 2) document.write(" two"); else if (b == 3) document.write(" three"); else if (b == 4) document.write(" four"); else if (b == 5) document.write(" five"); else if (b == 6) document.write(" six"); else if (b == 7) document.write(" seven"); else if (b == 8) document.write(" eight"); else if (b == 9) document.write(" nine");

Output:

## 7) Event Handling on Image

Event.html
<html> <head> <title> Event Handling on an Image </title> <script language="javascript"> function mouseOver() { document.getElementById("skc").src="krish.jpg"; } function mouseOut() { document.getElementById("skc").src="kittu.jpg"; } </script> </head> <body> <img id="skc" src="krish.jpg" onMouseOver="mouseOver()" onMouseOut="mouseOut()"> </body> </html>

Output:

## 9) J.S. Date and Time

DtTi.html
<html> <head> <title> Date and Time </title> </head> <body> <script language="javascript"> var dt=new Date(); document.write(dt); </script> </body> </html>

Output:

10) Convert static webpage to dynamic webpage using Servlets and Cookie
Sampleservlet.java
import java.io.*; importjavax.servlet.*; public class Sampleservlet extends GenericServlet { public void init(ServletConfigconfig)throws ServletException { System.out.println("in init()method"); } public void service(ServletRequestreq,ServletResponse res)throws ServletException,IOException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); out.println("<html><body>"); out.println("<h1>welcome to first servlet</h1>");

out.println(" } }

</body></html>");

Web.xml
<web-app> <servlet> <servlet-name>Ser</servlet-name>

## <servlet-class>Sampleservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Ser</servlet-name> <url-pattern>/se</url-pattern> </servlet-mapping> </web-app>

Output:

import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class CookieRead extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); Cookie ck[] = req.getCookies(); // read required cookie values out.println("<html><body><center>"); out.println("<h1>The Cookie data is</h1"); for(int i=0;i<ck.length;i++) { if(ck[i].getName().equals("ma")) { out.println("<br>"+ck[i].getName()+"--"+ck[i].getValue()); break; } }

out.println("</h1></center></body></html>"); } }

Web.xml
<web-app> <servlet> <servlet-name>Serv</servlet-name> <servlet-class>CookieRead</servlet-class> </servlet> <servlet-mapping> <servlet-name>Serv</servlet-name> <url-pattern>/ss</url-pattern> </servlet-mapping> </web-app>

Output:

## 11) HelloWorld Program using Servlet

HelloServlet.java
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { PrintWriter out=res.getWriter(); res.setContentType("text/html"); out.println("Hello World!"); out.close(); } }

Output:

## 12) Jsp Page to Display Date And Time

DtTi.jsp
<%@page import="java.util.*"%> <html> <body> <h1> Welcome to Date JSP... <br> Current Date is: <%= new Date().toString() %> </h1> </body> </html>

Output:

Hit Counter
HitCounter.jsp
<%@page import="java.io.*,java.util.*"%> <html>

<head> <title>No. of Hits</title> </head> <body> <% Integer hitcount=(Integer)application.getAttribute("hitcounter"); if(hitcount==0) hitcount=1; else hitcount++; out.println("No. of times visited"+hitcount); application.setAttribute("hitcounter"+hitcount); %> </body> </html>

Output:

## 13) Ms-Access DataBase:

Before Database

Delete.java
import java.sql.*; public class Delete { public static void main(String s[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con= DriverManager. getConnection("jdbc:odbc:mydsn"); Statement st=con.createStatement(); String query="delete from stu where sno=2"; int i = st.executeUpdate(query); if(i>0) System.out.println(i+" records deleted successfully.."); else System.out.println("Deletion failed.."); st.close();

con.close(); } }

Output:

Insert.java
import java.sql.*; public class Insert { public static void main(String s[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con= DriverManager. getConnection("jdbc:odbc:mydsn"); Statement st=con.createStatement(); String query="insert into stu values(5,'Srinivas')"; int i = st.executeUpdate(query); if(i>0) System.out.println(i+" records inserted successfully.."); else System.out.println("Insertion failed.."); st.close(); con.close();

} }

Output:

Select.java
import java.sql.*; public class Select { public static void main(String s[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con= DriverManager. getConnection("jdbc:odbc:mydsn"); Statement st=con.createStatement(); String query="select * from stu"; ResultSet rs = st.executeQuery(query); while(rs.next()) System.out.println("SNo: "+rs.getInt(1)+" "+rs.getString(2)); rs.close(); st.close(); con.close(); } } SName:

Output:

Update.java
import java.sql.*; public class Update { public static void main(String s[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con= DriverManager. getConnection("jdbc:odbc:mydsn"); Statement st=con.createStatement(); String query="update stu set sname='Murali' where sno=1"; int i = st.executeUpdate(query); if(i>0) System.out.println(i+" records updated successfully..");

Output:

After Database