Vous êtes sur la page 1sur 16

G.

NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


JAVA BEANS Program to display caption using simple visual beans Java file: package p1; import java.awt.*; public class demo extends Canvas{ private String caption; public demo(){ caption="demo"; } public String getCaption(){ System.out.println("get method"); return(caption); } public void setCaption(String ncaption){ System.out.println("set method"); caption=ncaption; repaint(); } public void paint(Graphics g){ g.drawString(caption,20,20); } }

Mft file: Name: p1/demo.mft Java-Bean: True

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Output:

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


WEEK 6 Create a simple visual bean with an area filled with a color. The shape of the area depends on the property shape. If it is set to true then shape is square and if it is set to false, shape is circle. The color should be changed dynamically for every mouse click and also when the color in the property window is changed. Java file: package p1; import java.awt.*; import java.io.*; import java.awt.event.*; import java.beans.*; public class week6 extends Canvas implements MouseListener{ private String shape; private String color; public week6(){ shape="true"; color="red"; addMouseListener(this); } public String getcolor(){ return(color); } public void setcolor(String c){ color=c; repaint(); } public String getshape(){ return(shape); } public void setshape(String s){ shape=s; repaint(); } public void mouseClicked(MouseEvent me){ if(color.equals("red")){ setcolor("black"); return; } if(color.equals("black")){ setcolor("blue"); return; }

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


if(color.equals("blue")){ setcolor("green"); return; } if(color.equals("green")){ setcolor("red"); return; } } public void mousePressed(MouseEvent me){} public void mouseReleased(MouseEvent me){} public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){} public void paint(Graphics g){ if(color.equals("red")) g.setColor(color.red); if(color.equals("black")) g.setColor(color.black); if(color.equals("blue")) g.setColor(color.blue); if(color.equals("green")) g.setColor(color.green); if(shape.equals("true")){ g.drawRect(50,50,100,100); g.fillRect(50,50,100,100); } else{ g.drawOval(20,20,200,200); g.fillOval(20,20,200,200); } } }

Mft file: Name: p1/week6.mft Java-Bean: True

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Output:

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Program illustrating the bound property Java file: package p1; import java.awt.*; import java.beans.*; public class bound1 extends Canvas{ private String caption; private PropertyChangeSupport pcs; public bound1(){ caption="demo"; pcs=new PropertyChangeSupport(this); } public String getCaption(){ System.out.println("getMethod"); return(caption); } public void setCaption(String ncap){ System.out.println("setMethods"); pcs.firePropertyChange("Caption",caption,ncap); caption=ncap; repaint(); } public void paint(Graphics g){ g.drawString(caption,20,20); } public void addPropertyChangeListener(PropertyChangeListener pcl){ pcs.addPropertyChangeListener(pcl); } public void removePropertyChangeListener(PropertyChangeListener pcl){ pcs.removePropertyChangeListener(pcl); } public void PropertyChange(String caption,String cap,String ncap){ g.drawString("inside pc"); } } Mft file: Name: p1/bound1.mft Java-Bean: True

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Output:

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Program illustrating the usage of auto property Java file: package p1; import java.awt.*; import java.beans.*; public class auto extends Canvas implements VetoableChangeListener{ private int speed; private VetoableChangeSupport vcs; public auto(){ speed=0; vcs=new VetoableChangeSupport(this); setSize(50,50); repaint(); } public int getSpeed(){ System.out.println("get method"); return(speed); } public void setSpeed(int nspeed) throws PropertyVetoException{ try{ System.out.println("set method"); vcs.fireVetoableChange("speed",speed,nspeed); speed=nspeed; repaint(); } catch(Exception e){ System.out.println(e); } } public void paint(Graphics g){ g.drawString(speed+" ",20,20); } public void addVetoableChangeListener(VetoableChangeListener vcl){ vcs.addVetoableChangeListener(vcl); } public void removeVetoableChangeListener(VetoableChangeListener vcl){ vcs.removeVetoableChangeListener(vcl); } public void VetoableChange(PropertyChangeEvent pce) throws PropertyVetoException{ String ne=(String)pce.getNewValue(); if(ne.equals(" ")) throw new PropertyVetoException("caption cannot be blank",pce); } } Mft file:

Name: p1/auto.mft Java-Bean: True

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Output:

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


SERVLETS Program illustrating working of simple servlet import java.io.*; import javax.servlet.*; public class Myservlet1 extends GenericServlet{ public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter pw=response.getWriter(); pw.println("<html><head><title>this is my srvlet</title>"); pw.println("<head><body><h1>this came from my servlet</h1>"); pw.println("</body></html>"); pw.close(); } } Output:

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Program for posting details through servelets Java file: import java.io.*; import java.util.*; import javax.servlet.*; public class postparamservlet extends GenericServlet{ public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException{ PrintWriter pw=res.getWriter(); Enumeration e=req.getParameterNames(); while(e.hasMoreElements()){ String pname=(String)e.nextElement(); pw.print(pname+"="); String pvalue=req.getParameter(pname); pw.println(pvalue); } pw.close(); } } Html file:

<html> <body> <center> <form name="Form1" method="post" action="http://localhost:8002/1204/postparamservlet"> <table> <tr> <td><b>employee</td> <td><input type=textbox name="e" size="25" value=""></td> </tr> <tr> <td><b>phone</td> <td><input type=textbox name="p" size="25" value=""></td> </tr> </table> <input type="submit" value="submit"> </body> </html>

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE


Output:

G.NARAYANAMMA INSTITUTE OF TECHNOLOGY AND SCIENCE

Vous aimerez peut-être aussi