Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Wednesday 22 February 2012

UNIX PRACTICAL

NOTE : ALL THESE PROGRAMS ARE DEVELOPED AND TESTED IN UWIN (unix for windows), FOR UNIX TERMINAL

1.  YOU HAVE TO CHANGE == (double equals to) to = (single equals to).

IF ANY OTHER PROBLEM OCCURS PLEASE COMMENT (using account or anonymously), WE WELCOME.
  1. Check the output of the following commands.date, ls, who, cal, ps, wc, cat, uname, pwd, mkdir, rmdir, cd, cp, rm, mv, diff, chmod, grep, sed, head, tail, cut, paste, sort, find.
  2. Write shell script
    1. Accept numbers and perform addition, subtraction, division and multiplication.
    2. Accept the string and checks whether the string is palindrome or not.
    3. Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number.
    4. Accept strings and replace a string by another string.
    5. Accept filename and displays last modification time if file exists, otherwise display appropriate message.
    6. Fetch the data from a file and display data into another file in reverse order.
  3. Write a script to find the global complete path for any file.
  4. Write a script to broadcast a message to a specified user or a group of users logged on any terminal.
  5. Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.
  6. Write a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory.
  7. Write a script to delete zero sized files from a given directory (and all its sub-directories).
  8. Write a script to display the name of those files (in the given directory) which are having multiple links.
  9. Write a script to display the name of all executable files in the given directory.
  10. Write a script to display the date, time and a welcome message (like Good Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.
  11. Write a script to display the directory in the descending order of the size of each file.
  12. Write a script to implement the following commands:
    Tree (of DOS)  which (of UNIX)
  13. Write a script for generating a mark sheet after reading data from a file. File contains student roll no, name , marks of three subjects.
  14. Write a script to make following file and directory management operations menu based:
    Display current directory

    List directory
    Make directory
    Change directory
    Copy a file
    Rename a file
    Delete a file Edit a file
  15. Write a script which reads a text file and output the following
    Count of character, words and lines.
    File in reverse.
    Frequency of particular word in the file.
    Lower case letter in place of upper case letter.
  16. Write a shell script to check whether the named user is currently logged in or not.
  17. Write a Script for Simple Database Management System Operation.                                                        Database File Contains Following Fields.                                                                                                                                   EMP_NO EMP_NAME EMP_ADDRESS EMP_AGE EMP_GENDER EMP_DESIGNATION EMP_BASIC_SALARY Provide Menu Driven Facility ForVIEW RECORD BASED ON QUERYADD RECORDDELETE RECORDMODIFY RECORD.COUNT TOTAL NUMBER OF RECORDSEXIT 
  18. Write A Script To Perform Following String Operations Using Menu:
    COMPARE TWO STRINGS.
    JOIN TWO STRINGS.
    FIND THE LENGTH OF A GIVEN STRING.
    OCCURRENCE OF CHARACTER AND WORDS

    REVERSE THE STRING.
  19. Write a script to calculate gross salary for any number of employees
    Gross Salary =Basic + HRA + DA.
    HRA=10% and DA= 15%.
         
  20. Write a script to check whether a given string is palindrome or not.
  21. Write a script to check whether a given number is palindrome or not.
  22. Write a script to display all words of a file in ascending order.
  23. Write a script to display all lines of a file in ascending order.
  24. Write a script to display the last modified file.
  25. Write a shell script to add the statement #include <stdio.h> at the beginning of every C source file in current directory containing printf and fprintf.
  26. Write a script that behaves both in interactive and non-interactive mode. When no arguments are supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts for deletion of the file. If the user supplies arguments with the script, then it works on those files only.
  27. Write a script that deletes all leading and trailing spaces in all lines in a file. Also remove blank lines from a file. Locate lines containing only printf but not fprintf.

Write a script to display the date, time and a welcome message (like Good Morning etc.). The time should be displayed with “a.m.” or “p.m.” and not in 24 hours notation.


HH=`date +%H`
time=`date +"%S %p"`
if [ $HH -ge 12 ];then
HH=`expr $HH % 12`
if [ $HH -lt 5 ];then
msg="GOOD AFTERNOON"
elif [ $HH -ge 5 ]  &&  [ $HH -lt 9 ];then
msg="GOOD EVENING"
else
msg="GOOD NIGHT"
fi
echo "$msg ,CURRENT TIME $HH:$time"
exit 1
else
if [ $HH -lt 5 ];then
msg="GOOD NIGHT"
else
msg="GOOD MORNING"
fi
echo "$msg ,CURRENT TIME $HH:$time"
exit 1
fi

Write a script to display the name of those files (in the given directory) which are having multiple links.


clear
echo "Enter Directory Name :="
read dir
len=`ls -l $dir | wc -l`
i=2
echo "File With Multiple Link are : "
echo " "
while [ $i -le $len ]
do
record=`ls -l $dir | head -n $i | tail -n 1`
filename=`echo $record | cut -d " " -f 9`
link=`echo $record | cut -d " " -f 2`
if [ $link -gt 1 ];then
echo "$filename = $link"
fi
i=`expr $i + 1`
done

Write a script to delete zero sized files from a given directory (and all its sub-directories).


[[  METHOD 1  ]]

clear
`echo ls`>filelist
echo "==============="
total=`cat filelist | wc -w`
echo "total = $total"
i=$total
while [ $i -ge 1 ]
do
# temp=`expr $total - 1`
file=`tail -n $i filelist | head -n 1`
i=`expr $i - 1`
size=`ls -s $file|cut -c 1-2`
# echo "$size"
if [ $size -eq 0 ];then
echo "$file"
rm $file
fi
done

[[  METHOD 2  ]]

clear
`echo ls`>filelist
echo "==============="
for filename in `cat filelist`
do
if [ ! -d $filename ];then
size=`ls -s $filename`
size=`echo $size|cut -d " " -f 1`
echo $size
if [ $size -eq 0 ];then
echo "Want to Remove $filename:"
rm -i $filename
fi
fi
done

Write a script to compare identically named files in two different directories and if they are same, copy one of them in a third directory.


[[  METHOD 1  ]]

clear
echo "Enter Directory 1:"
read dir1
echo "Enter Directory 2:"
read dir2
if [ ! -d $dir1 ] || [ ! -d $dir2 ]; then
echo "dir1 not exist"
exit 1
fi
`echo ls $dir1`>dir1.txt
`echo ls $dir2`>dir2.txt
echo "==============="
totalfile1=`wc -w dir1.txt|cut -c 8-9` # HOME
totalfile2=`wc -w dir2.txt|cut -c 8-9` # HOME
# totalfile1=`wc -w dir1.txt|cut -c 1-2`   # COLLEGE
# totalfile2=`wc -w dir2.txt|cut -c 1-2` # COLLEGE
echo "totalfile:$totalfile1"
echo "totalfile:$totalfile2"
mkdir NEW_DIR
i=$totalfile1
while [ $i -ge 1 ]
do
file1=`tail -n $i dir1.txt | head -n 1`
i=`expr $i - 1`
j=$totalfile2
while [ $j -ge 1 ]
do
file2=`tail -n $j dir2.txt | head -n 1`
j=`expr $j - 1`
if [ "$file1" == "$file2" ];then

str1=`ls -l $dir1/$file1 | cut -d " " -c 50-55` # HOME
str2=`ls -l $dir2/$file2 | cut -d " " -c 50-55` # HOME
# str1=`ls -l 3.sh|cut -d " " -f 7` # COLLEGE
# str2=`ls -l 3a.sh|cut -d " " -f 7` # COLLEGE

hh1=`echo $str1 | cut -c 1-2`
ss1=`echo $str1 | cut -c 4-5`
hh2=`echo $str2 | cut -c 1-2`
ss2=`echo $str2 | cut -c 4-5`

# echo "hh1:$hh1 and ss1:$ss1";
# echo "hh2:$hh2 and ss2:$ss2";
if [ $hh1 -eq $hh2 ];then
if [ $ss1 -le $ss2 ];then
cp $dir2/$file2 NEW_DIR
else
cp $dir1/$file1 NEW_DIR
fi
else
if [ $hh1 -le $hh2 ];then
cp $dir2/$file2 NEW_DIR
else
cp $dir1/$file1 NEW_DIR
fi
fi
fi
done
done

[[  METHOD 2  ]]

echo "Enter dir1:"
read dir1
echo "Enter dir2:"
read dir2
if [ ! -d $dir1 ] || [ ! -d $dir2  ];then
echo "DIRECTORY NOT EXIST"
exit 1
fi
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir RESULT_DIR
for name1 in `cat dir1.txt`
do
for name2 in `cat dir2.txt`
do
if [ "$name1" == "$name2" ];then
record1=`ls -l $dir1/$name1`
record2=`ls -l $dir2/$name2`
time1=`echo $record1 | cut -d " " -f 8`
time2=`echo $record2 | cut -d " " -f 8`
HH1=`echo $time1|cut -d ":" -f 1`
SS1=`echo $time1|cut -d ":" -f 2`
HH2=`echo $time2|cut -d ":" -f 1`
SS2=`echo $time2|cut -d ":" -f 2`
if [ $HH1 -gt $HH2 ];then
cp $dir1/$name1 RESULT_DIR
elif [ $HH2 -gt $HH1 ];then
cp $dir2/$name2 RESULT_DIR
elif [ $SS1 -gt $SS2 ];then
cp $dir1/$name1 RESULT_DIR
elif [ $SS2 -gt $SS1 ];then
cp $dir2/$name2 RESULT_DIR
fi
fi
done
done


[[  METHOD 3  ]]

echo "Enter dir1:"
read dir1
echo "Enter dir2:"
read dir2
if [ ! -d $dir1 ] || [ ! -d $dir2  ];then
echo "DIRECTORY NOT EXIST"
exit 1
fi
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir RESULT_DIR
for name1 in `cat dir1.txt`
do
for name2 in `cat dir2.txt`
do
if [ "$name1" == "$name2" ];then
record=`ls -lt $dir1/$name1 $dir2/$name2 | head -n 1`
cpyfilename=`echo $record | cut -d " " -f 9`
`cp $cpyfilename RESULT_DIR`
fi
done
done

WRITE A SCRIPT TO COPY THE FILE SYSTEM FROM TWO DIRECTORIES TO A NEW DIRECTORY IN SUCH A WAY THAT ONLY THE LATEST FILE IS COPIED IN CASE THERE ARE COMMON FILES IN BOTH THE DIRECTORIES

[[  METHOD 1  ]]

clear
echo "Enter Directory 1:"
read dir1
echo "Enter Directory 2:"
read dir2
if [ ! -d $dir1 ] || [ ! -d $dir2 ]; then
echo "dir1 not exist"
exit 1
fi
`echo ls $dir1`>dir1.txt
`echo ls $dir2`>dir2.txt
echo "==============="
totalfile1=`wc -w dir1.txt|cut -c 8-9` # FOR KSH
totalfile2=`wc -w dir2.txt|cut -c 8-9` # FOR KSH
# totalfile1=`wc -w dir1.txt|cut -c 1-2`   # FOR UNIX TERMINAL
# totalfile2=`wc -w dir2.txt|cut -c 1-2` # FOR UNIX TERMINAL
echo "totalfile:$totalfile1"
echo "totalfile:$totalfile2"
mkdir NEW_DIR
# copying all files from dir1 and comparing each file with files in dir2
i=$totalfile1
flag=0
while [ $i -ge 1 ]
do
file1=`tail -n $i dir1.txt | head -n 1`
i=`expr $i - 1`
flag=0
j=$totalfile2
while [ $j -ge 1 ]
do
file2=`tail -n $j dir2.txt | head -n 1`
j=`expr $j - 1`
if [ "$file1" == "$file2" ];then
flag=1
str1=`ls -l $dir1/$file1 | cut -d " " -c 50-55` # FOR KSH
str2=`ls -l $dir2/$file2 | cut -d " " -c 50-55` # FOR KSH
# str1=`ls -l 3.sh|cut -d " " -f 7` # FOR UNIX TERMINAL
# str2=`ls -l 3a.sh|cut -d " " -f 7` # FOR UNIX TERMINAL

hh1=`echo $str1 | cut -c 1-2`
ss1=`echo $str1 | cut -c 4-5`
hh2=`echo $str2 | cut -c 1-2`
ss2=`echo $str2 | cut -c 4-5`

# echo "hh1:$hh1 and ss1:$ss1";
# echo "hh2:$hh2 and ss2:$ss2";
if [ $hh1 -eq $hh2 ];then
if [ $ss1 -le $ss2 ];then
cp $dir2/$file2 NEW_DIR
else
cp $dir1/$file1 NEW_DIR
fi
else
if [ $hh1 -le $hh2 ];then
cp $dir2/$file2 NEW_DIR
else
cp $dir1/$file1 NEW_DIR
fi
fi
fi
done
if [ $flag -eq 0 ];then
cp $dir1/$file1 NEW_DIR
fi
done


# copying remaining files that are in dir2 but not in dir1
i=$totalfile2
flag=0
while [ $i -ge 1 ]
do
file1=`tail -n $i dir2.txt | head -n 1`
i=`expr $i - 1`
flag=0
j=$totalfile1
while [ $j -ge 1 ]
do
file2=`tail -n $j dir1.txt | head -n 1`
j=`expr $j - 1`
if [ "$file1" == "$file2" ];then
flag=1
fi
done
if [ $flag -eq 0 ];then
cp $dir2/$file1 NEW_DIR
fi
done

[[  METHOD 2  ]]


echo "Enter dir1:"
read dir1
echo "Enter dir2:"
read dir2
if [ ! -d $dir1 ] || [ ! -d $dir2  ];then
echo "DIRECTORY NOT EXIST"
exit 1
fi
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir RESULT_DIR
for name1 in `cat dir1.txt`
do
for name2 in `cat dir2.txt`
do
if [ "$name1" == "$name2" ];then
record1=`ls -l $dir1/$name1`
record2=`ls -l $dir2/$name2`
time1=`echo $record1 | cut -d " " -f 8`
time2=`echo $record2 | cut -d " " -f 8`
HH1=`echo $time1|cut -d ":" -f 1`
SS1=`echo $time1|cut -d ":" -f 2`
HH2=`echo $time2|cut -d ":" -f 1`
SS2=`echo $time2|cut -d ":" -f 2`
if [ $HH1 -gt $HH2 ];then
cp $dir1/$name1 RESULT_DIR
elif [ $HH2 -gt $HH1 ];then
cp $dir2/$name2 RESULT_DIR
elif [ $SS1 -gt $SS2 ];then
cp $dir1/$name1 RESULT_DIR
elif [ $SS2 -gt $SS1 ];then
cp $dir2/$name2 RESULT_DIR
fi
else
cp $dir1/$name1 RESULT_DIR
fi
done
done

for name2 in `cat dir2.txt`
do
flag=0
for name1 in `cat dir1.txt`
do
if [ "$name2" == "$name1" ];then
flag=1
fi
done
if [ $flag -eq 0 ];then
cp $dir2/$name2 RESULT_DIR
fi
done

[[  METHOD 3  ]]


echo "Enter dir1:"
read dir1
echo "Enter dir2:"
read dir2
if [ ! -d $dir1 ] || [ ! -d $dir2  ];then
echo "DIRECTORY NOT EXIST"
exit 1
fi
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir RESULT_DIR
for name1 in `cat dir1.txt`
do
flag=0
for name2 in `cat dir2.txt`
do
if [ "$name1" == "$name2" ];then
flag=1
record=`ls -lt $dir1/$name1 $dir2/$name2 | head -n 1`
cpyfilename=`echo $record | cut -d " " -f 9`
`cp $cpyfilename RESULT_DIR`
fi
done
if [ $flag -eq 0 ];then
`cp $dir1/$name1 RESULT_DIR`
fi
done

for name2 in `cat dir2.txt`
do
flag=0
for name1 in `cat dir1.txt`
do
if [ "$name2" == "$name1" ];then
flag=1
fi
done
if [ $flag -eq 0 ];then
cp $dir2/$name2 RESULT_DIR
fi
done

Write a script to broadcast a message to a specified user or a group of users logged on any terminal.


echo "Enter user Name."
read user_name
echo "\tNOTE : Press Ctrl+d To Broadcast"
write $user_name

Write a script to find the global complete path for any file.


clear
echo "Enter file name : "
read filename
str=`find . -name $filename`
if [ "$str" == "" ];then
    echo "File not found"
    exit 1
fi
path=`pwd`
len=`echo $str | wc -c`
str=`echo $str | cut -d "." -f 2-3`
echo "Full path of file is $path$str"

Fetch the data from a file and display data into another file in reverse order.


echo "Enter filename"
read filename
if [ ! -f $filename ];then
echo "file not exist"
exit 1
fi
str=`cat $filename`
len=`echo $str|wc -c`
i=$len
while [ $i -ge 1 ]
do
temp=$temp`echo $str|cut -c $i`
i=`expr $i - 1`
done
echo $temp>newfile.txt

Accept filename and displays last modification time if file exists, otherwise display appropriate message.


[[  METHOD 1  ]]

echo "Enter the file name"
read File_name
path=`pwd $File_name`
if [ -d $path/$File_name ] ; then
    last_update=`ls -lt $File_name`
elif [ -f $path/$File_name ]; then
last_update=`ls -lt $File_name`
else
echo "File or Directory Not Found"
fi
echo $last_update

[[  METHOD 2  ]]


echo "Enter filename"
read filename
record=`ls -lt $filename`
mtime=`echo $record | cut -d " " -f 6-8`
if [ -f $filename ];then
echo "file exist and modification time is $mtime"
else
echo "file not exist"
fi

Accept strings and replace a string by another string.


clear
echo "Enter string"
read str
echo "Enter string to search"
read sstr
echo "Enter string to replace"
read rstr
str=`echo $str | sed s/$sstr/$rstr/`
echo "string replaced successfully and string is $str"

Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number.


echo "Enter number:"
read no;
count=0
total=0
while [ $no -ne 0 ]
do
a=`expr $no % 10`
no=`expr $no / 10`
total=`expr $total + $a`
count=`expr $count + 1`
done
if [ `expr $no % 2` -eq 0  ]; then
echo "NUMBER IS EVEN"
else
echo "NUMBER IS ODD"
fi
echo "SUM OF ALL DIGITS:$total"
echo "TOTAL NUMBER OF DIGIT:$count"

Accept the string and checks whether the string is palindrome or not.


[[   METHOD 1   ]]
clear
echo "Enter string \c"
read str
len=`echo $str|wc -c`
echo "length"$len
i=1
while [ $i -le `expr $len / 2` ]
do
char1=`echo $str|cut -c $i`
temp=`expr $len - $i`
char2=`echo $str|cut -c $temp`
i=`expr $i + 1`
if [ "$char1" != "$char2" ]; then
i=10;
fi
done
if [ $i -eq 10 ]; then
echo "string is not palindrome"
else
echo "string is palindrome"
fi

[[   METHOD 2   ]]


echo "Enter string \c"
read str
len=`echo $str|wc -c`
len=`expr $len - 1`
echo "length"$len
i=1
while [ $i -le $len ]
do
revstr=`echo $str|cut -c$i`$revstr
i=`expr $i + 1`
done
if [ "$revstr" == "$str" ];then
echo "string is palindrome"
else
echo "string is not palindrome"
fi




Accept numbers and perform addition, subtraction, division and multiplication.


echo "Enter First Number := "
read a
echo "Enter Second Number := "
read b
ans=`expr $a + $b`
echo "Addition is :=" $ans

ans=`expr $a - $b`
echo "Subtraction is :=" $ans

ans=`expr $a / $b`
echo "Division is :=" $ans

ans=`expr $a \* $b`
echo "Multiplication is :=" $ans

Tuesday 14 February 2012

Assume that we have got three pdf files for the MCA-1 Syllabus, MCA-2 Syllabus and MCA-3 Syllabus respectively, Now write a Servlet which displays the appropriate PDF file to the client, by looking at a request parameter for the year (1, 2 or 3).

HTML FILE : 

<html>
<body>
<h2><marquee behavior="alternate" speed="2"> GTU MCA SYLLABUS LIST </marquee></h2>
<form action="showpdf.com" method="GET">
<table border=1>
<tr>
<td>
Your Name : 
<td>
<input type="text" name="name">
<tr>
<td>
Select Pdf :
<td>
<select name="sem">
<option value="1">MCA Sem - 1</option>
<option value="2">MCA Sem - 2</option>
<option value="3">MCA Sem - 3</option>
</select>
<tr>
<td colspan=2 align="center">
<input type="submit" value="SHOW">
</table>
</form>
</body>
</html>


SERVLET CLASS : 

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pro15 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("application/pdf");
PrintWriter out=res.getWriter();
String name=req.getParameter("sem")+".pdf";
res.sendRedirect(name);
}
}

Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class logincheck extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String uname=req.getParameter("uname");
String pass=req.getParameter("pass");
String color=req.getParameter("color");
String value=getCookieValue(uname,req);
if(value!=null)
{
out.println("<body bgcolor="+value+">");
out.println("WELCOME AGAIN");
}
else
{
out.println("<body bgcolor="+color+">");
out.println("WELCOME TO OUR SITE FIRST TIME");
Cookie cwrite=new Cookie(uname,color);
res.addCookie(cwrite);
}
out.println("</body></html>");
}
public String getCookieValue(String name,HttpServletRequest req)
{
String value=null;
Cookie carr[]=req.getCookies(); // IT WILL RETURN THE ARRAY OF COOKIE CLASS
if(carr!=null)
{
for(int i=0;i<carr.length;i++)
{
if(carr[i].getName().equals(name))
{
value=carr[i].getValue(); 
 break;
}
}
}
return value;
}
}

Write a Servlet to display all the attributes available from request and context.


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pro13 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter out=res.getWriter();
  out.printf("<html><head><title>2ND PROGRAM-pankaj</title></head><body bgcolor='limegreen'>");
  out.println("<table border=1 align='center' width=60%><caption>ATTRIBUTE NAME AND THEIR VALUES<tr><th>FIELD<th>VALUE");
   
  Enumeration name=req.getAttributeNames();
  
  while(name.hasMoreElements())
  {
   String hname = (String)name.nextElement();
   out.println("<tr><td>"+hname);
   out.println("<td>"+req.getAttribute(hname));
  }
  out.printf("</table>");
  out.printf("</body>");
 }
}

Write a Servlet to display parameters available on request.


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pro12 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter out=res.getWriter();
  out.printf("<html><head><title>2ND PROGRAM-pankaj</title></head><body bgcolor='limegreen'>");
  out.println("<table border=1 align='center' width=60%><caption>PARAMETER NAME AND THEIR VALUES<tr><th>FIELD<th>VALUE");
   
  Enumeration name=req.getParameterNames();
  
  while(name.hasMoreElements())
  {
   String hname = (String)name.nextElement();
   String hvalue= req.getParameter(hname);
   out.println("<tr><td>"+hname);
   out.println("<td>"+hvalue);
  }
  out.printf("</table>");
  out.printf("</body>");
 }
}

Write a Servlet to display all the headers available from request.


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pro11 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter out=res.getWriter();
  out.printf("<html><head><title>2ND PROGRAM-pankaj</title></head><body bgcolor='limegreen'>");
  out.println("<table border=1 align='center'><tr><th>FIELD<th>VALUE");
  out.printf("<tr><td>METHOD");
  out.printf("<td>"+req.getMethod());
  out.printf("<tr><td>Request URL");
  out.printf("<td>"+req.getRequestURL());
  out.printf("<tr><td>Protcol");
  out.printf("<td>"+req.getProtocol());
  out.printf("<tr><th colspan='2' align='center'>HEADER NAMES");
  Enumeration name=req.getHeaderNames();
  
  while(name.hasMoreElements())
  {
   String hname = (String)name.nextElement();
   String hvalue= req.getHeader(hname);
   out.println("<tr><td>"+hname);
   out.println("<td>"+hvalue);
  }
  out.printf("</table>");
  out.printf("</body>");
 }
}

Write a Servlet to display “Hello World” on browser.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pro10 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter out=res.getWriter();
  out.printf("<html><head><title>1ST PROGRAM-pankaj</title></head><body>");
  out.printf("HELLO WORLD....");
  out.printf("</body>");
 }
}

Write a JavaScript to show a pop up window with a message Hello and background color lime and with solid black border.


<html>
<head>
<script language="javascript">
function popup_window()
{
var win=window.createPopup();
win.document.body.style.backgroundColor="lime";
win.document.body.style.border="lime";
win.document.body.style.border="solid black 10px";
win.document.body.innerHTML="<b>HELLO<b>";
win.show(200,100,500,200,document.body);
}
</script>
</head>
<body>
<input type="button" name="b1" value="Show Popup Window" onClick="popup_window()">
</body>
</html>

Write a JavaScript to find a string from the given text. If the match is found then replace it with another string.

<html>
<head><title>STRING MANUPULATION</title>
<script language="javascript">
var str1,str2;
function clear1()
{
document.getElementById("msg").innerHTML="";
document.getElementById("msg1").innerHTML="";
document.getElementById("msg2").innerHTML="";
document.getElementById("msg3").innerHTML="";
document.getElementById("msg4").innerHTML="";
}
function find_string()
{
str1=new String(f1.txt1.value);
str2=new String(f1.txt2.value);
if(str2=="" ||str1=="" )
{
if(str1=="")
{
document.getElementById("msg1").innerHTML="<b>Enter Main String</b>";
}
else
{
document.getElementById("msg2").innerHTML="<b>Enter Sub String</b>";
}
}
else if(str1.indexOf(str2) < 0)
{
document.getElementById("msg3").innerHTML="<b>String is Not Found</b>";
}
else
{
document.getElementById("msg").innerHTML="<b>Sub String is Fond</b>";
f1.txt3.disabled=false;
f1.txt3.focus();
f1.b1.disabled=true;
f1.b2.disabled=false;
f1.txt3.value="";
}
}
function replace_string()
{
str3=new String(f1.txt3.value);
str1=str1.replace(f1.txt2.value,f1.txt3.value);
document.getElementById("msg").innerHTML="<b>After Replacing String = </b>"+str1;
f1.b1.disabled=false;
f1.b2.disabled=true;
f1.txt3.disabled=true;
}
</script>
</head>
<body>
<h3>PROGRAM 7 : </h3><h4>STRING MANUPULATION</h4>

<form name="f1">
<table>
<tr><td colspan=2>
<font color="red">
<div id="msg1"></div>
</font>
<tr><td>
<b>Enter Main String :</b>
<td><input type="text" name="txt1" onfocus="clear1()"><br/>
<tr><td colspan=2>
<font color="red">
<div id="msg2"></div>
</font>
<tr><td>
<b>Enter Sub String :</b>
<td><input type="text" name="txt2" onfocus="clear1()"><br/>
<tr><td colspan=2>
<font color="green">
<div id="msg4"></div>
</font>
<tr><td><b>Enter Replace String :</b>
<td><input type="text" name="txt3" disabled="true" onblur="clear1()"><br/>
<tr><td> &nbsp</td>
<tr>
<td><input type="button" name="b1" value="FIND" onclick="find_string()">
<td><input type="button" name="b2" value="REPLACE" onclick="replace_string()" disabled="true">
<td><input type="reset" name="b3" value="CLEAR" >
<tr><td colspan=2>
<font color="red">
<div id="msg3"></div>
</font>
<font color="green">
<div id="msg"></div>
</font>
</table>
</form>
</body>
</html>

Write a JavaScript to convert Celsius to Fahrenheit.


<head>
<script lang="javascript">
var flag=false;

function change(ele)
{
if(ele.value=="ctof")
{
flag=false;
document.getElementById("msg").innerHTML="<b>DEGREE CELSIUS</b>";
}
else
{
flag=true;
document.getElementById("msg").innerHTML="<b>DEGREE FAHRENHEIT</b>";
}
}
function numvalid()
{
e=event.keyCode;
if( e<48 || e >57)
{
event.keyCode=0;
}
}
function convert(val)
{
result=document.getElementById("ans");
if(flag==false)
{
f=(val*1.8) + 32;
result.innerHTML="FAHRENHEIT = " + f;
}
else
{
c=(val-32)/1.8;
result.innerHTML="CELCIUS = " + c;
}
}
</script>
<body>
<h3>PROGRAM 7 : </h3><h4>TEMPERATURE CONVERSION</h4>
<h4>SELECT CONVERTION:</h4>
<input type="radio" name="conv" value="ctof" checked="true" onclick="change(this)">CELSIUS TO FAHRENHEIT</input><br>
<input type="radio" name="conv" value="ftoc" onclick="change(this)">FAHRENHEIT TO CELSIUS</input><BR><BR>
<div id="msg"><b>DEGREE CELSIUS</b> </div>
<input type="text" id="degC" name="degC" onkeypress="numvalid()" onkeyup="convert(this.value)"/> <br><br>
<div id="ans"></div>
</body>
</head>

Write a JavaScript to remove the highest element from the array and arrange the array in ascending order.


<html>
<script lang="text/javascript">
document.write("<h3>PROGRAM 6 : </h3><h4>REMOVE HIGHEST ELEMENT FROM ARRAY AND SORTING DATA IN ARRAY</h4>");
var arr=new Array();
arr[0]=5;
arr[1]=20;
arr[2]=2;
arr[3]=7;
arr[4]=15;
display("BEFORE SORTING<br>");

sort();
display("AFTER SORTING <br>");

arr.pop(); //POP IS INBUILD FUNCTION
display("REMOVING HIGHEST DATA AND DISPLAYING  <br>");

//*******DISPLAYING FUNCTION***********
function display(msg)
{
document.write(msg);
for(var i in arr)
document.write(arr[i]+"<BR>");
}
//***********SORTING FUNCTION***********
function sort()
{
var temp;
for(var i in arr)
{
for(var j in arr)
{
if (arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
</script>
<body>
</body>
</html>

Write a JavaScript to generate two random numbers and find out maximum and minimum out of it.


<html>
<script type="text/javascript">
function rnd(ele)
{
no=Math.floor(Math.random()*10 + 1);
ele.value=no;
}
function check()
{
var no1,no2;
no1=parseInt(document.getElementById("no1").value);
no2=parseInt(document.getElementById("no2").value);
if(no1>no2)
document.getElementById("msg").innerHTML=no1+" IS GREATER";
else
document.getElementById("msg").innerHTML=no2+" IS GREATER";
}
</script>
<body>
<form name=f1>
<h3>PROGRAM 5 : </h3><h4>Random Number Generation And Checking relationship</h4>
Number 1 : <input type="text" id="no1" name="no1"/><input type="button" name="b1" value="Generate Number" onclick="rnd(no1)"><br>
Number 2 : <input type="text" id="no2" name="no2"/><input type="button" name="b2" value="Generate Number" onclick="rnd(no2)"><br>
<input type="button" name="result" value="Check Number" onclick="check()"><div id="msg" style.background="limegreen"></div>

</form>
</body>
</html>

Write a JavaScript that finds out multiples of 10 in 0 to 10000. On the click of button start the timer and stop the counter after 10 seconds. Display on the screen how many multiples of 10 are found out within stipulated time.


<html>
<head>
<script lang="javascript">
var arr=new Array();

var no=1,counter=0,sec=0;
function calc1()
{
if(no%10 == 0 && no<=1000000)
{
arr[counter]=no;
counter++;
}
no+=1;
sec++;
t=setTimeout("calc1()",1000);
if(sec==10)
{
f1.t1.value=counter + " Number Found";
}
}
function calc()
{
var no=0,counter=0;
var diff=0;
d=new Date();
startsec=d.getSeconds();
currentsec=startsec;
while( diff < 1)
{
if(no%10 == 0 && no<=1000000)
{
arr[counter]=no;
counter++;
}
no++;
//**********calculation of time*********
f1.t1.value=counter + " Number Found";
d=new Date();
currentsec=d.getSeconds();
if(currentsec<startsec)
currentsec=60+currentsec;
diff=currentsec-startsec;
//********************
}
}
function starttimer()
{
d=new Date();
f1.sec.value=d.getSeconds();
t=setTimeout("starttimer()",1000);
}
function stoptimer()
{
clearTimeout(t);
display();
}
function display()
{
var str="";
for(var i in arr)
{
str=str+arr[i]+" | ";
}
document.getElementById("msg").innerHTML=str;
}
</script>
</head>

<body onload="starttimer()">
<h3>PROGRAM 4 : </h3><h4>FINDING MULTIPLES OF 10</h4><BR>
<form name="f1">
<input type="text" id="t1" name="t1"/>
<input type="text" id="sec" name="sec"/>
<input type="button" name="b1" value="START (According to Time)" onclick="calc()"/>
<input type="button" name="b1" value="START (According to Timeout)" onclick="calc1()"/>
<input type="button" name="b2" value="SHOW NUMBERS" onclick="stoptimer()"/>
<div id="msg"></div>
</form>
</body>
</html>

Create a Form in HTML with two fields, minimum and maximum, write JavaScript to validate that only numeric value is entered in both, and the value entered in minimum is less than the value entered in maximum.

<html>
<head>
<script language="javascript">
function check()
{

minno=f1.min.value;
maxno=f1.max.value;
if ( isNaN(minno) || isNaN(maxno))
{
alert("Number should be numeric in nature");
f1.min.value="";
f1.max.value="";
}
else
if (minno < maxno)
alert("CONGRATULATION YOU KNOW MATHEMATICS");
else
alert("GOTO TO FIRST CLASS TO LEARN MATHEMATICS");
}
</script>
</head>
<body>
<form name=f1>
<h3>PROGRAM 3 : </h3><h4>Numeric validation</h4>
<TABLE>
<TR><TH>Minimum Number : 
<TD><input type="text" name="min"><BR>
<TR><TH>Maximum Number :
<TD><input type="text" name="max"><BR>
<TR><TD><input type="button" name="b1" value="CLICK ME" onClick="check()">
</TABLE>
</form>
</body>
</html>