Ads

Tuesday, 23 October 2012

servlet


PROGRAM:
Servlet Code:
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParam extends GenericServlet
{
public void service(ServletRequest request,ServletResponse response) throws ServletException,IOException
{
PrintWriter pw = response.getWriter();
Enumeration e = request.getParameterNames();
while(e.hasMoreElements())
{
String pname = (String)e.nextElement();
pw.print(pname + " = ");
String pvalue = request.getParameter(pname);
pw.println(pvalue);
}
pw.close();
}}
HTML CODE:
<HTML>
<head>
 <TITLE>INVOKING SERVLET FROM HTML</TITLE>
</head>
<BODY>
<CENTER>
<FORM name = "PostParam" method = "Post" action="http://localhost:8080/servlets-examples/servlet/PostParam">
<TABLE>
<tr>
<td><B>Employee </B> </td>
<td><input type = "textbox" name="ename" size="25"
value=""></td>
</tr>
<tr>
<td><B>Phone </B> </td>
<td><input type = "textbox" name="phoneno" size="25"
value=""></td>
</tr>
</TABLE>
<INPUT type = "submit" value="Submit">
</FORM>
</CENTER>
</body>
</html>

CHAT APPLICATION


PROGRAM

Server Side
import java.io.*;
import java.net.*;
class Server
{
public static DatagramSocket serversocket;
public static DatagramPacket dp;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[]=new byte[1024];
public static int cport=789,sport=790;
public static void main(String a[])throws IOException
{
serversocket=new DatagramSocket(sport);
dp=new DatagramPacket(buf,buf.length);
dis=new BufferedReader(new InputStreamReader(System.in));
ia=InetAddress.getLocalHost();
System.out.println("Server is waiting for data from client");
while(true){
serversocket.receive(dp);
String s=new String(dp.getData(),0,dp.getLength());
System.out.println(s);
}}}

Client Side
import java.io.*;
import java.net.*;
class Client{
public static DatagramSocket clientsocket;
public static BufferedReader dis;
public static InetAddress ia;
public static byte buf[]=new byte[1024];
public static int cport=789,sport=790;
public static void main(String a[])throws IOException{
clientsocket = new DatagramSocket(cport);
dis=new BufferedReader(new InputStreamReader(System.in));
ia=InetAddress.getLocalHost();
System.out.println("Client is sending data to Server ");
while(true){                            
String str=dis.readLine();
buf=str.getBytes();
clientsocket.send(new DatagramPacket(buf,str.length(),ia,sport));
}}}

COLOR PALETTE


PROGRAM:

COLOR PALETTE:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//<applet code="color1.class" height=400 width=450></applet>
public class color1 extends Applet implements ItemListener
{         
int currcolor=5;
int flag=1;
String text="check any of the button";
Button buttons[]=new Button[5];
String colors[]={"Red","Blue","Green","Yellow","Orange"};
Image img;
CheckboxGroup cbg=new CheckboxGroup();
Checkbox box1=new Checkbox("Background Color",cbg,true);
Checkbox box2=new Checkbox("Text Color" ,cbg,false);
Checkbox box3=new Checkbox("Loading Image",cbg,false);
public void init(){
for(int i=0;i<5;i++){
buttons[i]=new Button(" ");
add(buttons[i]);}
buttons[0].setBackground(Color.red);
buttons[1].setBackground(Color.blue);
buttons[2].setBackground(Color.green);
buttons[3].setBackground(Color.yellow);
buttons[4].setBackground(Color.orange);
add(box1);
add(box2);
add(box3);
box1.addItemListener(this);
box2.addItemListener(this);
box3.addItemListener(this);}
public void itemStateChanged(ItemEvent e){
if(box1.getState()==true)
flag=1;
else if(box2.getState()==true){
text="defalult color is black";
flag=2;}
else if(box3.getState()==true){
img=getImage(getDocumentBase(),"purple-flowers.jpg");
flag=3;}
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 2:g.setColor(Color.green);
break;
case 3:g.setColor(Color.yellow);
break;
case 4:g.setColor(Color.orange);
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 2:setBackground(Color.green);
break;
case 3:setBackground(Color.yellow);
break;
case 4:setBackground(Color.orange);
break;}}
else if(flag==3){
g.drawImage(img,20,90,this);}}
public boolean action(Event e, Object o){
for(int i=0;i<5;i++){
if(e.target==buttons[i]){
currcolor=i;
text="you have chosen "+colors[i];
repaint();
return true;}}
return false;}}

Color1.html

<html>
<applet code="color1.class" height=200 width=320>
</applet>
</html>

AWT CONTROL


PROGRAMS

CHECK BOX:
import java.awt.*;
import java.awt.event.*;
class checkbox extends Frame implements ActionListener,ItemListener
{
Checkbox x,y,z;
CheckboxGroup a;
Button b1;
public checkbox()
{
b1=new Button("EXIT");
setLayout(new FlowLayout());
a=new CheckboxGroup();
x=new Checkbox("CSE");
y=new Checkbox("IT");
z=new Checkbox("ECE");
add(x);
add(y);
add(z);
add(b1);
x.addItemListener(this);
y.addItemListener(this);
z.addItemListener(this);
setLocation(100,100);
show();
b1.addActionListener(this);}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("EXIT"))
{
System.exit(1);
}}
public void itemStateChanged(ItemEvent e)
{
System.out.println("CSE"+x.getState());
System.out.println("IT"+y.getState());
System.out.println("ECE"+z.getState());
}
public static void main(String ar[])
{
checkbox c=new checkbox();
} }


CHOICE:

import java.awt.*;
import java.awt.event.*;
class choice extends Frame implements ActionListener,ItemListener{
Button b1;
Choice c;
public choice(){
b1=new Button("EXIT");
c=new Choice();
c.add("Green");
c.add("Red");
c.add("Blue");
c.add("Gray");
add(c);
add(b1);
b1.addActionListener(this);
c.addItemListener(this);}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("EXIT")){
System.exit(1);
}}
public void itemStateChanged(ItemEvent b1)
{
if(c.getSelectedItem().equals("Green"))
{
setBackground(Color.green);
}
else if(c.getSelectedItem().equals("Red"))
{
setBackground(Color.red);
}
else if(c.getSelectedItem().equals("Blue")){
setBackground(Color.blue);}
else if(c.getSelectedItem().equals("Gray")){
setBackground(Color.gray);
}}
public static void main(String args[]){
choice t=new choice();
t.setLayout(new FlowLayout());
t.setSize(200,200);
t.setLocation(100,100);
t.show();
}}

LIST:

import java.awt.*;
import java.awt.event.*;
class list extends Frame implements ActionListener
{
Button b1;
public list()
{
b1=new Button("EXIT");
add(b1);
List lst=new List(4,false);
lst.add("CSE");
lst.add("IT");
lst.add("ECE");
lst.add("EEE");
lst.add("CHEM");
add(lst);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("EXIT"))
{
System.exit(1);
}
}
public static void main(String args[])
{
list l=new list();
l.setLayout(new FlowLayout());
l.setSize(400,200);
l.setLocation(100,100);
l.show();
}
}

SCROLL:

import java.awt.*;
import java.awt.event.*;
class Demo extends Frame implements ActionListener
{
Scrollbar sc=new Scrollbar();
Button b1;
public Demo()
{
b1=new Button("EXIT");
add(b1);
FlowLayout f1=new FlowLayout();
setLayout(f1);
add(sc);
b1.addActionListener(this);
setSize(500,500);
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("EXIT"))
{
System.exit(1);
}
}
}

class Scroll
{
public static void main(String args[])
{
Demo s=new Demo();
}
}

TEXT:

import java.awt.*;
import java.awt.event.*;
class text extends Frame implements ActionListener
{
FlowLayout f=new FlowLayout();
TextField t1=new TextField(10);
TextField t2=new TextField(10);
Button b=new Button("COPY");
Button b1=new Button("EXIT");
public text()
{
setLayout(f);
setSize(600,400);
setLocation(200,200);
add(t1);
add(t2);
b.addActionListener(this);
add(b);
b1.addActionListener(this);
add(b1);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("COPY"))
{
t2.setText(t1.getText());
}
else if(e.getActionCommand().equals("EXIT"))
{
System.exit(1);
}
}
public static void main(String args[])
{
text t1=new text();
}
}