You are on page 1of 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 {

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 {

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();

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)

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 (Enter the file name to download : b.txt)

1. a.txt and b.txt contains the same content.

OUTPUT:-

JText Field import java.awt.*; import javax.swing.*; /* <applet code="JTextFieldDemo" width=300 height=50> </applet> */ 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); } }

2 JBUTTON import java.awt.*; import java.awt.event.*; import javax.swing.*; /* <applet code="JButtonDemo" width=250 height=300> </applet> */ 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);

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()); } }

3 JCheck Boxes import java.awt.*; import java.awt.event.*; import javax.swing.*; /* <applet code="JCheckBoxDemo" width=400 height=50> </applet> */ 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");

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()); } }

4 JRadio Button import java.awt.*; import java.awt.event.*; import javax.swing.*; /* <applet code="JRadioButtonDemo" width=300 height=50> </applet> */ 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);

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()); } }

5 JCOMBO BOX import java.awt.*; import java.awt.event.*; import javax.swing.*; /* <applet code="JComboBoxDemo" width=300 height=100> </applet> */ 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) {

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.*; /* <applet code="JTreeEvents" width=400 height=200> </applet> */ public class JTreeEvents extends JApplet { JTree tree; JTextField jtf; public void init() { // Get content pane

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() {

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(""); } }

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 {

// get a printwriter from the HttpServletResponse objects ref. printWriter = response.getWriter(); // return on the HttpServletResponse objects ref. requested values printWriter.println("<p>"); printWriter.print("You entered: "); printWriter.print(enteredValue); printWriter.print("</p>"); printWriter.println("<p>"); printWriter.print("The following options were selected:"); printWriter.println("<br/>");

if (selectedOptions != null) { for (String option : selectedOptions) { printWriter.print(option); printWriter.println("<br/>"); } } else { printWriter.println("None");

} printWriter.println("</p>"); } catch (IOException e) { e.printStackTrace(); } } }

Login.html
<html> <head> <title>Login</title> </head>

<body> <h1>Login</h1>

Please enter your username and password <form action="/ServletSession/LoginSES" method="POST"> <p><input type="text" name="username" length="40"> <p><input type="password" name="password" length="40">

<p><input type="submit" value="Submit"> </form> </body></html>

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("<html><body>"); // If you are not in a session - you are not logged in if (session == null) { writer.println("<p>You are not logged in</p>"); } 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();

Object value = session.getAttribute(name); writer.println("<p>name=" + name + " value=" + value + "</p>"); } } // Write html for a new login writer.println("<p><a href=\"/ServletSession/login.html\">Return" + "</a> to login page</p>"); writer.println("</body></html>"); 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");

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("<html><body>"); 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 <a> tag with the new url writer.println("Click <a href=\"" + newURL + "\">here</a> for another servlet"); writer.println("</body></html>"); writer.close(); } catch (Exception e) { e.printStackTrace(); } } }