JAVA Practical Book

                                                                                                                    
Title :- Write a multithreading program in java to display all the vowels from a given String.(Use Thread Class)
import java.lang.*;
import java.util.*;
class Vowels extends Thread
{
            String s1;
            Vowels(String s)
            {           s1=s;
                        start();
            }
            public void run()
            {
                        System.out.println("Vowels are  ");
                        for(int i=0;i<s1.length();i++)
                        {
                                    char ch=s1.charAt(i);
                        if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
                                                System.out.print(" "+ch);
                        }
            }
}
public class Demo1 {
    public static void main(String[] args)
    {
            Scanner sn=new Scanner(System.in);
            System.out.println("Enter a string");
            String str1=sn.next();
            Vowels v=new Vowels(str1);
   } 
}




















                                                                                                                 
Title :- Write a java program to simulate traffic signal using multithreading.
import java.awt.*;
/*<applet code="signal.class" height=450 width=350>
</applet>*/
public class signal extends java.applet.Applet implements Runnable
{    
      Thread t;
       int r,g1,y,i;
    public void init()
    {      
            t=new Thread(this);
             t.start();
            r=0; g1=0; i=0; y=0;    
    }
    public void run()
    {
   try{  
          for(i=24;i>=1;i--)
         {                        
            if (i>16&&i<=24)  {           t.sleep(200);
                                                       r=1;
                                                      repaint();
                                            }
            if (i>8&&i<=16)    {            t.sleep(200);
                                                         y=1;
                                                         repaint();
                                             }
             if(i>1&&i<=8)         {            t.sleep(200);
                                                          g1=1;
                                                          repaint();
                                              }
        }
     if (i==0){
                        run();
                  }
            }catch(Exception e){ }
    }
    public void paint(Graphics g)
    {
          g.drawRect(100,100,100,300);
        if (r==1)  {        
        g.setColor(Color.red);
                                g.fillOval(100,100,100,100);
                                g.setColor(Color.black);
                                g.drawOval(100,200,100,100);
                                g.drawOval(100,300,100,100);
                               r=0;
                          }
         if (y==1)    {                  
       g.setColor(Color.black);
                                            g.drawOval(100,100,100,100);
                                            g.drawOval(100,300,100,100);
                                             g.setColor(Color.yellow);
                                            g.fillOval(100,200,100,100);
                                           y=0;
                               }     
        if (g1==1)   {                g.setColor(Color.black);
                                           g.drawOval(100,100,100,100);
                                          g.drawOval(100,200,100,100);
                                            g.setColor(Color.green);
                                             g.fillOval(100,300,100,100);
                                           g1=0;
                        }
            }
}






































                                                                                                               
Title :- Write a MultiThreading program in java using Runnable interface to draw temple flag on an applet container.
import java.awt.*;
import java.applet.*;
/* <APPLET     code= "flag.class"  width= "500" height= "300">
     </APPLET> */
public class flag extends Applet implements Runnable
{
            Thread t;
            int x1,x2,x3,y3,x4,y4,x5,ln;
            public void init()
            {
                        t=new Thread(this);
                        t.start();
                        ln=1;
            }
            public void run()
            {
            try{      if(ln==1) {         for(x1=200;x1>100;)
                                               {
                                                       t.sleep(200);
                                                       repaint();
                                                }
                                       }
                        ln=2;
                        if(ln==2) {        for(x2=100;x2<150;)
                                                {
                                                          t.sleep(200);
                                                          repaint();
                                                }
                                      }
                        ln=3;
                        if(ln==3) {       for(x3=150,y3=100;x3>125&&y3<125;)
                                               {
                                                      t.sleep(200);
                                                      repaint();
                                              }
                                          }
                        ln=4;
                        if(ln==4) {     for(x4=125,y4=125;x4<150&&y4<150;)
                                            {
                                                t.sleep(200);
                                                repaint();
                                            }
                                       }
                        ln=5;
                        if(ln==5)  {     for(x5=150;x5>100;)
                                             {
                                                t.sleep(200);
                                                repaint();
                                              }
                                        }
                        ln=1;
            }catch(Exception e){
                                                      System.out.println(e);
                                             }
            run();  
            }
            public void paint(Graphics g)
            {
                        if(ln==1&&x1>100)
                        {
                                    g.drawLine(100,200,100,x1-=5);
                        }
                        if(ln==2&&x2<150)
                        {
                                    g.drawLine(100,200,100,100);
                                    g.drawLine(100,100,x2+=5,100);
                        }
                        if(ln==3&&x3>125&&y3<125)
                        {
                                    g.drawLine(100,200,100,100);
                                    g.drawLine(100,100,150,100);
                                    g.drawLine(150,100,x3-=5,y3+=5);
                        }
                        if(ln==4&&x4<150&&y4<150)
                        {
                                    g.drawLine(100,200,100,100);
                                    g.drawLine(100,100,150,100);
                                    g.drawLine(150,100,125,125);
                                    g.drawLine(125,125,x4+=5,y4+=5);
                        }
                        if(ln==5&&x5>100)
                        {
                                    g.drawLine(100,200,100,100);
                                    g.drawLine(100,100,150,100);
                                    g.drawLine(150,100,125,125);
                                    g.drawLine(125,125,150,150);
                                    g.drawLine(150,150,x5-=5,150);
                        }                      
            }
}






                                                                                                               
Title :- Write a Multithreading program using Runnable interface to blink Text on the frame.
import java.awt.*;
import java.awt.event.*;
public class BlinkText extends Frame implements Runnable
{
            Thread t;
            Label l1;
            int f;
            public BlinkText()
            {
                        t=new Thread(this);
                        t.start();
                        setLayout(null);
                        l1=new Label("Hello JAVA");
                        l1.setBounds(100,100,100,40);
                        add(l1);
                        setSize(300,300);
                        setVisible(true);
                        f=0;
            }
            public void run()
            {
                        try
                        {
                                    if(f==0)
                                    {
                                                t.sleep(200);
                                                l1.setText("");
                                                f=1;
                                    }
                                    if(f==1)
                                    {
                                                t.sleep(200);
                                                l1.setText("Hello Java");
                                                f=0;
                                    }
                        }catch(Exception e)
                        {
                                    System.out.println(e);
                        }
                        run();
            }
            public static void main(String args[])
            {
                        new BlinkText();
            }
}




                                                                                                               
Title :- Write a Multithreading program in java using Runnable interface to move text on the frame as follow:

                                                                                                                                                     

Starting Position of Text                                                                                                         [25]


import java.awt.*;
import java.awt.event.*;
class MoveText extends Frame implements Runnable
{           Label l1;
            Thread t;
            int x,y,side;
            public MoveText()
            {
                        setLayout(null);
                        l1=new Label(" Hello Java");
                        l1.setFont(new Font("",Font.BOLD,14));
                        l1.setForeground(Color.red);
                        setSize(400,400);
                        setVisible(true);
                        t=new Thread(this);
                        t.start();
                        x=5;  y=200; side=1;
                        addWindowListener(new WindowAdapter()
                                                {
                                                            public void windowClosing(WindowEvent we)
                                                            {         System.exit(0);        }
                                                });
            }
public void run()
{        try
              {
                        if(side==1){      t.sleep(50);
                                                l1.setBounds(x+=5,y-=5,80,15);
                                                add(l1);
                                                if(y==20)
                                                side=2;
                                          }
                        if(side==2){      t.sleep(50);
                                                l1.setBounds(x+=5,y+=5,80,15);
                                                add(l1);
                                                if(y==200)
                                                side=3;
                                         }
                        if(side==3){      t.sleep(50);
                                                l1.setBounds(x-=5,y+=5,80,15);
                                                add(l1);
                                                if(y==390)
                                                side=4;
                                         }
                        if(side==4){      t.sleep(50);
                                                l1.setBounds(x-=5,y-=5,80,15);
                                                add(l1);
                                                if(x==0){    side=1;  x=0;  y=200;
                                                              }
                                        }
                        }catch(Exception e)
                           {           
                                       System.out.println(e);          
                           }
                        run();
            }
            public static void main(String args[])
            {
                         new MoveText();
             }
}




















                                                                                                               
Title :- Write a Multithreading program in java for bouncing ball. For each bounce, Change the color of ball randomly.
import java.awt.*;
/*<applet code="BounsingBall.class" height=400 width=350></applet>*/
public class BounsingBall extends java.applet.Applet implements Runnable
    Thread t;
    int f,y,f1,f2,f3;
    public void init()
    {        
             t=new Thread(this);
             t.start();
            f=0;  y=0;  f1=0;          
    }
    public void run()
    {   try{         
            if (f==0){      t.sleep(10);
                                 y=y+5;
                                 repaint();
                                 if(f1==6)
                                f1=0;
                           }
            if(f==1) {   t.sleep(10);
                               y=y-5;
                              repaint();
                             if(f1==6)
                              f1=0;
                          }
            }catch(Exception e){ }
            run();  
    }
    public void paint(Graphics g)
    {
        if(f==0) { 
                         if(f1==1)
                        g.setColor(Color.green);
                        if(f1==2)
                        g.setColor(Color.blue);
                        if(f1==3)
                        g.setColor(Color.red);
                        if(f1==4)
                        g.setColor(Color.yellow);
                        if(f1==5)
                        g.setColor(Color.orange);
                        g.fillOval(150,y+10,20,20);
                        if(y==400)
                         {        
                                 f1++;  f=1;       
                           }                   
                   }
        if(f==1) {
                         if(f1==1)
                        g.setColor(Color.green);
                        if(f1==2)
                        g.setColor(Color.blue);
                         if(f1==3)
                         g.setColor(Color.red);
                        if(f1==4)
                        g.setColor(Color.yellow);
                       if(f1==5)
                       g.setColor(Color.orange);
                       g.fillOval(150,y-10,20,20);
                        if(y==0)
                         {           
                                 f1++; f=0;           
                          }
                   }
      }
}



















                                                                                                                        
Title :- Write a Multithreading program in java to convert smile face into the crying face after 5 seconds and vice versa(Use Applet).
import java.awt.*;
/* <applet code="Smileface.class" height=400 width=350>
</applet> */
public class Smileface extends java.applet.Applet implements Runnable
{
    Thread t;
    int f;
    public void init()
    {
         t=new Thread(this);
         t.start();
          f=0;
    }
 public void run()
    {
       try{
            if (f==0)
            {
                        t.sleep(1000);
                        f=1;
                        repaint();
            }
            else
            {
                        t.sleep(1000);
                        repaint();
                        f=0;
            }
            }catch(Exception e){ }
            run();
       }
    public void paint(Graphics g)
    {
        g.drawOval(100,100,100,100);
        g.fillOval(120,125,20,20);
        g.fillOval(160,125,20,20);
        g.drawLine(150,125,150,150);
        if (f==0) {
                         g.drawArc(130,135,40,40,0,-180);
                        f=1;
                       }
            else
                      {
                        g.drawArc(130,170,40,40,0,180);
                        f=0;
                       }
     }
}
                                                                                                                    
Title :- Write a JDBC program to remove “percentage” column from student (rno, sname, percentage) table
import java.io.*;
import java.sql.*;
public class RemoveColumn
{           static Connection cn;
            static Statement st;
            static ResultSet rs;
            public static void main(String args[])
            {           try{
                                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
                                    st=cn.createStatement();
                                    rs=st.executeQuery("select * from student");
                                    while(rs.next())
                                    {
                        System.out.println(rs.getInt("rno")+"\t"+rs.getString("sname")+"\t"+rs.getInt("percentage"));                               }
                                    String str="alter table student drop column percentage";
                                    st.executeUpdate(str);
                                    System.out.println("Column is remove");
                                    rs=st.executeQuery("select * from student");
                                    while(rs.next()) {
                                                                     System.out.println(rs.getInt("rno")+"\t"+rs.getString("sname"));
                                                                  }
                                    cn.close();       
                        }catch(Exception e){
                                                               System.out.println(e);
                                                           }
            }
}



















                                                                                                             
Title :- Write a java program to create Teacher table(TNo.TName, Sal, Desg) and insert a record in it.      
import java.io.*;
import java.sql.*;
public class CreateTeacherTable
{            static Connection cn;
            static Statement st;
            public static void main(String args[])
            {   
                 try
                 {       
                          int tno,sal;
                        String tname,desg;
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
                        st=cn.createStatement();
                       String str="create table Teacher(TNo number,TName varchar(20),Sal number,Desg varchar(20))";
                        st.executeUpdate(str);
                        System.out.println("Table Created");
                        System.out.println("Enter Tno");
                        tno=Integer.parseInt(br.readLine());
                        System.out.println("Enter Tname");
                        tname=br.readLine();
                        System.out.println("Enter Sal");
                        sal=Integer.parseInt(br.readLine());
                        System.out.println("Enter Desg");
                        desg=br.readLine();
                        st.executeUpdate("insert into Teacher values("+tno+",'"+tname+"',"+sal+",'"+desg+"')");
                        System.out.println("Record added successfully");
                        cn.close();       
                        }catch(Exception e)
                               {            
                                      System.out.println(e);           
                               }
            }
}








                                                                                                               
Title :- Write a JDBC program to delete the records of employees whose names are starting with ‘A’ character.
import java.sql.*;
public class DeleteEmployeeRecord
{           static Connection cn;
            static Statement st;
            public static void main(String args[])
            {     try {
                                    ResultSet rs,rs1;
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    cn=DriverManager.getConnection("jdbc:odbc:emp1");
                                    st=cn.createStatement();
                                    System.out.println("\nBefore deleting records are :");
                                    rs=st.executeQuery("select * from employees");
                                    System.out.println("\nEno \t  Ename\t Sal \n");
                                    while(rs.next())
                                    {
                                    System.out.println(rs.getInt("eno")+"\t"+rs.getString("ename")+"\t"+rs.getInt("sal"));
                                    }
                                    st.executeUpdate("delete from employees where ename like 'A%'");
                                    System.out.println("\nAfter deleting records are :");
                                    rs1=st.executeQuery("select * from employees");
                                    System.out.println("\nEno \t  Ename\t Sal \n");
                                    while(rs1.next())
                                  {
                                    System.out.println(rs1.getInt("eno")+"\t"+rs1.getString("ename")+"\t"+rs1.getInt("sal"));
                                    }
                                    cn.close();
                        }catch(Exception e)
                             {      
                                     System.out.println(e);        
                             }
            }
}










                                                                                                               
Title :- Write a JDBC application using swing for the following:


Type DDL Query
 
       
                                                                                                                                                    




Create Table
 

Alter Table
 

Drop Table
 
 





import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class DDL extends JFrame implements ActionListener
{
            JLabel l1;
            JButton b1,b2,b3;
            JTextArea t1;
            DDL()
            {
                        setLayout(null);
                        l1=new JLabel("Type DDL Query");
                        b1=new JButton("Create Table");
                        b2=new JButton("Alter Table");
                        b3=new JButton("Drop Table");
                        t1=new JTextArea();
                        l1.setBounds(20,30,100,20);
                        t1.setBounds(150,30,250,150);
                        b1.setBounds(10,250,120,20);
                        b2.setBounds(130,250,120,20);
                        b3.setBounds(250,250,120,20);
                        add(l1);
                        add(t1);
                        add(b1);
                        add(b2);
                        add(b3);
                        b1.addActionListener(this);
                        b2.addActionListener(this);
                        b3.addActionListener(this);
                        setSize(440,350);
            }
            public void actionPerformed(ActionEvent ae)
            {
                        if(ae.getSource()==b1)
                        {
                                    try{
                                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                                Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
                                                Statement st=cn.createStatement();
                                                String str=t1.getText();
                                                st.executeUpdate(str);
                                                JOptionPane.showMessageDialog(null,"Table created");
                                                cn.close();
                                    }catch(Exception e)
                                    {
                                                System.out.println(e);
                                    }
                        }
                        if(ae.getSource()==b2)
                        {
                                    try{
                                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                                Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
                                                Statement st=cn.createStatement();
                                                String str1=t1.getText();
                                                st.executeUpdate(str1);
                                                JOptionPane.showMessageDialog(null,"Table altered");
                                                cn.close();
                                    }catch(Exception e)
                                    {
                                                System.out.println(e);
                                    }
                        }
                        if(ae.getSource()==b3)
                        {
                                    try{
                                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                                Connection cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
                                                Statement st=cn.createStatement();
                                                String str1=t1.getText();
                                                st.executeUpdate(str1);
                                                JOptionPane.showMessageDialog(null,"Table droped");
                                                cn.close();
                                    }catch(Exception e)
                                    {
                                                System.out.println(e);
                                    }
                        }
            }
            public static void main(String args[])
            {
                        new DDL().show();
            }
}



                                                                                                                        
Title :- Write a java program to create a student table with field’s rno, name and per. Insert values in the table. Display all the details of the student on screen. (Use PreparedStatement Interface)
import java.io.*;
import java.sql.*;
public class CreateStudentTable
{           static Connection cn;
            static Statement st;
            static PreparedStatement ps;
            static ResultSet rs;
            public static void main(String args[])
            {         try{
                                    int rno,per;
                                    String name;
                                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
                                    String str="create table Stud(rno number,name varchar(20),per number)";
                                    ps=cn.prepareStatement(str);
                                    ps.executeUpdate();
                                    System.out.println("\nStudent Table Created");
                                    System.out.println("\n Howmany Records you want to insert?");
                                    int n=Integer.parseInt(br.readLine());
                                    System.out.println("\nEnter "+n+" Records \n");
                                    for(int i=0;i<n;i++)
                                    {
                                                System.out.println("\nEnter ");
                                                rno=Integer.parseInt(br.readLine());
                                                System.out.println("Enter Student name");
                                                name=br.readLine();
                                                System.out.println("Enter per");
                                                per=Integer.parseInt(br.readLine());
                                                ps=cn.prepareStatement("insert into Stud values(?,?,?)");
                                                ps.setInt(1,rno);
                                                ps.setString(2,name);
                                                ps.setInt(3,per);
                                                ps.executeUpdate();
                                    }
                                    System.out.println("\nAll Records are: \n");
                                    st=cn.createStatement();
                                    rs=st.executeQuery("select * from Stud");
                                    System.out.println("RollNo \t Name \t Perc.");
                                    while(rs.next())
                                    {
                                    System.out.println(rs.getInt("rno")+"\t"+rs.getString("name")+"\t"+rs.getInt("per"));                                 }
                                    cn.close();       
                        }catch(Exception e){
                                                            System.out.println(e);
                                                        }
            }
}
.                                                                                                                        
Title :- Write a SERVLET program which counts how many times a user has visited a web page. If user is visiting the page for the first time, display a welcome message. If the user is revisiting the page, display the number of times visited. (Use Cookie)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class VisitServlet extends HttpServlet
{
    static int i=1;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String k=String.valueOf(i);
        Cookie c = new Cookie("visit",k);
        response.addCookie(c);
        int j=Integer.parseInt(c.getValue());
        if(j==1)
        {
            out.println("Welcome");
        }
        else
        {
            out.println("You visited "+i+" times");
        }
                i++;                       
    }
}


Web.xml file(servlet entry)
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
        <servlet-name>VisitServlet</servlet-name>
        <servlet-class>VisitServlet</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>VisitServlet</servlet-name>
        <url-pattern>/servlet/VisitServlet</url-pattern>
    </servlet-mapping>
</web-app>




                                                                                                                
Title :- Write a SERVLET application to accept username and password, search them into database, if found then display appropriate message on the browser otherwise display error message.
UserPass.html
<html>
<body>
<form method=post action="http://localhost:4141/Program/servlet/UserPass">
User Name :<input type=text name=user><br><br>
Password :<input type=text name=pass><br><br>
<input type=submit value="Login">
</form>
</body>
</html>

UserPass.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class UserPass extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
            PrintWriter out = response.getWriter();
            try{
                        String us=request.getParameter("user");
                        String pa=request.getParameter("pass");
                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        Connection cn=DriverManager.getConnection("jdbc:odbc:dsn2","","");
                        Statement st=cn.createStatement();
                        ResultSet rs=st.executeQuery("select * from UserPass");
                        while(rs.next())
                      {
                        if(us.equals(rs.getString("user"))&&pa.equals(rs.getString("pass")))
                        out.println("Valid user");
                        else
                        out.println("Invalid user");
                      }
                }catch(Exception e)
                  {     
                      out.println(e);           
                  }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        doGet(request, response);
    }
}

Web.xml file(servlet entry)
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
        <servlet-name>UserPass</servlet-name>
        <servlet-class>UserPass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserPass</servlet-name>
        <url-pattern>/servlet/UserPass</url-pattern>
    </servlet-mapping>
</web-app>






















                                                                                                                        
Title :  Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form. (Use PreparedStatement interface)
SaveAccount.html
<html><body>
<form method=get action="saveAccount.jsp">
Enter Account No. : <input type=text name=ano><br><br>
Enter Account Type:<input type=text name=type><br><br>
Enter Balance : <input type=text name=bal><br><br>
<input type=submit value="Save">
</form>
</body></html>

saveAccount.jsp
<html><body>
<%@ page import="java.sql.*;" %>
<%! int ano,bal;
       String type;  %>
<%
      ano=Integer.parseInt(request.getParameter("ano"));
      type=request.getParameter("type");
      bal=Integer.parseInt(request.getParameter("bal"));
      try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn=DriverManager.getConnection("jdbc:odbc:acnt","","");
            PreparedStatement s=cn.prepareStatement("insert into Account values(?,?,?)");
            s.setInt(1,ano);
            s.setString(2,type);
            s.setInt(3,bal);
            s.executeUpdate();
            out.println("Record is saved");
            Statement st=cn.createStatement();
            ResultSet rs=st.executeQuery("select * from Account");
%>
<table border="1" width="40%">
<%      while(rs.next())
            {
%>
<tr> <td><%= rs.getInt("ano") %></td>
            <td><%= rs.getString("type") %></td>
            <td><%= rs.getInt("bal") %></td>
</tr>
<%
            }
            cn.close();
          }catch(Exception e)
            {      
                   out.println(e);      
            }
%>
</body></html>
                                                                                                                        
Title :  Write a JSP program to calculate sum of first and last digit of a given number. Display sum in Red Color with font size 18.
Number.html
<html>
<body>
<form method=get action="Number.jsp">
Enter Any Number : <Input type=text name=num><br><br>
<input type=submit value=Calculate>
</form>
</body>
</html>

Number.jsp
<html>
<body>
<%! int n,rem,r; %>
<% n=Integer.parseInt(request.getParameter("num"));
      if(n<10)
     {
       out.println("Sum of first and last digit is   ");
%><font size=18 color=red><%= n %></font>
<%
     }
    else
    {
      rem=n%10;
      do{
                 r=n%10;
                 n=n/10;
            }while(n>0);
         n=rem+r;
        out.println("Sum of first and last digit is    ");
%><font size=18 color=red><%= n %></font>
<%
     }
%>
</body>
</html>







                                                                                                                        
Title : Write a JSP script to accept UserName and his NickName through html page and then displays username when visit count to page is odd and displays nickname when the visit count to the page is even. 
VisitPage.html
<html>
<body>
<form method=get action="VisitPage.jsp">
Enter User Name : <input type=text name=uname><br><br>
Enter Nick Name : <input type=text name=nname><br><br>
<input type=submit value="visit">
</form>
<body>
</html>

VisitPage.jsp
<html>
<body>
<%!
       int cnt=0;
       String uname,nname;
%>
<%
      uname=request.getParameter("uname");
       nname=request.getParameter("nname");
       cnt++;
      if(cnt%2==0)
       out.println("Hello "+nname+".........    Visit Count   "+cnt);
      else
       out.println("Hello "+uname+".........    Visit Count   "+cnt);
%>
<br><br>
<a href="VisitPage.html">VISIT</a>
</body>
</html>

















                                                                                                                        
Title :- Create a JSP page to accept a number from an user and display it in words:
Example: 123 – One Two Three. The output should be in red color.

NumberWord.html
<html>
<body>
<form method=get action="NumberWord.jsp">
Enter Any Number : <input type=text name=num><br><br>
<input type=submit value="Display">
</form>
<body>
</html>

NumberWord.jsp
<html>
<body>
<font color=red>
<%! int i,n;
       String s1;
%>
<%   s1=request.getParameter("num");
         n=s1.length();
         i=0;
         do
         {
           char ch=s1.charAt(i);
           switch(ch)
            {
                case '0': out.println("Zero  ");break;
                case '1': out.println("One  ");break;
                case '2': out.println("Two  ");break;
                case '3': out.println("Three  ");break;
                case '4': out.println("Four ");break;
                case '5': out.println("Five  ");break;
               case '6': out.println("Six  ");break;
               case '7': out.println("Seven  ");break;
               case '8': out.println("Eight  ");break;
               case '9': out.println("Nine  ");break;
           }
           i++;
         }while(i<n);
%>
</font>
</body>
</html>






                                                                                                                         
Title :- Write a socket program in java for simple stand alone chatting application.

Client side :::--
Import java.io.*;
Import java.net.*;
public class Chatclient
{
Public static void main(String args[])
{
Boolean b = true;
try
{
Socket s1 = new Socket(“localhost”,40);
DataInputStream in1 = new DataInputStream(s1.getInputStream());
DataInputStream out1 = new DataInputStream(s1.getOutputStream());
printStream pw = new printStream(out1);
System.out.println(“Send Message”);
While(b)
{
DataInputStream in = new DataInputStream(System.in);
String str1 = in.readLine();
If(str1.equals(“quit”))
b=false;
else
{
pw.println(str1);
String str2 = in1.readLine();
System.out.println(str2);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}

Server side:::--
Import java.io.*;
Import java.net.*;
public class Chatserver
{
Public static void main(String args[])
{
Boolean b = true;
try
{
ServerSocket s = new ServerSocket(40);
ServerSocket s1 = s.accept();
DataInputStream in1 = new DataInputStream(s1.getInputStream());
DataOutputStream out1 = new DataOutputStream(s1.getOutputStream());
printStream pw = new printStream(out1);
while(b)
{
String str1 = in1.readLine();
If(str1.equals(“quit”))
b =false;
else
{
System.out.println(str1);
DataInputStream in = new DataInputStream(System.in);
String str = in.readLine();
pw.println(str2);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}




















  Updated on 19/02/2016                                                                                                                       
Title :- Write a program for connecting server with client like google.com with ip address of it.


// java socket client example
import java.io.*;
import java.net.*;
public class socket_client
{
Public static void main(String[] args) throws IOException
{
Socket s = new Socket();
String host = “www.google.com”;
Try
{
s.connect(new InetSocketAddress(host,80));
}
//Host not found
catch(UnknownHostException e)
{
System.out.println(“Dont know about this host: ” +host);
System.exit(1);
}
System.out.println(“Connected”);
}
}


No comments:

Ebook

Ebook
Ebook