Вы находитесь на странице: 1из 35

INDEX

S.No

NAME OF THE PROGRAMS

Program for learning basics of java language and its


library.

Program to create an applet with a textfield and


three buttons,when you press each button make
some different text appear in the textfield.

Program to create an applet with a button and a


textfield,write a handle event so that if the button
have been focused,character types into it will appear
in textfield

Program to show connectivity of java with database

Program to create an editor screen containing


menus,dialog boxes etc using java

Program to show Java Networking-Java Sockets

and RMI
Program to develop a servlet that get invoked when
a form on a web page in HTML is submitted.

SIGNATURE

Learn basics of java language and its development


tools/libraries.
Introduction:
Java is an object-oriented programming language with a built-in application
programming interface (API) that can handle graphics and user interfaces and that can be
used to create applications or applets. Because of its rich set of API's, similar to
Macintosh and Windows, and its platform independence, Java can also be thought of as a
platform in itself. Java also has standard libraries for doing mathematics

Need for Java:


The java language contains built in support for the World Wide Web(WWW), which is a
service of the internet to retrieve information in the form of the Web pages. The primary
motive behind developing Java language was the need for portable and platform
independent language that could be used to produce code that run on a variety of CPU
under different environments. One can use Java to develop Network oriented programs
because networking features are built in features in Java.

Features of Java :

Simple
Object-Oriented
Compiled and interpreted
robust
Multithreaded
Platform Independent
High Performance
Distributed
Secure
Portable

SIMPLE
A java programmer does not need to know the interior details of java as to how memory
is allocated to data because in java, The programmer does not need to handle memory
manipulation. Java is a simple language that can be learn easily even if u have just started
programming. It was designed to be easy for the programmer to learn and use effectively.
The syntax for various java statements is easy to understand.
OBJECT ORIENTED
Java supports the object oriented approach to develop programs.It supports various
features of an object oriented language. To implement the object oriented language the
entire code of the program must be written within the class. The java language does not

support stand-alone statements. Even the most basic program in java must be within a
class .
COMPILED AND INTERPRETED
The java programs are first compiled and then interpreted while compiling,the compiler
checks for the error in the programs. After you have made the program error free and
have recompiled it, the compiler converts the program into computer language. The java
compiler compiles the code to a byte code that is understood by the java. The java virtual
machine then interprets this bytecode into the computer code and runs it.
PORTABLE
Portability refers to the ability of the program to run on any platform without changing
the source code of the program.The java enables the creation of cross platform programs
by compiling the programs into an intermediate representation called java bytecode. You
can execute this code on any platform.
PLATFORM INDEPENDENT
Java is a platform for application development. Java byte code is exactly the same on
every platform. Java programs that have been compiled into byte code still need an
interpreter to execute them on any given platform. The interpreter reads the byte code and
translates it into the native language of the host machine on the fly. Since the byte code is
completely platform independent, only the interpreter and a few native libraries need to
be ported to get Java to run on a new computer or operating system.
ROBUST
Java implements a robust exception handling mechanism to deal with both expected and
unexpected errors. The worst that an applet can do to a host system is bringing down the
runtime environment. It cannot bring down the entire system.
Most importantly Java applets can be executed in an environment that prohibits them
from introducing viruses, deleting or modifying files, or otherwise destroying data and
crashing the host computer. A Java enabled web browser checks the byte codes of an
applet to verify that it doesn't do anything nasty before it will run the applet.
MULTITHREAD
Java is inherently multi-threaded. A single Java program can have many different threads
executing independently and continuously. Three Java applets on the same page can run
together with each getting equal time from the CPU with very little extra effort on the
part of the programmer.
HIGH PERFORMANCE
Java byte codes can be compiled on the fly to code that rivals C++ in speed using a "justin-time compiler." Several companies are also working on native-machine-architecture
compilers for Java. These will produce executable code that does not require a separate
interpreter, and that is indistinguishable in speed from C++.

Some important concept of OOPS


1 Encapsulation
2 Abstraction
3 Inheritance
4 Polymorphism
ENCAPSULATION
Encapsulation is the process of hiding all of the details of an object that do not contribute
to its essential characteristics.Encapsulation implies that the non-essential details of an
object are hidden from the user and an access is provided to its essential details. The
complexity is hidden from the user as a result of encapsulation.In object oriented
methodology, need of encapsulation arises because the emphasis is on designing classes
in such a manner that the classes share data and methods among themselves.
Encapsulation is the feature that provides security to the data and the methods of a class.
ABSTRACTION
An abstraction denotes the essential characteristics of an object that uniquely
distinguishes it from all other kinds of objects and thus crisply defined conceptual
boundaries ,relative to the perspective of the viewer. Abstraction refers to the attributes of
an object that clearly demarcates it from other objects .To implement abstraction, we also
use the encapsulation feature .encapsulation hides the irrelevant details of an object and
abstraction makes only the relevant details of an object visible .We use access specifier to
implement abstraction and encapsulation.
INHERITANCE
In object-oriented methodology, inheritance enables you to extend the functionality of an
existing class. We create a class that inherits the attributes and the behavior of another
class .In addition, the new class can consist of a new attributes and the behaviors that are
specific to the class .In terms of classes and object, attributes refer to the methods.
A program can have following types of inheritance:
(a.) Single inheritance
(b.)Multiple inheritance
POLYMORPHISM
Polymorphism is derived from two Latin words poly ,which means many , and morph,
which means forms .Any thing that exist in more than one form is known as polymorph .
In object-oriented methodology, polymorphism is the feature that enables us to assign a
different meaning or usage to an entity in different contexts. The entity can be a variable ,
method ,or an object .Polymorphism enables an entity to have more than one form

depending upon the context in which it is used . Polymorphism enables one entity to be
used as a general category for different types of actions.

Java Architecture
Various components of Java architecture are:
1.Java programming language and Java class file
2. Java platform
Java Programming Language and class file:
Java programs are saved with an extension ,.java. A .java file is compiled to generate
the .class file, which contains the bytecode. The JVM converts the Bytecode contained in
the .class file to machine object code. The JVM needs to be implemented for each
platform running on different operating systems.
Java platform
A java platform is the hardware or software environment in which a program runs. The
java platform has two components:
Java Virtual Machine (JVM)
Java Application Programming Interface (Java API)
JVM is a standardized hypothetical computer, which is emulated inside your computer by
a program.

Java
Source
Code

Java
Compiler

Java Virtual Machine


Java interpreter

The program executed


By the interpreter

Computer Operating System

Java Virtual Machine:

Java
Object
Code

The JVM forms the base for the java platform and is convenient to use on various
hardware based platforms.

Components of JVM:
1.Class loader
The class loader loads the class files, which are required by the program running in the
memory. The classes are loaded dynamically when required by the running program.
2. Execution engine
The Java execution engine is the component of the JVM that runs the bytecode one line
after another. It converts the bytecode into the machine object code and runs it.
3. Just In Time(JIT) compiler
The JIT is used for compiling the bytecode into the executable code. The JVM runs the
JIT compiled code without interpreting because the JIT compiled code is in the machine
code format.
Java Application Programming Interface:
The Java API is a collection of software components that provide capabilities, such as
GUI. The related classes and interfaces of the Java API are grouped into packages.

Advantages of Object Oriented Programming:


1. Real World Programming
2. Reusability of the code
3. Modularity of the code

4. Resillence to change
5. Information hiding

Java applications
1.Applications that use Character User Interface(CUI)
Applications are executable programs that are controlled by operating system.
2. Applications that use Graphical User Interface(GUI)
These applications are used in Windows Environment.In GUI, you can interact with the
application in graphical mode.
3. Applets
Applets are small executable programs that run a web page.
4. Servlets
Servlets are the programs that are used to extend the functionality of the web servers.
5.Packages
Packages are collection of classes that are reused by applications and applets

Program-1
// Program for learning basics of java language and its library.
// 1st class
class Box
{
double width;
double depth;
double height;
Box(Box obj1)
{
width=obj1.width;
depth=obj1.depth;
height=obj1.height;
}
Box(double w,double d,double h)
{
width=w;
depth=d;
height=h;
}
Box()
{
width=-1;
depth=-1;
height=-1;
}
double volume( )
{
return(width*depth*height);
}
}
//2nd class inheriting features from Box class
class BoxWeight extends Box
{
double mass;
BoxWeight(BoxWeight obj2)

{
super(obj2);
mass=obj2.mass;
}
BoxWeight(double w,double d,double h,double m)
{
super(w,d,h);
mass=m;
}
BoxWeight()
{
super();
mass=-1;
}
}
//3rd class inheriting features from BoxWeight class
class Shipment extends BoxWeight
{
double cost;
Shipment(Shipment obj3)
{
super(obj3);
cost=obj3.cost;
}
Shipment(double w,double d,double h,double m,double c)
{
super(w,d,h,m);
cost=c;
}
Shipment()
{
super();
cost=-1;
}
}
//main class
class DemoShipment
{
public static void main(String args[])
{
Shipment shipment1=new Shipment(20,30,40,50,10);
double vol1=shipment1.volume();
Shipment shipment2=new Shipment(shipment1);
double vol2=shipment2.volume();

Shipment shipment3=new Shipment();


double vol3=shipment3.volume();
System.out.println("1st Volume is"+vol1);
System.out.println("2nd Volume is"+vol2);
System.out.println("3rd Volume is"+vol3);
System.out.println("1st Weight is"+shipment1.mass);
System.out.println("2nd Weight is"+shipment2.mass);
System.out.println("3rd Weight is"+shipment3.mass);
}
}

OUTPUT:

Program-2
/* Program to create an applet with a textfield and three buttons,when you press each button make
some different text appear in the textfield.Add a checkbox to the applet created,capture the
event,and insert different text into the textfield.*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="button.java" height="200" width="200"> </applet>*/
public class button extends Applet implements ActionListener,ItemListener
{
TextField t1;
Button b1,b2,b3;
Checkbox c1,c2,c3;
BorderLayout b;
Panel p1,p2;
//applet code=button.java width=600 height=300> </applet>
public void init()
{
b=new BorderLayout();
t1=new TextField(10);
t1.setEditable(false);
b1=new Button("Click Here");
b2=new Button("Now Click Here");
b3=new Button("And Click Here");
c1=new Checkbox("Click Here");
c2=new Checkbox("Now Click Here");
c3=new Checkbox("And Click Here");
setLayout(b);
}
public void start()
{
p1=new Panel();
p1.add(t1);
p1.add(b1);
p1.add(b2);
p1.add(b3);

add(p1,BorderLayout.NORTH);
p2=new Panel();
p2.add(c1);
p2.add(c2);
p2.add(c3);
add(p2,BorderLayout.SOUTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t1.setText("OK");
if(e.getSource()==b2)
t1.setText("Cancel");
if(e.getSource()==b3)
t1.setText("Help");
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c1)
t1.setText("File");
if(ie.getSource()==c2)
t1.setText("edit");
if(ie.getSource()==c3)
t1.setText("view");
}
}

OUTPUT:

Program-3
/*Program to create an applet with a button and a textfield,write a handle event so that if
the button have been focused,character types into it will appear in textfield*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="focus" width=300 height=500> </applet>*/
public class focus extends Applet implements FocusListener
{
TextField t1;
Button b1,b2;
public void init()
{
t1=new TextField(10);
b1=new Button("cancel");
b2=new Button("help");
t1.setEditable(false);
add(t1);
add(b1);
add(b2);
b1.addFocusListener(this);
b2.addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
if(e.getSource()==b1) t1.setText("cancel");
if(e.getSource()==b2) t1.setText("help");
}
public void focusLost(FocusEvent ae)
{
if(ae.getSource()==b1) t1.setText("");
if(ae.getSource()==b2) t1.setText("");
}
}

OUTPUT:

Program-4
//Program to show connectivity of java with database
import java.io.*;
import java.sql.*;
class data
{
public static void main(String arc[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:man");
PreparedStatement ps;
ResultSet rs;
while(true)
{
System.out.println("\n\n");
System.out.println("
1> VIEW CONTACTS");
System.out.println("
2> ADD CONTACTS");
System.out.println("
3> DELETE CONTACTS");
System.out.println("
4> EXIT");
System.out.print("
ENTER YOUR CHOICE ");
DataInputStream br=new DataInputStream(System.in);
String a=br.readLine();
int a1=Integer.parseInt(a);
switch(a1)
{
case 1:
Statement st=con.createStatement();
rs=st.executeQuery("select sno,name,address,tno,emailid from mang");
System.out.println("\n\n");
System.out.println("--------------------------------------");
System.out.println("S.NO"+"\t"+"NAME"+"\t"+"ADDRESS"+"\t"+"TEL.NO"+"\t"+"E
MAIL ID");
while(rs.next())
{
int s1=rs.getInt(1);
String s2=rs.getString(2);

String s3=rs.getString(3);
int s4=rs.getInt(4);
String s5=rs.getString(5);
System.out.println(s1+"\t"+s2+"\t"+s3+"\t"+s4+"\t"+s5);
}
break;
case 2:
int b1=0;
ps=con.prepareStatement("select sno from mang");
rs=ps.executeQuery();
while(rs.next())
{
b1++;
}
System.out.print("ENTER NAME ");
String b2=br.readLine();
System.out.print("ENTER ADDRESS ");
String b3=br.readLine();
System.out.print("ENTER TELEPHONE NUMBER ");
String b4=br.readLine();
int b5=Integer.parseInt(b4);
System.out.print("ENTER EMAIL ID ");
String b6=br.readLine();
ps=con.prepareStatement("insert into mang values(?,?,?,?,?)");
ps.setInt(1,b1);
ps.setString(2,b2);
ps.setString(3,b3);
ps.setInt(4,b5);
ps.setString(5,b6);
ps.executeUpdate();
con.commit();
System.out.println("VALUES ENTERED SUCCESSFULLY");
break;
case 3:
ps=con.prepareStatement("delete from mang where sno=?");
System.out.print("ENTER SNO TO DELETE ");
int m=Integer.parseInt(br.readLine());
ps.setInt(1,m);
ps.executeUpdate();
con.commit();
System.out.println("DELETED SUCCESSFULLY");
break;
default:
System.out.println("EXITED SUCCESSFULLY");

con.close();
System.exit(0);
}
}
}
catch(Exception e)
{
System.out.println("Exception Caught");
}
}
}

OUTPUT:

Program-5
/*Program to create an editor screen containing menus,dialog boxes etc using java*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="DialogDemo.java" height="200" width="200"> </applet>*/
class SampleDialog extends Dialog implements ActionListener
{
SampleDialog (Frame parent,String title)
{
super(parent,title,false);
setLayout(new FlowLayout());
setSize(300,200);
add(new Label("press this Buttom:"));
Button b;
add(b=new Button("Cancel"));
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
public void paint(Graphics g)
{
g.drawString("This is in the dialog box",10,70);
}
}
class MenuFrame extends Frame
{
String msg=" ";
CheckboxMenuItem debug, test;
MenuFrame(String title)
{
super(title);
MenuBar mbar=new MenuBar();
setMenuBar(mbar);
Menu file=new Menu("File");
MenuItem item1,item2,item3,item4;
file.add(item1=new MenuItem("New"));
file.add(item2=new MenuItem("Open"));
file.add(item3=new MenuItem("Close"));

file.add(new MenuItem("-"));
file.add(item4=new MenuItem("Quit"));
mbar.add(file);
Menu edit=new Menu("Edit");
MenuItem item5,item6,item7;
edit.add(item5=new MenuItem("Cut"));
edit.add(item6=new MenuItem("Copy"));
edit.add(item7=new MenuItem("Paste"));
edit.add(new MenuItem("-"));
Menu sub=new Menu("Special",true);
MenuItem item8,item9,item10;
sub.add(item8=new MenuItem("First"));
sub.add(item9=new MenuItem("Second"));
sub.add(item10=new MenuItem("Third"));
edit.add(sub);
debug=new CheckboxMenuItem("Debug");
edit.add(debug);
test=new CheckboxMenuItem("Testing");
edit.add(test);
mbar.add(edit);
MyMenuHandler handler=new MyMenuHandler(this);
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
debug.addItemListener(handler);
test.addItemListener(handler);
MyWindowAdapter adapter=new MyWindowAdapter(this);
addWindowListener(adapter);
}
public void paint(Graphics g)
{
g.drawString(msg,10,200);
if(debug.getState())
g.drawString("Debug Is On",10,220);
else

g.drawString("Debug Is Off",10,220);
if(test.getState())
g.drawString("Testing Is On",10,240);
else
g.drawString("Testing Is Off",10,240);
}
}
class MyWindowAdapter extends WindowAdapter
{
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
public void windowClosing(WindowEvent we)
{
menuFrame.dispose();
}
}
class MyMenuHandler implements ActionListener,ItemListener
{
MenuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
public void actionPerformed(ActionEvent ae)
{
String msg = "YOU SELECTED";
String arg = (String)ae.getActionCommand();
if(arg.equals("New"))
{
msg+="New";
SampleDialog d=new SampleDialog(menuFrame,"New
DialogBox");
d.setVisible(true);
}
else if(arg.equals("Open"))
msg+="Open";
else if(arg.equals("Close"))
msg+="Close";
else if(arg.equals("Quit"))

msg+="Quit";
else if(arg.equals("Edit"))
msg+="Edit";
else if(arg.equals("Cut"))
msg+="Cut";
else if(arg.equals("Copy"))
msg+="Copy";
else if(arg.equals("Paste"))
msg+="Paste";
else if(arg.equals("First"))
msg+="First";
else if(arg.equals("Second"))
msg+="Second";
else if(arg.equals("Third"))
msg+="Third";
else if(arg.equals("Debug"))
msg+="Debug";
else if(arg.equals("Testing"))
msg+="Testing";
menuFrame.msg=msg;
menuFrame.repaint();
}
public void itemStateChanged(ItemEvent ie)
{
menuFrame.repaint();
}
}
public class DialogDemo extends Applet
{
Frame f;
public void init()
{
f=new MenuFrame("Menu Demo");
int width=Integer.parseInt(getParameter("width"));
int height=Integer.parseInt(getParameter("height"));
setSize(width,height);
f.setSize(width,height);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{

f.setVisible(false);
}
}

OUTPUT:

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
/*<applet code="CustomerApplet.java" height="200" width="200"> </applet>*/
class Customer extends Object implements java.io.Serializable
{
String custName;
String custPassword;
}
public class CustomerApplet extends JApplet
{
JPanel panelObject;
JLabel labelCustName;
JLabel labelCustPassword;
JTextField textCustName;
JPasswordField textCustPassword;
JButton buttonLogin;
GridBagLayout g1;
GridBagConstraints gbc;
public void init()
{
g1=new GridBagLayout();
gbc=new GridBagConstraints();
panelObject=(JPanel)getContentPane();
panelObject.setLayout(g1);
labelCustName=new JLabel("Customer Login Name");
labelCustPassword=new JLabel("Password");
textCustName=new JTextField(15);
textCustPassword=new JPasswordField(15);
buttonLogin=new JButton("login");
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=5;
g1.setConstraints(labelCustName,gbc);

panelObject.add(labelCustName);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=5;
g1.setConstraints(textCustName,gbc);
panelObject.add(textCustName);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=9;
g1.setConstraints(labelCustPassword,gbc);
panelObject.add(labelCustPassword);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=9;
g1.setConstraints(textCustPassword,gbc);
panelObject.add(textCustPassword);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=2;
gbc.gridy=13;
g1.setConstraints(buttonLogin,gbc);
panelObject.add(buttonLogin);
LoginAction loginrequest=new LoginAction();
buttonLogin.addActionListener(loginrequest);
}
class LoginAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==buttonLogin)
{
Customer data=new Customer();
data.custName=textCustName.getText();
data.custPassword=new String(textCustPassword.getPassword());

try
{
Socket toServer;
toServer=new Socket("192.168.0.1",1001);
ObjectOutputStream streamToServer=new
ObjectOutputStream(toServer.getOutputStream());
streamToServer.writeObject((Customer )data);
BufferedReader fromServer=new BufferedReader(new
InputStreamReader(toServer.getInputStream()));
String status=fromServer.readLine();
getAppletContext().showStatus(status);
streamToServer.close();
fromServer.close();
}
catch(InvalidClassException e)
{
getAppletContext().showStatus("The Customer Name is Valid"+e);
}
catch(NotSerializableException e)
{
getAppletContext().showStatus("The Object is not serializable"+e);
}
catch(IOException e)
{
getAppletContext().showStatus("Cannot write to server"+e);
}
}
}
}
}

Program-6
//Program to show Java Networking-Java Sockets and RMI
import java.awt.event.*;
import java.io.*;
import java.net.*;
class Customer implements Serializable
{
String custName;
String custPassword;
}
public class AppServer extends Thread
{
ServerSocket serverSocket;
public void Appserver()
{
try
{
serverSocket=new ServerSocket(1001);
}
catch(IOException e)
{
fail(e,"could not start server.");
}
System.out.println("server Started......");
this.start();
}
public static void fail(Exception e,String str)
{
System.out.println(str+"."+e);
}
public void run()
{
try
{
while(true)
{
Socket client=serverSocket.accept();
Connection con=new Connection(client);
}
}
catch(IOException e)
{
fail(e,"Not Listening");
}
}
public static void main(String args[])
{
new AppServer();
}
}
class Connection extends Thread

protected Socket netClient;


protected ObjectInputStream fromClient;
protected PrintStream toClient;
public Connection(Socket client)
{
netClient=client;
try
{
fromClient=new ObjectInputStream(netClient.getInputStream());
toClient=new PrintStream(netClient.getOutputStream());
}
catch(IOException e)
{
try
{
netClient.close();
}
catch(IOException e1)
{
System.err.println("unable to set up streams"+e1);
return;
}
}
this.start();
}
public void run()
{
Customer clientMessage;
try
{
for(;;)
{
clientMessage=(Customer)fromClient.readObject();
if(clientMessage==null)
break;
toClient.println("Received
from:"+clientMessage.custName);
}
}
catch(IOException e)
{}
catch(ClassNotFoundException e1)
{
System.out.println("Error in reading object"+e1);
}
finally
{
try
{
netClient.close();
}
catch(IOException e)
{}
}
}
}

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
/*<applet code="CustomerApplet.java" height="200" width="200"> </applet>*/
class Customer extends Object implements java.io.Serializable
{
String custName;
String custPassword;
}
public class CustomerApplet extends JApplet
{
JPanel panelObject;
JLabel labelCustName;
JLabel labelCustPassword;
JTextField textCustName;
JPasswordField textCustPassword;
JButton buttonLogin;
GridBagLayout g1;
GridBagConstraints gbc;
public void init()
{
g1=new GridBagLayout();
gbc=new GridBagConstraints();
panelObject=(JPanel)getContentPane();
panelObject.setLayout(g1);
labelCustName=new JLabel("Customer Login Name");
labelCustPassword=new JLabel("Password");
textCustName=new JTextField(15);
textCustPassword=new JPasswordField(15);
buttonLogin=new JButton("login");
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=5;
g1.setConstraints(labelCustName,gbc);

panelObject.add(labelCustName);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=5;
g1.setConstraints(textCustName,gbc);
panelObject.add(textCustName);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=9;
g1.setConstraints(labelCustPassword,gbc);
panelObject.add(labelCustPassword);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=9;
g1.setConstraints(textCustPassword,gbc);
panelObject.add(textCustPassword);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=2;
gbc.gridy=13;
g1.setConstraints(buttonLogin,gbc);
panelObject.add(buttonLogin);
LoginAction loginrequest=new LoginAction();
buttonLogin.addActionListener(loginrequest);
}
class LoginAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==buttonLogin)
{
Customer data=new Customer();
data.custName=textCustName.getText();
data.custPassword=new String(textCustPassword.getPassword());

try
{
Socket toServer;
toServer=new Socket("192.168.0.1",1001);
ObjectOutputStream streamToServer=new
ObjectOutputStream(toServer.getOutputStream());
streamToServer.writeObject((Customer )data);
BufferedReader fromServer=new BufferedReader(new
InputStreamReader(toServer.getInputStream()));
String status=fromServer.readLine();
getAppletContext().showStatus(status);
streamToServer.close();
fromServer.close();
}
catch(InvalidClassException e)
{
getAppletContext().showStatus("The Customer Name is Valid"+e);
}
catch(NotSerializableException e)
{
getAppletContext().showStatus("The Object is not serializable"+e);
}
catch(IOException e)
{
getAppletContext().showStatus("Cannot write to server"+e);
}
}
}
}
}

OUTPUT:

Program-7
/*Program to develop a servlet that get invoked when a form on a web page in HTML is
submitted.Create a cookie object and enter/ display value for that cookie.*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet
{
public void doGet(HttpServletREquest req,HttpServletResponse res)throws
ServletException,IOException;
res.setContentType("Text/Html");
Printwriter out=res.getWriter();
"<html> \n"+
"<head><title>Hello</title></head> \n"+
"<body bgcolor=">"
out.println("Hello");
out.println("</body></html>");
}

Вам также может понравиться