Ajp Programs
Ajp Programs
TextField t3,t4,t5,t6,t7;
Button b1,b2;
Checkbox c1,c2,c3,c4,m,f;
CheckboxGroup cbg;
List l1;
Label l2,l3,l4,l5;
TextArea tx1;
l2=new Label("NAME");
l2.setBounds(0,0,50,50);
add(l2);
t3=new TextField(20);
t3.setBounds(130,10,150,20);
add(t3);
l3=new Label("ADDRESS");
l3.setBounds(0,40,70,50);
add(l3);
t4=new TextField(20);
t4.setBounds(130,50,150,20);
add(t4);
l4=new Label("SEX");
l4.setBounds(0,80,70,50);
add(l4);
cbg=new CheckboxGroup();
m=new Checkbox("male",false,cbg);
m.setBounds(130,90,75,20);
add(m);
m.addItemListener(this);
f=new Checkbox("female",false,cbg);
f.setBounds(225,90,75,20);
add(f);
f.addItemListener(this);
l5=new Label("PURSUING");
l5.setBounds(0,120,70,50);
add(l5);
c1= new Checkbox("GRADUATE");
c1.setBounds(80,130,100,20);
add(c1);
c1.addItemListener(this);
c2= new Checkbox(" POST GRADUATE");
c2.setBounds(180,130,130,20);
add(c2);
c2.addItemListener(this);
c3= new Checkbox("UNDER GRADUATE");
c3.setBounds(310,130,130,20);
add(c3);
c3.addItemListener(this);
c4= new Checkbox("PROFESSIONAL");
c4.setBounds(450,130,130,20);
add(c4);
c4.addItemListener(this);
l5=new Label("LANGUAGE KNOWN");
l5.setBounds(0,160,120,50);
add(l5);
l1=new List(4,true);
l1.add("C");
l1.add("C++");
l1.add("JAVA");
l1.add(".NET");
l1.add("FORTRAN");
l1.add("PHP");
l1.add("JAVASCRIPT");
l1.add("C#");
l1.add("COBOL");
l1.setBounds(130,170,150,80);
add(l1);
b1= new Button("SUBMIT");
b1.setBounds(150,280,70,20);
add(b1);
b1.addActionListener(this);
b2= new Button("RESET");
b2.setBounds(300,280,70,20);
add(b2);
b2.addActionListener(this);
tx1=new
TextArea("",10,20,TextArea.SCROLLBARS_BOTH);
tx1.setBounds(0,350,600,100);
add(tx1);
}
String selections[];
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
tx1.insert(t3.getText()+"*********",0);
tx1.insert(t4.getText()+"**********",0);
}
}
}
setLayout(new FlowLayout(FlowLayout.RIGHT));
b1 = new Button("ok");
b2 = new Button("cancel");
b3 = new Button("exit");
add(b1);
add(b2);
add(b3);
}
}
4. Develop a program to create six buttons & apply Grid
Layout.
import java.awt.*;
import java.applet.*;
/*
<applet code="GridLayoutDemo1" width=300
height=300>
</applet>
*/
public class GridLayoutDemo1 extends Applet
{
public void init()
{
setLayout(new GridLayout(4, 4));
Button b1=new Button("one0");
Button b2=new Button("one1");
Button b3=new Button("one2");
Button b4=new Button("one3");
Button b5=new Button("one4");
Button b6=new Button("one5");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
}
}
5. Write a program which will create checkable menu item
„Picture‟ under Insert Menu & „Paste „Menu item under the
menu Home.
import java.awt.*;
class MenuExample
{
MenuExample()
{
Frame f= new Frame("Menu and MenuItem
Example");
MenuBar mb=new MenuBar();
Menu menu1=new Menu("Home");
Menu menu2=new Menu("Insert");
CheckboxMenuItem c=new
CheckboxMenuItem("picture",true);
MenuItem m=new MenuItem("paste");
menu1.add(c);
menu2.add(m);
mb.add(menu1);
mb.add(menu2);
f.setMenuBar(mb);
f.setSize(200,200);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}
}
6. Develop a program that includes three menus such as Page
Layout, References & mailing .Also disable the mailing
menu.
import java.awt.*;
class MenuExample1
{
MenuExample1(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu1=new Menu("Page Layout");
Menu menu2=new Menu("References ");
Menu menu3=new Menu("mailing ");
menu3.setEnabled(false);
mb.add(menu1);
mb.add(menu2);
mb.add(menu3);
f.setMenuBar(mb);
f.setSize(200,200);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample1();
}
}
7. Develop a frame having title as “changing Color” with a
Provision to select a particular among Red, Green & Blue.
Make the use of JComboBox.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
}
public static void main(String args[])
{
ComboBox1 c=new ComboBox1();
}
import javax.swing.*;
/*
<applet code="JTabbedPaneDemo" width=400
height=100>
</applet>
*/
public class JTabbedPaneDemo extends JApplet {
public void init() {
JTabbedPane jtp = new JTabbedPane();
JButton b1 = new JButton("New York");
jtp.addTab("Cities", new CitiesPanel());
jtp.addTab("Colors", new ColorsPanel());
jtp.addTab("Flavors", new FlavorsPanel());
add(jtp);
}
}
}
}
11. Develop a program to create three radio button once
user click on button background color will changes such as
“Red”, “Green” &”Blue”.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class radio extends JApplet implements ItemListener
{
JRadioButton b1,b2,b3;
Container contentPane = getContentPane();
public void init()
{
contentPane.setLayout(new FlowLayout());
b1 = new JRadioButton("RED",true);
contentPane.add(b1);
b2 = new JRadioButton("GREEN",false);
contentPane.add(b2);
b3 = new JRadioButton("BLUE",false);
contentPane.add(b3);
b1.addItemListener(this);
b2.addItemListener(this);
b3.addItemListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);
}
contentPane.setLayout(new FlowLayout());
int v =
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NE
EDED;
int h =
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_
NEEDED;
JScrollPane jsp = new JScrollPane(table, v, h);
contentPane.add(jsp);
}
}
13. Develop a program which wills implements various
methods of Mouse Motion Listener.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MouseEvents" width=300 height=100>
</applet>
*/
public class MouseEvents extends Applet
implements MouseMotionListener {
String msg = "";
int mouseX = 0, mouseY = 0;
public void init() {
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent me) {
showStatus("Dragging mouse at " + me.getX() + ", " +
me.getY());
}
public void mouseMoved(MouseEvent me) {
showStatus("Moving mouse at " + me.getX() + ", " +
me.getY());
}
public void paint(Graphics g) {
g.drawString(msg, mouseX, mouseY);
}
}
14. Develop a program to create an applet to accept a
number in 2 text field & display the larger of two numbers
when a button with the caption “Largest” is pressed.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TwoNumbers extends Applet implements
ActionListener
{
TextField firstNum, secondNum, resultNum;
Button b;
Label l1,l2,l3;
public void init()
{
setLayout(new GridLayout(5,5));
firstNum = new TextField(15);
secondNum = new TextField(15);
resultNum = new TextField(15);
l1=new Label("Enter First Number");
l2=new Label("Enter Second Number");
l3=new Label("Largest no");
b=new Button("Press to find Largest No");
add(l1);
add(firstNum);
add(l2);
add(secondNum);
add(l3);
add(resultNum);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int a=Integer.parseInt(firstNum.getText());
int b=Integer.parseInt(secondNum.getText());
if(a<b)
{
resultNum.setText(Integer.toString(b));
}
else
{
resultNum.setText(Integer.toString(a));
}
}
}
/*<applet code="TwoNumbers" width=400 height=200>
</applet>*/
15. Develop a program to create an applet to accept a
number in a text field & display the square of the number
when a button with caption Square is pressed.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Square extends Applet implements
ActionListener
{
TextField firstNum, resultNum;
Button b;
Label l1,l3;
public void init()
{
a = new Button("yes");
b = new Button("no");
c=new Button("exit");
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
add(a);
add(b);
add(c);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
if(str.equals("yes"))
msg ="You clicked yes button, Thank you for accepting
the agreement";
else if(str.equals("no"))
msg= "You clicked no button, Sorry for no the
agreement";
else
msg= "You clicked exit button, Sorry for exit the
agreement";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,100);
}
}
/*<applet code="ButtonDemo1" width=200 height=200>
</applet>
*/
public void
adjustmentValueChanged(AdjustmentEvent e)
{
repaint();
}
exp10()
{
add(ta,"Center");
add(tf,"South");
add(b,"North");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
ta.setText(" ****following are Contents of web-page
****");
try
{
URL u=new URL(tf.getText());
URLConnection uc=u.openConnection();
InputStream is=uc.getInputStream();
int i;
int len = uc.getContentLength();
if (len != 0)
{
ta.setText(" **** Contents of web-page ****");
while((i=is.read())!=-1)
{
ta.setText(Integer.toString(i));
}
}
else
ta.setText(" **** Contents not found ****");
}catch(MalformedURLException e)
{}
catch(IOException e)
{}
}
});
}
public static void main(String s[])
{
exp10 fr = new exp10();
fr.setSize(300,300);
fr.setVisible(true);
fr.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
24. Develop a program to print protocol, port ,host ,file of
http://www.msbte.com.
import java.io.*;
import java.net.*;
public class exp121
{
public static void main(String[] args)
{
Try
{
URL url=new URL("http://www.msbte.com");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}catch(Exception e){System.out.println(e);
}
}
}
25. Develop a program to send a password through client
to the server & server will send the message to the client
that the password is valid or invalid.
//clientside
import java.net.*;
import java.io.*;
public class Client1
{
public static void main( String args[]) throws
Exception
{
Socket cs = new Socket("localhost",1234);
BufferedReader kb = new BufferedReader(new
InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new
InputStreamReader(cs.getInputStream()));
DataOutputStream dos = new
DataOutputStream(cs.getOutputStream());
}
System.out.println("Terminated..");
cs.close();
dos.close();
kb.close();
}
}
//serverside
import java.net.*;
import java.io.*;
public class Server1
{
public static void main( String args[]) throws
Exception
{
ServerSocket srs = new ServerSocket(1234);
System.out.println("Server is running...");
Socket ss=srs.accept();
System.out.println("connection establised");
BufferedReader kb = new BufferedReader(new
InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new
InputStreamReader(ss.getInputStream()));
DataOutputStream dos = new
DataOutputStream(ss.getOutputStream());
while(true)
{
String s2,s1="password";
while((s2=br.readLine())!=null)
{
System.out.println("Client Entered Password: "+s2);
if(s2.equals(s1))
{
System.out.println("your password is correct");
}
else
{
System.out.println("your password is incorrect");
}
}
System.out.println("Terminated..");
ss.close();
srs.close();
dos.close();
kb.close();
System.exit(0);
}
}
}
}catch(Exception ee){System.out.println(ee);}
}}
import java.sql.*;
import java.sql.Statement;
import java.sql.Connection;
public class update
{
public static void main(String args[]) throws
ClassNotFoundException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("driver LOaded");
String dsn="jdbc:odbc:abc";
Connection con=DriverManager.getConnection(dsn);
Statement st=con.createStatement();
String sql="update product set name='computer' where
name='tv'";
st.executeUpdate(sql);
System.out.println("Table is updated");
st.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
How to run servlet program
Steps:
1.Open Netbeans IDE, Select File -> New Project
2.Select Java Web -> Web Application, then click on
Next,
3.Give a name to your project and click on Next,
4. then, Click Finish
5.The complete directory structure required for the Servlet
Application will be created automatically by the IDE.
6. To create a Servlet, open Source Package, right click
on default packages -> New -> Servlet.
7.Give a Name to your Servlet class file,
8.Now, your Servlet class is ready, and you just need to
change the method definitions and you will good to go.
9. Write some code inside your Servlet class.
10.Under Webpages->WEB-INF->index.html
11.Write some code inside your HTML file.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int n1=Integer.parseInt(request.getParameter("no1"));
int n2=Integer.parseInt(request.getParameter("no2"));
int add=n1+n2;
int sub=n1-n2;
int mul=n1*n2;
int div=n1/n2;
out.println("Addition is"+add);
out.println("Subtraction is"+sub);
out.println("multiplication is"+mul);
out.println("Division is"+div);
%>
</body>
</html>