Ex - No: 1 Java Components Date: Program

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 45

Ex.

No: 1 JAVA COMPONENTS


Date:
Program:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.lang.*;
/*<applet code="component.class" width=500 height=2000>
</applet>*/
public class component extends Applet implements AdjustmentListener
{
Checkbox it,cse,ece;
CheckboxGroup cbg;
Choice quali;
List design;
Checkbox m,f;
TextField naml,name,dept,exp,des,qua,gen,ba;
Scrollbar bar;
Button ok,reset;
public void init()
{
add(new Label("Choose Department"));
cbg=new CheckboxGroup();
cse=new Checkbox("CSE Dept",cbg,true);
it=new Checkbox("IT Dept",cbg,false);
ece=new Checkbox("ECE Dept",cbg,false);
add(cse);
add(it);
add(ece);
add(new Label(" "));
add(new Label(" Enter your name "));
naml=new TextField(15);
add(naml);
add(new Label(" "));
add(new Label(" Choose your designation "));
design=new List(5,true);
design.addItem("HOD");
design.addItem("Professor");
design.addItem("Asst.Professor");
design.addItem("Sr.Lecturer");
design.addItem("Lecturer");
add(design);
add(new Label(" "));
add(new Label(" "));
add(new Label(" Choose your qualification "));
quali=new Choice();
quali.addItem("Phd");
quali.addItem("M.Tech");
quali.addItem("M.E");
quali.addItem("B.E.");
quali.addItem("B.Tech");
quali.addItem("M.S.");
add(quali);
add(new Label(" "));
add(new Label(" "));
add(new Label(" Choose your gender "));
m=new Checkbox("Male");
f=new Checkbox("Female");
add(m);
add(f);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Choose your experience "));
bar=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,50);
add(bar);
bar.addAdjustmentListener(this);
ba=new TextField(" ",5);
add(new Label(" "));
add(ba);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Click of after choosing data "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
ok=new Button("ok");
add(ok);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" your data "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Name "));
name=new TextField(" ",5);
add(name);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Gender "));
gen=new TextField(" ",7);
add(gen);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Department "));
dept=new TextField(" ",10);
add(dept);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Designation "));
des=new TextField(" ",10);
add(des);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Qualification "));
qua=new TextField(" ",10);
add(qua);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" Experience "));
exp=new TextField(" ",10);
add(exp);
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
add(new Label(" "));
reset=new Button("RESET");
add(reset);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
if(e.getAdjustable()==bar)
{
bar.setValue(bar.getValue());
ba.setText(Integer.toString(bar.getValue()));
}
}
public boolean action(Event e,Object arg)
{
if(e.target instanceof Scrollbar)
{
ba.setText(Integer.toString(bar.getValue()));
return true;
}
else if(e.target.equals(ok))
{
name.setText(naml.getText());
dept.setText(cbg.getCurrent().getLabel());
des.setText(design.getSelectedItem());
qua.setText(quali.getSelectedItem());
exp.setText(Integer.toString(bar.getValue()));
if(m.getState())
gen.setText(m.getLabel());
else if(f.getState())
gen.setText(f.getLabel());
return true;
}
else if(e.target.equals(reset))
{
naml.setText(" ");
name.setText(" ");
dept.setText(" ");
des.setText(" ");
qua.setText(" ");
exp.setText(" ");
gen.setText(" ");
m.setState(false);
f.setState(false);
return true;
}
return false;
}
}

Output:
Ex.No: 2a FLOW LAYOUT
Date:
Program:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.lang.*;
/*<applet code="flay.class" width=300 height=300>
</applet>*/
public class flay extends Applet
{
public void init()
{
setLayout(new FlowLayout());
add(new Button("First REGION"));
add(new Button("Second REGION"));
add(new Button("Third REGION"));
add(new Button("Forth REGION"));
add(new Button("Fifth REGION"));
}
}

Output:
Ex.No: 2b BORDER LAYOUT
Date:
Program:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.lang.*;
/*<applet code="brd.class" width=300 height=300>
</applet>*/
public class brd extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add("North",new Button("NORTH REGION"));
add("West",new Button("WEST REGION"));
add("Center",new Button("CENTER REGION"));
add("East",new Button("EAST REGION"));
add("South",new Button("SOUTH REGION"));
}
}

Output:
Ex.No: 2c GRID LAYOUT
Date:
Program:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.lang.*;
/*<applet code="grid.class" width=300 height=300>
</applet>*/
public class grid extends Applet
{
public void init()
{
setLayout(new GridLayout(3,2));
add(new Button("1,1 region"));
add(new Button("1,2 region"));
add(new Button("2,1 region"));
add(new Button("2,2 region"));
add(new Button("3,1 region"));
add(new Button("3,2 region"));
}
}

Output:
Ex.No: 2d CARD LAYOUT
Date:
Program:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="crdlay10.class" width=600 height=600>
</applet>*/
public class crdlay10 extends Applet implements ActionListener
{
Checkbox os,dbms,ds,dm,ip;
Panel cards;
CardLayout crd;
Button practical,theory;
public void init()
{
practical=new Button("Practical");
theory=new Button("Theory");
add(practical);
add(theory);
crd=new CardLayout();
cards=new Panel();
cards.setLayout(crd);
os=new Checkbox("OPERATING SYSTEM",null,true);
dbms=new Checkbox("Data Base Management System");
ds=new Checkbox("Data Structures");
dm=new Checkbox("Data mining");
ip=new Checkbox("Internet Programming");
Panel practicalpanel=new Panel();
practicalpanel.add(ds);
practicalpanel.add(ip);
Panel theorypanel=new Panel();
theorypanel.add(os);
theorypanel.add(dbms);
theorypanel.add(dm);
cards.add(practicalpanel,"PRACTICAL");
cards.add(theorypanel,"THEORY");
add(cards);
practical.addActionListener(this);
theory.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==practical)
{
crd.show(cards,"PRACTICAL");
}
else
{
crd.show(cards,"THEORY");
}
}
}

Output:
Ex.No: 3 COLOR PALETTE
Date:
Program:
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/*<applet code="Exp3" width=500 height=2000>
</applet>*/
public class Exp3 extends Applet implements ItemListener
{
int currcolor=3;
int flag=1;
String text="click any of the button";
Button buttons[]=new Button[3];
String colors[]={"Red","Blue","Yellow"};
CheckboxGroup cbg= new CheckboxGroup();
Checkbox box1=new Checkbox("Background color",cbg,true);
Checkbox box2=new Checkbox("Text color",cbg,false);
public void init()
{
for(int i=0;i<3;i++)
{
buttons[i]=new Button(" ");
add(buttons[i]);
}
buttons[0].setBackground(Color.red);
buttons[1].setBackground(Color.blue);
buttons[2].setBackground(Color.yellow);
add(box1);
add(box2);
box1.addItemListener(this);
box2.addItemListener(this);
}
public void itemStateChanged(ItemEvent ev)
{
if(box1.getState()==true)
flag=1;
else if(box2.getState()==true)
{
text="default color is black";
flag=2;
}
repaint();
}
public void paint(Graphics g)
{
if (flag==2)
{
g.drawString(text,30,100);
switch(currcolor)
{
case 0:
g.setColor(Color.red);
break;
case 1:
g.setColor(Color.blue);
break;
case 3:
g.setColor(Color.yellow);
break;
}
g.drawString(text,30,100);
}
else if(flag==1)
{
g.drawString(text,30,100);
switch(currcolor)
{
case 0:
setBackground(Color.red);
break;
case 1:
setBackground(Color.blue);
break;
case 3:
setBackground(Color.yellow);
break;
}
}
}
public boolean action(Event e, Object o)
{
for(int i=0;i<3;i++)
{
if(e.target==buttons[i])
{
currcolor=i;
text="you have chosen "+colors[i];

repaint();
return true;
}
}
return false;
}
}

Output:
Ex.No: 4a URL OF ANOTHER SERVER
Date:
Program:
import java.net.*;
import java.io.*;
import java.util.*;
public class HeaderViewer {
public static void main(String args[]) {
for (int i=0; i < args.length; i++) {
try {
URL u = new URL(args[0]);
URLConnection uc = u.openConnection( );
System.out.println("Content-type: " + uc.getContentType( ));
System.out.println("Content-encoding: " + uc.getContentEncoding( ));
System.out.println("Date: " + new Date(uc.getDate( )));
System.out.println("Last modified: " + new Date(uc.getLastModified( )));
System.out.println("Expiration date: " + new Date(uc.getExpiration( )));
System.out.println("Content-length: " + uc.getContentLength( ));
}
catch (MalformedURLException ex) {
System.err.println(args[i] +" is not a URL I understand");
}
catch (IOException ex) {
System.err.println(ex);
}
System.out.println( );
}
}
}

Output:
D:\bijendra>javac HeaderViewer.java
D:\bijendra>java HeaderViewer http://192.168.0.45/default.html
Content-type: text/html; charset=iso-8859-1
Content-encoding: null
Date: Tue Sep 22 10:54:39 GMT+05:30 2010
Last modified: Thu Jan 01 05:30:00 GMT+05:30 1990
Expiration date: Thu Jan 01 05:30:00 GMT+05:30 1999
Content-length: 298
D:\vidhun>java HeaderViewer http://192.168.0.45
Content-type: text/html
Content-encoding: null
Date: Tue Sep 22 11:07:17 GMT+05:30 2010
Last modified: Wed Aug 21 03:11:18 GMT+05:30 2008
Expiration date: Thu Jan 01 05:30:00 GMT+05:30 1999
Content-length: 220
Ex.No: 4b DOWNLOAD HOMEPAGE OF SERVER
Date:
Program:
import java.net.*;
import java.io.*;
public class SourceViewer {
public static void main (String[] args) {
if (args.length > 0) {
try {
URL u = new URL(args[0]);
InputStream in = u.openStream( );
in = new BufferedInputStream(in);
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1) {
System.out.print((char) c);
}
}
catch (MalformedURLException ex) {
System.err.println(args[0] +
" is not a parseable URL");
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
}

Output:
D:\bijendra>java SourceViewer
D:\bijendra>java SourceViewer http://192.168.0.45
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Mozilla/4.72 [en] (WinNT; U) [Netscape]">
<title>Oracle HTTP Server Index</title>
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#0000EE"vlink="#551A8B"
alink="#FF0000">
<img SRC="header.gif" height=74 width=292 align=ABSCENTER>
<br><img SRC="header1.gif" height=39 width=100%>
<blockquote>
<blockquote>
<li>
<u><font face="Arial,Helvetica"><a href="manual/index.html">Apache Documentation
</a></font></u></li>
<li>
<font face="Arial,Helvetica"><a href="/jservdocs/">JServ Documentation</a>
</font></li>
<br><font face="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp; <a href="../servlet/
IsItWorking/">Demo</a></font>
<li>
<font face="Arial,Helvetica"><a href="/jspdocs/">Oracle JSP Documentation</a>
</font></li>
<br><font face="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;
<ahref="../demo/">Demos</a></font>
<li>
<font face="Arial,Helvetica"><a href="/soapdocs/ReleaseNotes.html">SOAP Release
Notes and Documentation</a></font></li>
<li>
<font face="Arial,Helvetica"><a href="http://www.modssl.org/">Mod_SSL Web
Site</a></font></li>
<li>
<font face="Arial,Helvetica"><a href="http://www.openssl.org/">OpenSSL</a>
</font></li>
<li>
<font face="Arial,Helvetica"><a href="/fastcgi/">FastCGI Developer's Kit
Documentation</a></font></li>
<li>
<font face="Arial,Helvetica"><a href="http://perl.apache.org/">mod_perl
Web Site</a></font></li>
<li>
<font face="Arial,Helvetica"><a href="/pls/admin_/gateway.htm">Mod_plsql
Configuration Menu</a></font></li>
<li>
<font face="Arial,Helvetica"><a href="bc4j.html">BC4J Documentation and
Samples</a></font></li>
<li>
<font face="Arial,Helvetica"><a href="../xsql/">XML Developers Kit XDK
Documentation</a></font></li>
<li>
<font face="Arial,Helvetica"><a href="../mod_ose.html">Mod_OSE Documentation</a>
</font></li>
</blockquote>
</blockquote>
<img SRC="footer1.gif" height=39 width=100%>
<p><font size=-1>Copyright&nbsp; 2001&nbsp; Oracle Corporation.&nbsp; All
Rights Reserved.</font>
</body>
</html>
Ex.No: 5a SOCKET PROGRAMMING
Date:
Program:
FILE SERVER
import java.net.*;
import java.io.*;
public class FileServer
{
ServerSocket serverSocket;
Socket socket;
int port;
FileServer()
{
this(9999);
}
FileServer(int port)
{
this.port = port;
}
void waitForRequests() throws IOException
{
serverSocket = new ServerSocket(port);
while (true)
{
System.out.println("Server Waiting...");
socket = serverSocket.accept();
System.out.println("Request Received From " + socket.getInetAddress()
+"@"+socket.getPort());
new FileServant(socket).start();
System.out.println("Service Thread Started");
}
}
public static void main(String[] args)
{
try
{
new FileServer().waitForRequests();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
FILE CLIENT

import java.net.*;
import java.io.*;
public class FileClient
{
String fileName;
String serverAddress;
int port;
Socket socket;
FileClient()
{
this("localhost", 9999, "Sample.txt");
}
FileClient(String serverAddress, int port, String fileName)
{
this.serverAddress = serverAddress;
this.port = port;
this.fileName = fileName;
}
void sendRequestForFile() throws UnknownHostException,IOException
{
socket = new Socket(serverAddress, port);
System.out.println("Connected to Server...");
PrintWriter writer = new PrintWriter(new
OutputStreamWriter(socket.getOutputStream()));
writer.println(fileName);
writer.flush();
System.out.println("Request Sent...");
getResponseFromServer();
socket.close();
}
void getResponseFromServer() throws IOException
{
BufferedReader reader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String response = reader.readLine();
if(response.trim().toLowerCase().equals("filenotfound"))
{
System.out.println(response);
return;
}
else
{
BufferedWriter fileWriter = new BufferedWriter(new FileWriter("Recdfile.txt"));
do
{
fileWriter.write(response);
fileWriter.flush();
}
while((response=reader.readLine())!=null);
fileWriter.close();
}
}
public static void main(String[] args)
{
try
{
new FileClient().sendRequestForFile();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
FILE SERVANT
import java.net.*;
import java.io.*;
public class FileServant extends Thread
{
Socket socket;
String fileName;
BufferedReader in;
PrintWriter out;
FileServant(Socket socket) throws IOException
{
this.socket = socket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
}
public void run()
{
try
{
fileName = in.readLine();
File file = new File(fileName);
if (file.exists())
{
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
String content = null;
while ((content = fileReader.readLine())!=null)
{
out.println(content);
out.flush();
}
System.out.println("File Sent...");
}
else
{
System.out.println("Requested File NotFound...");
out.println("File Not Found");
out.flush();
}
socket.close();
System.out.println("Connection Closed!");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
}
}

Output:

C:\IPLAB>javac FileServer.java
C:\IPLAB>javac FileClient.java
C:\IPLAB>javac FileServant.java
C:\IPLAB>copy con Sample.txt
Welcome to FTP
C:\IPLAB>java FileServer
Server Waiting...
C:\IPLAB>java FileClient
Connected to Server...
Request Sent...
C:\IPLAB>java FileServer
Server Waiting...
Request Received From /127.0.0.1@2160
Service Thread Started
Server Waiting...
File Sent...
Connection Closed!
C:\IPLAB>type Recdfile.txt
Welcome to FTP
Ex.No: 5b HTTP
Program:
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class SourceViewer3 {
public static void main (String[] args) {
for (int i = 0; i < args.length; i++) {
try {
URL u = new URL(args[i]);
HttpURLConnection uc = (HttpURLConnection)
u.openConnection( );
int code = uc.getResponseCode( );
String response = uc.getResponseMessage( );
System.out.println("HTTP/1.x " + code + " " +
response);
for (int j = 1; ; j++) {
String header = uc.getHeaderField(j);
String key = uc.getHeaderFieldKey(j);
if (header == null || key == null) break;
System.out.println(uc.getHeaderFieldKey(j) + ": " +
header);
}
InputStream in = new
BufferedInputStream(uc.getInputStream( ));
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1) {
System.out.print((char) c);
}
}
catch (MalformedURLException ex) {
System.err.println(args[0] + " is not a parseable
URL");
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
}
Output:

C:\IPLAB>javac SourceViewer3.java
C:\IPLAB>java SourceViewer http://172.16.0.15/default.htm
HTTP/1.x 200 OK
Key Content-Length: 14895
Key Content-Type: text/html
Key Last-Modified: Sat, 20 Sep 2008 05:55:01 GMT
Key Accept-Ranges: bytes
Key ETag: "80a87d6be51ac91:489"
Key Server: Microsoft-IIS/6.0
Key X-Powered-By: ASP.NET
Key Date: Mon, 06 Oct 2008 02:59:26 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
.
.
.
</html>
Ex.No: 5c SMTP
Program:
import javax.mail.internet.*;
import javax.mail.*;
import java.util.*;
public class Smtpass {
public static void main(String[] args) {
try {
Properties props = new Properties( );
props.put("mail.host", "mail.studentwebsite.org");
Session mailConnection =
Session.getInstance(props, null);
Message msg = new MimeMessage(mailConnection);
Address programer = new
InternetAddress("programer@student.com",
"Bill Gates");
Address bhuvangates = new
InternetAddress("webadmin@studentwebsite.org"
);
msg.setContent("Wish You a Happy Christmas
2008", "text/plain");
msg.setFrom(programer);
msg.setRecipient(Message.RecipientType.TO,
bhuvangates);
msg.setSubject("Greetings");
Transport.send(msg);
}
catch (Exception er) {
er.printStackTrace( );
}
}
}
Output:

C:\IPLAB>javac Smtpass.java
C:\IPLAB>java Smtpass
Ex.No: 5d POP3
Program:
import javax.mail.internet.*;
import javax.mail.*;
import java.io.*;
import java.util.*;
public class ClientPOP3 {
public static void main(String[] args) {
Properties props = new Properties( );
String host = "mail.studentwebsite.org";
String username = "webadmin@studentwebsite.org";
String password = "student";
String provider = "pop3";
try {
Session session =
Session.getDefaultInstance(props, null);
Store store = session.getStore(provider);
store.connect(host, username, password);
Folder inbox = store.getFolder("INBOX");
if (inbox == null) {
System.out.println("No INBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages( );
for (int j = 0; j < messages.length; j++) {
System.out.println("---------------
Message " + (j+1) + " ---------------");
messages[j].writeTo(System.out);
}
inbox.close(false);
store.close( );
}
catch (Exception er) {
er.printStackTrace( );
}
}
}
Output :

C:\IPLAB>javac ClientPOP3.java
C:\IPLAB>java ClientPOP3
Ex.No: 6 TCP CHAT APPLICATION
Date:
Program: SERVER
import java.io.*;
import java.net.*;
public class server
{
public static void main(String[] args)throws IOException
{
ServerSocket serverSocket=null;
try{
serverSocket=new ServerSocket(4444);}
catch(IOException e){
System.err.println("couldnot listen to port");
System.exit(1);
}
Socket clientSocket=null;
try{
clientSocket=serverSocket.accept();
}
catch(IOException e){
System.err.println("accept failed");
System.exit(1);
}
PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader in=new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String inputline,outputline;
DataInputStream dis=new DataInputStream(System.in);
outputline=dis.readLine();
out.println(outputline);
while((inputline=in.readLine())!=null)
{
System.out.println("fromclient:"+inputline);
outputline=dis.readLine();
out.println(outputline);
if(outputline.equals("bye"))
break;
}
out.close();
in.close();
dis.close();
clientSocket.close();
serverSocket.close();
}
}
CLIENT
import java.io.*;
import java.net.*;
public class client
{
public static void main(String[] args)throws IOException
{
Socket csocket=null;
PrintWriter out=null;
BufferedReader in=null;
try{
csocket=new Socket(InetAddress.getLocalHost(),4444);
out=new PrintWriter(csocket.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(csocket.getInputStream()));
}
catch(UnknownHostException e)
{
System.err.println("error");
System.exit(1);
}
catch(IOException e)
{
System.err.println("could not get io for connection");
System.exit (1);
}
BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));
String fromserver ; String fromuser;
while((fromserver=in.readLine())!=null)
{
System.out.println("server:"+fromserver);
if(fromserver.equals("bye"))break;
fromuser=stdIn.readLine();
if(fromuser!=null){
System.out.println("client:"+fromuser);
out.println(fromuser);
}
}
out.close();
in.close();
stdIn.close();
csocket.close();
}
}
Output:

Server:

Client:
Ex no: 7a INVOKING SERVLETS FROM HTML FORMS
Date:
Program: GET METHOD
<html>
<head>
<title>
HANDLING FROM USING GET
</title >
<body>
<form action="/servlet/passwordcheck" method=GET>
<B>Enter The Username:<input type="text" name="name"></B>
<BR>
<b>Enter the Password:<input type="password' name="pass"></B>
<BR>
<B>Login: <input type:"submit" value="login"></B>
</form>
</body>
</html>

Output:
Ex no: 7b
Program: POST METHOD
<html>
<head>
<title>Post Example</tilte>
</head>
<body bgcolor="aqua">
<form action="http://8080/servlet/form1.java" method="post">
<fieldset>

<legend>
<b>User Information</b>
</legend>
<BR>
<b>Enter Name</b>:<input type:"text" name="id">
<b>Enter Password</b>:<input type="password" name="pwd">
</fieldset>
<b>Enter Your Sex:</b>
<BR>
<input type="radio" name="check"><b>Male</b>
<BR>
<input type="radio" name="check"><b>Female</b>
<BR>
<b>Enter Your Favourite IceCream:</b>
<BR><input type="checkbox" name="cb1"><b>Vanilla</b><BR>
<input type="checkbox" name="cb2"><b>ChocoBar</b><BR>
<input type="checkbox" name="cb3"><b>Pista</b><BR>
<b>Enter The Quantity Of IceCream:</b>
<select name="qty" size=2>
<option selected>Baby</option>
<option selected>Regular></option>
<option selected>Family Pack</option>
</select><BR>
<b>Submit The Details:</b><input type="button" value="Submit"><BR>
<b>Reset The Details:</b> <input type="button" value="Reset"><BR>
</form>
</body>
</html>
Output:
Ex no: 8 THREE- TIER APPLICATION USING SERVLETS
Date:
Program:
<HTML>
<BODY>
<CENTER>
<FORM name = "students" method = "post"
action="http://localhost:8080/Student/Student">
<TABLE>
<tr>
<td><B>Roll No. </B> </td>
<td><input type = "textbox" name="rollno" size="25"
value=""></td>
</tr>
</TABLE>
<INPUT type = "submit" value="Submit">
</FORM>
<CENTER>
</BODY>
</HTML>

Source code in java programming connection to the database using jdbc.odbc


import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
import java.sql.*;
public class Student extends HttpServlet {
Connection dbConn ;
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
dbConn =
DriverManager.getConnection("jdbc:odbc:Student","","") ;
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}

res.setContentType("text/html");
PrintWriter out = res.getWriter();
String mrollno = req.getParameter("rollno") ;
try {
PreparedStatement ps =
dbConn.prepareStatement("select * from stud where
rollno = ?") ;
ps.setString(1, mrollno) ;
ResultSet rs = ps.executeQuery() ;
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border = 1>");
while(rs.next())
{
out.println("<tr><td>Roll No. : </td>");
out.println("<td>" + rs.getString(1) +
"</td></tr>");
out.println("<tr><td>Name : </td>");
out.println("<td>" + rs.getString(2) +
"</td></tr>");
out.println("<tr><td>Branch : </td>");
out.println("<td>" + rs.getString(3) +
"</td></tr>");
out.println("<tr><td>10th Mark : </td>");
out.println("<td>" + rs.getString(4) +
"</td></tr>");
out.println("<tr><td>12th Mark : </td>");
out.println("<td>" + rs.getString(5) +
"</td></tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Output:
Database Structure �� Student.mdb
Records
D:\Student\WEB-INF\classes>javac Student.java
D:\PostParam\WEB-INF>type web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>Student</servlet-name>
<servlet-class>Student</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Student</servlet-name>
<url-pattern>/Student</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
</web-app>

D:\PostParam>jar –cvf Student.war .


D:\Student>jar -cvf Student.war .
added manifest
adding: Student.html(in = 299) (out= 204)(deflated 31%)
adding: WEB-INF/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/Student.class(in = 2658) (out=
1358)(deflated 48%)
adding: WEB-INF/classes/Student.java(in = 1849) (out=
636)(deflated 65%)
adding: WEB-INF/classes/Student.mdb(in = 139264) (out=
7251)(deflated 94%)
adding: WEB-INF/web.xml(in = 666) (out= 312)(deflated 53%)

Step 1: Create ODBC connection for the database


Step 2: Open Web Browser and type
Step 3: http://localhost:8080
Step 4: Select Tomcat Manager
Step 5: Deploy the war file and Run
Ex.No:9 WEBPAGE DESIGN
Date:
Program:
<html>
<head>
<title>WEB DOCUMENT</title>
</head>
<body bgcolor="White">
<h1>SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY</h1>
<h2><B><U>DEPARTMENTS</B></U></h2>
<ol><b><i>
<li>CSE</li>
<li>EEE</li>
<li>ECE</li>
<li>MECH</li>
</b></i>
</ol>
<br>
<h1><I>SEATS AVAILABLE IN EACH DEPARTMENT</I></h1>
<legend><b><u>
AVAILABLE SEATS
</legend></b></u>
<table align="center" border="5" cellspacing="10" cellpadding="5">
<TR>
<TH>Dept</TH>
<TH>CSE</TH>
<TH>ECE</TH>
<TH>EEE</TH>
<TH>MECH</TH>
</TR>
<TR>
<TH>NO.OF SEATS</TH>
<TH>120</TH>
<TH>135</TH>
<TH>86</TH>
<TH>120</TH>
</TR>
</table>
</body>
</html>
Output:
Ex.No:10 WEBPAGE (CASCADING STYLE SHEETS)
Date:
Program: EMBEDDED STYLE SHEET
<html>
<head>
<title>Embedded Style Sheet</title>
<style type="text/css">
em
{
background-color:#ff00ff;
color:white;
width:100%
}
h1
{
font-family:Arial
}
h2
{
font-family:Airal;
color:red;
left:20px
}
h3
{
font-family:Arial;
color:blue;
}
p
{
font-size:14pt;
font-family:verdana
}
.special
{
color:green
}
</style>
</head>
<body>
<em>
<h1 class="special"><center>Technical college of Engineering</center></h1>
</em>
<h2>
General Overview
</h2>
<p>
Technical college of Engineering,Chennai established in 2005 by Mr.Muthukumaran.It is
a non
profitable institute.
This college is situated at the heart of city. The college building is
architecturally designed with 20,000 sq.meters as per AICTE norms. "Excellence
in performance" is the moto of this institution. This institute is always leader in
providing the quality education to both the urban and rural students of
Chennai.
</p>
<p>The college offers courses on ETE,ECE,CSE,IT and Mech engineering.This college
also offers
courses on management branch such as MBA and MCA.</p>
<h3>
<a href="Homepage.html">Back</a>
</h>
</body>
</html>

Output:
Program: EXTERNAL STYLE SHEET
<html>
<head>
<link rel="stylesheet"
typr="text/css"href="E:\DHTML_Examples\ex1.css"/>
</head>
<body>
<em>
<h2>
General Overview
</h2>
<p>
Technical college of Engineering,Chennai established in 2005 by Mr.Muthukumaran.It is
a non profitable institute. This college is situated at the heart of city. The college building
is architecturally designed with 20,000 sq.meters as per AICTE norms. "Excellence in
performance" is the moto of this institution. This institute is always leader in providing
the quality education to both the urban and rural students of Chennai.
</p>
<p>The college offers courses on ETE,ECE,CSE,IT and Mech engineering.This college
also offers
courses on management branch such as MBA and MCA.</p>
<h3>
<a href="Homepage.html">Back</a>
</h>
</body>
</html>

ex1.css
h1
{
font-family:Arial
}
h2
{
font-family:times new roman;
color:red;
left:20px
}
h3
{
font-family:times new roman;
color:blue;
}
p
{
font-size:14pt;
font-family:monotype corsiva
}
.special
{
color:green
}

Output:
Program: INLINE STYLE SHEET
<html>
<head>
<title>My JavaScript Page</title>
</head>
<body>
<em style=background-color:#00ff;color:white;width:100%>
<h1 style=font-family:Arial;color:green>
<center>Technical College of Engineering</center>
</h1>
</em>
<h2 style=font-familt:"Arial";color:red;left:20px>General Overview
</h2>
<p style=font-size:14pt;font-family:"SMIT">Technical college of Engineering,Chennai
established in 2005 by Mr.Muthukumaran.It is a non profitable institute.
This college is situated at the heart of city.The college building is
architecturally designed with 20,000 sq.meters as per AICTE norms."Excellence in
performance"is the moto of this institute.This institute is always leader in
providing the quatity education to both urban and rural students of
Chennai.Our chairman is visionary and he has wide experience of running
many educational institutes.
</p>
<p style=font-size:14pt;font-family:"Verdana">The college offer courses on
ETE,CSE,IT,Mech and ECE.This college also offers courses on management branches
such as MBA and MCA.</p>
<h3 style=font-family:arial;color:blue;>
<a href="homepage.html">Back</a>
</h3>
</body>
</html>
Output:

You might also like