0% found this document useful (0 votes)
156 views

WT Lab Programs

The document provides code snippets for several web development tasks including taking user input, performing calculations, and manipulating data using JavaScript, PHP, HTML, and Java. It includes examples of sorting numbers, converting numbers to words, counting text fields, selecting country capitals, creating XML files with DOM and SAX parsers, and a basic calculator application.

Uploaded by

saikrishna muddu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

WT Lab Programs

The document provides code snippets for several web development tasks including taking user input, performing calculations, and manipulating data using JavaScript, PHP, HTML, and Java. It includes examples of sorting numbers, converting numbers to words, counting text fields, selecting country capitals, creating XML files with DOM and SAX parsers, and a basic calculator application.

Uploaded by

saikrishna muddu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1.

Write an HTML page including JavaScript that takes a given set of


integer numbers and shows them after sorting in descending order.
<html>
<head>
<script language="JavaScript">
var numbers = [50, 110, 2, 7, 29, 15];
function myFunction()
{
numbers.sort(function(a, b){return b-a});
document.writeln("Descending order is:"+numbers);
}
</script>
</head>
<body>
<form>
<input type="button" name="b1" value="Button1" onclick="myFunction()">
</form>
</body>
</html>

Output:

2. Write html page to take a number from text field in the range 0 to 999
and display in other text field in words. If number is out of range display
alert with “out of range” & display “Not a number” if it is not a number.

<html>
<script language="javascript">
function numinwrd()
{
var numbr=document.getElementById('num').value;
var str=new String(numbr)
var splt=str.split("");
var rev=splt.reverse();

var once=['Zero', ' One', 'Two', 'Three', 'Four','Five', 'Six', 'Seven', 'Eight', 'Nine'];
var twos=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', '
Seventeen', ' Eighteen',' Nineteen'];
var tens=[ '', 'Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', '
Eighty', ' Ninety' ];

numlen=rev.length;
var word=new Array();
var j=0;

for(i=0;i<numlen;i++)
{
switch(i)
{
case 0:
if((rev[i]==0) || (rev[i+1]==1))
{
word[j]='';
}
else
{
word[j]=once[rev[i]];
}
word[j]=word[j] ;
break;

case 1:
abovetens();
break;

case 2:
if(rev[i]==0)
{
word[j]='';
}
else if((rev[i-1]==0) || (rev[i-2]==0) )
{
word[j]=once[rev[i]]+"Hundred ";
}
else
{
word[j]=once[rev[i]]+"Hundred and";
}
break;
case 3:
if(rev[i]==0 || rev[i+1]==1)
{
word[j]='';
}
else
{
word[j]=once[rev[i]];
}
default:break;
}
j++;
}
function abovetens()
{
if(rev[i]==0)
{
word[j]='';
}
else if(rev[i]==1)
{
word[j]=twos[rev[i-1]];
}
else
{
word[j]=tens[rev[i]];
}
}
word.reverse();
var finalw='';
for(i=0;i<numlen;i++)
{
finalw= finalw+word[i];
}
document.niw.word.value=finalw;
}
</script>

<form name="niw">
Number:
<input type="text" name="num" id="num" maxlength="9">
<input type="button" name="sr1" value="Click Here" onClick="numinwrd()">
Number in Words:
<input type="text" name="word" id="word" size="30">
</form>
</html>
Output:

3. Write an HTML page that has one input, which can take multi-line text
and a submit button. Once the user clicks the submit button, it should
show the number of characters, words and lines in the text entered using
an alert message. Words are separated with white space and lines are
separated with new line character.

<html>
<script language="javascript">
function countWCL()
{
var textarea=document.getElementById("tarea");
var text = textarea.value;
value = "Words: " + (text.split(/\b\S+\b/).length - 1) + " Characters: " +
text.replace(/\s/g, "").length + "/ " + text.replace(/\n/g, "").length + "lines:" +
text.split("\n").length;
alert(value);
}
</script>
<form name="cwl">
Enter Multi Line Text <br>
<textarea name="string" id="tarea" rows=4 cols=30></textarea>
<input type="button" name="sub" value="count" onClick="countWCL()">
</form>
</html>
Output:
4. Write an HTML page that contains a selection box with a list of 5
countries. When the user selects a country, its capital should be printed
next to the list. Add CSS to customize the properties of the font of the
capital (color, bold and font size).

<html>
<title>
country name as capital
</title>
<head>
<style>
div.capstr{
color: red; font-style: bold; font-size:12pt;
}
</style>
</head>
<body>
<select id="country" onchange="getcap();">
<option value="NEW DELHI">india </option>
<option value="LONDON">uk </option>
<option value="Beijing">china </option>
<option value="COLOMBO">srilanka </option>
<option value="Washington">USA</option>
</select>
<div class="capstr" id="capcountry"> </div>
<script type="text/javascript">
function getcap()
{
document.getElementById("capcountry").innerHTML=
document.getElementById("country").value;
}
</script>
</div >
</body>
</html>

Output:
5.Write a java program to create an xml document using DOM parser.

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class Dom
{
public static void main(String rags[]) throws Exception
{
DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
DocumentBuilder b=f.newDocumentBuilder();
Document doc=b.newDocument();

Element rootEle=doc.createElement("students");
Element studentEle=doc.createElement("student");

Element nameEle=doc.createElement("name");
Element emailEle=doc.createElement("email");
Element mobileEle=doc.createElement("mobile");
Element addressEle=doc.createElement("address");

Text t1=doc.createTextNode("VAAGESWARI");
Text t2=doc.createTextNode("[email protected]");
Text t3=doc.createTextNode("9849750204");
Text t4=doc.createTextNode("KARIMNAGAR");

nameEle.appendChild(t1);
emailEle.appendChild(t2);
mobileEle.appendChild(t3);
addressEle.appendChild(t4);

studentEle.appendChild(nameEle);
studentEle.appendChild(emailEle);
studentEle.appendChild(mobileEle);
studentEle.appendChild(addressEle);

rootEle.appendChild(studentEle);
doc.appendChild(rootEle);

Transformer t=TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(doc), new StreamResult(new
FileOutputStream("students.xml")));
}
}

Output:
C:\>javac Dom.java
C:\>java Dom

students.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students>
<student>
<name>VAAGESWARI</name>
<email>[email protected]</email>
<mobile>9849750204</mobile>
<address>KARIMNAGAR</address>
</student>
</students>

6. Write a java program to read the XML file contents and print as it is
using SAX parser.

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.io.*;
public class SAXReader extends DefaultHandler
{
public static void main(String rags[]) throws Exception
{
SAXParser p=SAXParserFactory.newInstance().newSAXParser();
p.parse(new FileInputStream("students.xml"), new SAXReader());
}
public void startDocument()
{
System.out.println("Document begins here");
}
public void startElement(String uri, String localName, String qName, Attributes
attrs)
{
System.out.println(" <"+qName+" >");
}
public void characters(char ch[], int start, int len)
{
System.out.println(new String(ch, start, len));
}

public void endElement(String uri, String localName, String qName)


{
System.out.println(" </"+qName+" >");
}
public void endDocument()
{
System.out.println("Document ends here");
}
}

Output:

C:\>javac SAXReader.java

C:\>java SAXReader

Document begins here


<students >
<student >
<name >
VAAGESWARI
</name >
<email >
[email protected]
</email >
<mobile >
9849750204
</mobile >
<address >
KARIMNAGAR
</address >
</student >
</students >
Document ends here
7.A simple calculator web application that takes two numbers and an
operator (+, -, /, * and %) from an HTML page and returns the result page
with the operation performed on the operands.

calc.html
<html>
<head>
<title>calculator</title>
</head>
<body>
<form name="f1" method="post" action="success.php">
<table cellpadding="5" cellspacing="5" border="0">
<tr>
<td>Enter First Number</td>
<td colspan="1"><input name="fvalue" id="fvalue" type="text"/></td>
</tr>
<tr>
<td>Select Operator</td>
<td><select name="operator">
<option selected value=""> choose operator</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select></td>
</tr>
<tr>
<td>Enter second Number</td>
<td><input name="lvalue" type="text" id="lvalue"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="calculate" value="Calculate" /></td>
</tr>
</table>
</form>
</body>
</html>

success.php
<?php
if( isset( $_REQUEST['calculate']))
{
$operator = $_REQUEST['operator'];
$a = $_REQUEST['fvalue'];
$b = $_REQUEST['lvalue'];

if($operator == "+")
{
$res = $a + $b;
$result = 'SUM';
}
if($operator == "-")
{
$res = $a - $b;
$result = 'DIFFERENCE';
}
if($operator == "*")
{
$res = $a * $b;
$result = 'PRODUCT';
}
if($operator == "/")
{
$res= $a / $b;
$result = 'DIVISION';
}
if($operator == "%")
{
$res= $a % $b;
$result = 'REMAINDER';
}
}
echo "The".$result." Of ".$a." And".$b." Is ". $res;
?>
<br/><br/><a href="calc.html">click Here</a> To Go Back To Calculator

Output:
8. A web application that takes name and age from an HTML page. If the
age is less than 18, it should send a page with “Hello <name>, you are not
authorized to visit this site” message, where <name> should be replaced
with the entered name. Otherwise it should send “Welcome <name> to
this site” message.

user.html
<html>
<head>
<title> Authorization of user</title>
<body>
<form action="authenticate.php" method="POST">
Name <input type="text" name="name" value=""/><br>
Age <input type="text" name="age" value=""/><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>

authenticate.php
<?php
if($_POST['age']<18)
{
echo "Hello ".$_POST['name']." You are not authorized to visit this site";
}
else
{
echo "Welcome ".$_POST['name']." to this site";
}
?>
Output:

9. A web application that lists all cookies stored in the browser on clicking
“List Cookies” button. Add cookies if necessary.

1st File: File Name: GaintCookies.html


<HTML>
<HEAD> <TITLE> Welcome to Cookie Programming </TITLE> </HEAD>
<BODY BCCOLOR = lightcyan TEXT = midnightblue>
<FORM METHOD = "post" ACTION = "GaintCookies.jsp">
<CENTER>
<B> Enter Item Name </B> <INPUT TYPE = text NAME = item> <BR>
<B> Enter Item Qty </B> <INPUT TYPE = text NAME = qty> <BR>
<INPUT TYPE = submit NAME = add VALUE = "Add Cookie">
<INPUT TYPE = submit NAME = list VALUE = "List Cookies"> <BR>
</CENTER>
</FORM>
</BODY>
</HTML>
2nd File: File Name: GaintCookies.jsp
<body>
<%
String str1 = request.getParameter( "item" ) ;
String str2 = request.getParameter( "qty" ) ;
String str3 = request.getParameter( "add" ) ;
String str4 = request.getParameter( "list" ) ;
if( str3 != null )
{
Cookie c1 = new Cookie( str1, str2 ) ;
response.addCookie( c1 ) ;
response.sendRedirect("GaintCookies.html") ;
}
if( str4 != null )
{
Cookie c[ ] = request.getCookies( );
for( int i = 0 ; i < c.length ; i++ )
{
out.print( "<B>" + c[ i ].getName( ) + " : " + c[ i ].getValue( ) + "</B> <BR>");
}
}
%>
</body>
Output:
PROGRAM-1.1
AIM: Install TOMCAT web server and APACHE.
While installation, we assign port number 8080 to APACHE. Make sure that these
ports are available i.e., no other process is using this port.

DESCRIPTION:
Set the JAVA_HOME Variable
You must set the JAVA_ HOME environment variable to tell Tomcat where to find
Java. Failing to properly set this variable prevents Tomcat from handling JSP
pages. This variable should list the base JDK installation directory, not the bin
subdirectory. On Windows XP, you could also go to the Start menu, select Control
Panel, choose System, click on the Advanced tab, press the Environment Variables
button at the bottom, and enter the JAVA_HOME variable and value directly as:

Name: JAVA_HOME
Value: C:\jdk
Set the CLASSPATH
Since servlets and JSP are not part of the Java 2 platform, standard edition, you
have to identify the servlet classes to the compiler. The server already knows about
the servlet classes, but the compiler (i.e., javac) you use for development probably
doesn't. So, if you don't set your CLASSPATH, attempts to compile servlets, tag
libraries, or other classes that use the servlet and JSP APIs will fail with error
messages about unknown classes.

Name: JAVA_HOME

Value: install_dir/common/lib/servlet-api.jar
Turn on Servlet Reloading
The next step is to tell Tomcat to check the modification dates of the class files of
requested servlets and reload ones that have changed since they were loaded into
the server's memory. This slightly degrades performance in deployment situations,
so is turned off by default. However, if you fail to turn it on for your development
server, you'll have to restart the server every time you recompile a servlet that has
already been loaded into the server's memory.

To turn on servlet reloading, edit install_dir/conf/server.xml and add a


DefaultContext subelement to the main Host element and supply true for the
reloadable attribute. For example, in Tomcat 5.0.27, search for this entry:

<Host name="localhost" debug="0" appBase="webapps" ...> and then insert the


following immediately below it:
<DefaultContext reloadable="true"/>
Be sure to make a backup copy of server.xml before making the above change.
Enable the Invoker Servlet
The invoker servlet lets you run servlets without first making changes to your Web
application's deployment descriptor. Instead, you just drop your servlet into WEB-
INF/classes and use the URL http://host/servlet/ServletName. The invoker servlet
is extremely convenient when you are learning and even when you are doing your
initial development.

To enable the invoker servlet, uncomment the following servlet and servlet-
mapping elements in install_dir/conf/web.xml. Finally, remember to make a backup
copy of the original version of this file before you make the changes.
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
...
</servlet>
...
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

OUTPUT:

RESULT: Thus TOMCAT web server was installed successfully.


PROGRAM-1.2
AIM: Access the developed static web pages for books web site, using these
servers by putting the web pages developed in week-1 and week-2 in the
document root.

Output:

RESULT: These pages are accessed using the TOMCAT web server successfully.
PROGRAM-1.3 Install MySQL
Aim: Step-by-Step guide for Installing MySQL on Windows
You can download the MySQL database from the MySQL website
http://www.mysql.com by clicking on the downloads tab. Scroll down to the
MySQL database server & standard clients section and select the latest production
release of MySQL, 5.1.35 at the time of writing.

Installation of MySQL Server


Unzip the setup file and execute the downloaded MSI file. Follow the instructions
below exactly when installing MySQL Server:

Click on the "setup"

Perform a typical installation


Check box to configure MySQL Server
If you checked the Configure the MySQL Server now check box on the final dialog
of the MySQL Server installation, then the MySQL Server Instance Configuration
Wizard will

automatically start. Follow the instructions below carefully to configure your


MySQL Server to run correctly with Event Sentry.

Select Detailed Configuration


I was installing it on my local machine where other applications & tools are running
I decided to opt "developer machine" but it is recommended that you use a
Dedicated MySQL Server Machine for your MySQL database, if this is not an
option then select "Server Machine".
If you selected Dedicated MySQL Server Machine and your MySQL service does
not start after the wizard completes, then try to re-run the wizard (or re-install)
MySQL, but this time select the Server Machine option.

I have checked "Multifunctional databases" as I wanted MyISAM as default storage


engine but if you want you can select "Transactional Database Only", this will make
sure that InnoDB is the main storage engine. If you have checked 3rd option then
only myISAM engine would be available

Select the drive where the database files will bestored.


Select the drive on the fastest drive(s) on your server
It is recommended that you leave the default port 3306 in place

It is highly recommended that you run the MySQL Server as a Windows


service(you can disable this if you want to start it manually whenever required) and
include the binary directory in the search path.
Specify a secure root password; you may want to check the box Enable root
access from remote machines if you plan on administering your MySQL server
from your workstation or other servers.

If you are getting an error message after clicking the Next button, then please
enable port 3306 in the Windows XP Firewall Settings

Done!!!
But if you are installing MySQL on a Windows XP workstation, or any other
computer that has a firewall enabled, and the wizard fails with an error message
similar to the one shown below (Can't connect to MySQL server on 'localhost'),
then you will have to exclude the MySQL daemon from your firewall configuration
On Windows XP, you can exclude MySQL from the firewall by following the steps
below:

1. Navigate to Start -> Settings -> Control Panel -> Windows Firewall

2. In the resulting dialog, enter the information as shown in the screenshot

After clicking OK twice, return to the MySQL error message and select Retry.
MySQL should now be able to create the instance correctly.
Installation of WampServer2:
To start the installation process, you need to open the folder where you saved the
file, and double-click the installer file. A security warning window will open,
asking if you are sure you want to run this file. Click Run to start the installation
process.

Next you will see the Welcome To The WampServer Setup Wizard screen. Click
Next to continue the installation.

The next screen you are presented with is the License Ageement. Read the
agreement, check the radio button next to I accept the agreement, then click
Next to continue the installation.

Next you will see the Select Destination Location screen. Unless you would like to
install WampServer on another drive, you should not need to change anything.
Click Next to continue.

The next screen you are presented with is the Select Additional Tasks screen. You
will be able to select whether you would like a Quick Launch icon added to the
taskbar or a Desktop icon created once installation is complete. Make your
selections, then click Next to continue.
Next you will see the Ready To Install screen. You can review your setup choices,
and change any of them by clicking Back to the appropriate screen, if you choose
to. Once you have reviewed your choices, click Install to continue.

WampServer will begin extracting files to the location you selected.

Once the files are extracted, you will be asked to select your default browser.
WampServer defaults to Internet Explorer upon opening the local file browser
window. If your default
browser isn’t IE, then look in the following locations for the corresponding .exe file:
 Opera: C:\Program Files (x86)\Opera\opera.exe
 Firefox: C:\Program Files (x86)\Mozille Firefox\firefox.exe
 Safari: C:\Program Files (x86)\Safari\safari.exe
 Chrome:
C:\Users\xxxxx\AppData\Local\Google\Chrome\Application\chrome.exe

Select your default browser’s .exe file, then click Open to continue. The Setup
screen will appear next, showing you the status of the installation process.

Once the progress bar is completely green, the PHP Mail Parameters screen will
appear. Leave the SMTP server as localhost, and change the email address to
one of your choosing. Click Next to continue.
The Installation Complete screen will now appear. Check the Launch
WampServer Now box, then click Finish to complete the installation.

Testing WampServer:
Once you have completed the installation process, test that your installation is
working properly by going to http://localhost/ in your browser. You should see the
WampServer homepage displayed.

You might also like