CBT lab
-
Upload
kalaivani-vasudevan -
Category
Documents
-
view
215 -
download
0
Transcript of CBT lab
-
7/28/2019 CBT lab
1/29
Program
FileInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FileInterface extends Remote {
public byte[] downloadFile(String fileName) throws
RemoteException;
}
FileImpl.java
import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class FileImpl extends UnicastRemoteObject
implements FileInterface {
private String name;
public FileImpl(String s) throws RemoteException{
super();
name = s;
}
public byte[] downloadFile(String fileName){
try {
-
7/28/2019 CBT lab
2/29
File file = new File(fileName);
byte buffer[] = new byte[(int)file.length()];
BufferedInputStream input = new
BufferedInputStream(new FileInputStream(fileName));
input.read(buffer,0,buffer.length);
input.close();
return(buffer);
} catch(Exception e){
System.out.println("FileImpl: "+e.getMessage());
e.printStackTrace();
return(null);
}
}
}
FileServer.java
import java.io.*;
import java.rmi.*;
public class FileServer {
public static void main(String argv[]) {
try {
-
7/28/2019 CBT lab
3/29
FileInterface fi = new FileImpl("FileServer");
Naming.rebind("//127.0.0.1/FileServer", fi);
} catch(Exception e) {
System.out.println("FileServer: "+e.getMessage());
e.printStackTrace();
}
}
}
FileClient.java
import java.io.*;
import java.rmi.*;
public class FileClient{
public static void main(String argv[]) {
if(argv.length != 2) {
System.out.println("Usage: java FileClient fileName
machineName");
System.exit(0);
}
try {
String name = "//" + argv[1] + "/FileServer";
FileInterface fi = (FileInterface) Naming.lookup(name);
byte[] filedata = fi.downloadFile(argv[0]);
System.out.println(enter the file to download);
BufferedReader br = new BufferedReader(new
InputStreamReader(system.in));
String newname = br.readLine();
-
7/28/2019 CBT lab
4/29
File file = new File(newname);
BufferedOutputStream output = new
BufferedOutputStream(new FileOutputStream(file.getName()));
output.write(filedata,0,filedata.length);
output.flush();
output.close();
} catch(Exception e) {
System.err.println("FileServer exception: "+ e.getMessage());
e.printStackTrace();
}
}
}
Execution
Server folder [interface.java,fileimpl.java,file server.java]
1. Compile all the java program using (javac filename.java)
2. enter the (start rmiregistry) in command line
3. create stub and skeleton using (rmic FileImpl)
4.create one file i.e a.txt in server folder
5. Run the server using (java FileServer)
-
7/28/2019 CBT lab
5/29
Client folder[file interface.java,fileclient.java,copy the stub and
skeleton from server folder and paste it in client folder]
1. Compile all the java program using (javac filename.java)
(The next contains two parameters First is the text file created
by the user second is machine IP Address)
2. Run the Client using (java FileClient a.txt 192.168.0.154)
7. next step you will give the file name to download the file (Enterthe file name to download : b.txt)
1.a.txt and b.txt contains the same content.
OUTPUT:-
-
7/28/2019 CBT lab
6/29
-
7/28/2019 CBT lab
7/29
-
7/28/2019 CBT lab
8/29
-
7/28/2019 CBT lab
9/29
-
7/28/2019 CBT lab
10/29
JText Field
import java.awt.*;
import javax.swing.*;
/*
*/
public class JTextFieldDemo extends JApplet {
JTextField jtf;
public void init() {
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
// Add text field to content pane
jtf = new JTextField(15);
contentPane.add(jtf);
}
}
-
7/28/2019 CBT lab
11/29
2
JBUTTON
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
*/
public class JButtonDemo extends JApplet
implements ActionListener {
JTextField jtf;
public void init() {
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
// Add buttons to content pane
ImageIcon france = new ImageIcon("france.gif");
JButton jb = new JButton(france);
-
7/28/2019 CBT lab
12/29
jb.setActionCommand("France");
jb.addActionListener(this);
contentPane.add(jb);
ImageIcon germany = new ImageIcon("germany.gif");
jb = new JButton(germany);
jb.setActionCommand("Germany");
jb.addActionListener(this);
contentPane.add(jb);
ImageIcon italy = new ImageIcon("italy.gif");
jb = new JButton(italy);
jb.setActionCommand("Italy");
jb.addActionListener(this);
contentPane.add(jb);
ImageIcon japan = new ImageIcon("japan.gif");
jb = new JButton(japan);
jb.setActionCommand("Japan");
jb.addActionListener(this);
contentPane.add(jb);
// Add text field to content pane
jtf = new JTextField(15);
contentPane.add(jtf);
}
public void actionPerformed(ActionEvent ae) {
jtf.setText(ae.getActionCommand());
}
}
-
7/28/2019 CBT lab
13/29
3
JCheck Boxes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
*/
public class JCheckBoxDemo extends JApplet
implements ItemListener {
JTextField jtf;
public void init() {
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
// Create icons
ImageIcon normal = new ImageIcon("normal.gif");
ImageIcon rollover = new ImageIcon("rollover.gif");
-
7/28/2019 CBT lab
14/29
ImageIcon selected = new ImageIcon("selected.gif");
// Add check boxes to the content pane
JCheckBox cb = new JCheckBox("C", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);
cb = new JCheckBox("C++", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);
cb = new JCheckBox("Java", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);
cb = new JCheckBox("Perl", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);
// Add text field to the content pane
jtf = new JTextField(15);
contentPane.add(jtf);
}
public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox)ie.getItem();
jtf.setText(cb.getText());
}
}
-
7/28/2019 CBT lab
15/29
4
JRadio Button
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
*/
public class JRadioButtonDemo extends JApplet
implements ActionListener {
JTextField tf;
public void init() {
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
// Add radio buttons to content pane
JRadioButton b1 = new JRadioButton("A");
b1.addActionListener(this);
contentPane.add(b1);
-
7/28/2019 CBT lab
16/29
JRadioButton b2 = new JRadioButton("B");
b2.addActionListener(this);
contentPane.add(b2);
JRadioButton b3 = new JRadioButton("C");
b3.addActionListener(this);
contentPane.add(b3);
// Define a button group
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);
// Create a text field and add it
// to the content pane
tf = new JTextField(5);
contentPane.add(tf);
}
public void actionPerformed(ActionEvent ae) {
tf.setText(ae.getActionCommand());
}
}
-
7/28/2019 CBT lab
17/29
5
JCOMBO BOX
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
*/
public class JComboBoxDemo extends JApplet
implements ItemListener {
JLabel jl;
ImageIcon france, germany, italy, japan;
public void init() {
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
// Create a combo box and add it
// to the panel
JComboBox jc = new JComboBox();
jc.addItem("France");
jc.addItem("Germany");
jc.addItem("Italy");
jc.addItem("Japan");
jc.addItemListener(this);
contentPane.add(jc);
// Create label
jl = new JLabel(new ImageIcon("france.gif"));
contentPane.add(jl);
}
public void itemStateChanged(ItemEvent ie) {
-
7/28/2019 CBT lab
18/29
String s = (String)ie.getItem();
jl.setIcon(new ImageIcon(s + ".gif"));
}
}
6
JTREE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
/*
*/
public class JTreeEvents extends JApplet {
JTree tree;
JTextField jtf;
public void init() {
// Get content pane
-
7/28/2019 CBT lab
19/29
Container contentPane = getContentPane();
// Set layout manager
contentPane.setLayout(new BorderLayout());
// Create top node of tree
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
// Create subtree of "A"
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
// Create subtree of "B"
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
// Create tree
tree = new JTree(top);
// Add tree to a scroll pane
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(tree, v, h);
// Add scroll pane to the content pane
contentPane.add(jsp, BorderLayout.CENTER);
// Add text field to applet
jtf = new JTextField("", 20);
contentPane.add(jtf, BorderLayout.SOUTH);
// Anonymous inner class to handle mouse clicks
tree.addMouseListener(new MouseAdapter() {
-
7/28/2019 CBT lab
20/29
public void mouseClicked(MouseEvent me) {
doMouseClicked(me);
}
});
}
void doMouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
if(tp != null)
jtf.setText(tp.toString());
else
jtf.setText("");
}
}
-
7/28/2019 CBT lab
21/29
ServletFormhandling:-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FormHandlerServlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
String enteredValue;
// gets all the selected options from the client browser
String[] selectedOptions = request.getParameterValues("options");
// gets the enteredValue fields value
enteredValue = request.getParameter("enteredValue");
response.setContentType("text/html");
PrintWriter printWriter;
try
{
-
7/28/2019 CBT lab
22/29
// get a printwriter from the HttpServletResponse objects ref.
printWriter = response.getWriter();
// return on the HttpServletResponse objects ref. requested values
printWriter.println("
");
printWriter.print("You entered: ");
printWriter.print(enteredValue);
printWriter.print("
");printWriter.println("
");
printWriter.print("The following options were selected:");
printWriter.println("
");if (selectedOptions != null)
{
for (String option : selectedOptions)
{
printWriter.print(option);
printWriter.println("
");}
}
else
{
printWriter.println("None");
-
7/28/2019 CBT lab
23/29
}
printWriter.println("
");}
catch (IOException e)
{
e.printStackTrace();
}
}
}
-
7/28/2019 CBT lab
24/29
Login.html
Login
Login
Please enter your username and password
-
7/28/2019 CBT lab
25/29
GetSession.java
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class GetSession extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
// gets the session if it exists
HttpSession session = request.getSession(false);
try {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("");
// If you are not in a session - you are not logged in
if (session == null) {
writer.println("
You are not logged in
");} else {
writer.println("Thank you, you are already logged in");
writer.println("Here is the data in your session");
Enumeration names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
-
7/28/2019 CBT lab
26/29
Object value = session.getAttribute(name);
writer.println("
name=" + name + " value=" + value + "
");}
}
// Write html for a new login
writer.println("
Return" +
" to login page
");writer.println("");
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
LoginSES.java
import javax.servlet.http.*;
import java.io.*;
public class LoginSES extends HttpServlet {
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response)
{
String username = request.getParameter("username");
-
7/28/2019 CBT lab
27/29
String password = request.getParameter("password");
// Get the session - if no session exists create one
HttpSession session = request.getSession(true);
// Set some attribute values to the session
// In this case user and password from the request and client
session.setAttribute("username", username);
session.setAttribute("password", password);
try {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("");
writer.println("Thank you, " + username +
". You are now logged into the system");
// Encodes the specified URL by including the session ID in it,
// or, if encoding is not needed, returns the URL unchanged
String newURL = response.encodeURL("/ServletSession/GetSession");
// Return a tag with the new url
writer.println("Click here for another servlet");
writer.println("");
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
7/28/2019 CBT lab
28/29
-
7/28/2019 CBT lab
29/29