• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"APPLET SERVLET " NOT WORKING

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an applet which simply has to communicate with the Servlet.My applet has textfield which send the data to the servlet .

MY APPLET:
---------:
public class CompanyMasterMaintenance extends JApplet {

boolean isStandalone = false;
GridBagLayout gridBagLayout1 = new GridBagLayout();
JLabel jtlcmpcode = new JLabel();
JTextField jtfcmpcode = new JTextField();
JLabel jlcmpname = new JLabel();
JTextField jtfcmpname = new JTextField();
JLabel jladd = new JLabel();
JLabel jldblink = new JLabel();
JTextField jtfdblink = new JTextField();
//Get a parameter value
DialogDemo dlgMode;
static String str1 ;
JLabel jlcmpcode = new JLabel();
TextArea jtaadd = new TextArea();
JButton jblkup = new JButton();
ButtonControl jbSave = new ButtonControl();
ButtonControl jbCancel = new ButtonControl();
ButtonControl jbExit = new ButtonControl();
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public CompanyMasterMaintenance() {
}
//Initialize the applet
public void init() {
try {
this.getContentPane().setBackground( new Color(155,218,238));
//dlgMode=new DialogDemo(this);
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//static {
// try {
// //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.metal.MetalLookAndFeel());
// //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.motif.MotifLookAndFeel());
// UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
// }
// catch (Exception e) {}
//}
//Component initialization
private void jbInit() throws Exception {
this.getContentPane().setBackground( new Color(155,218,238));
this.setVisible(true);
jtlcmpcode.setText("Company Code");
jlcmpcode.setForeground(new Color(0, 0, 120));
jlcmpcode.setFont(new Font("TimesRoman", 1, 12));
jtfcmpcode.setFont(new Font("TimesRoman", 0, 12));
jtfcmpcode.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
jtfcmpcode_keyTyped(e);
}
});
jtfcmpcode.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfcmpcode_actionPerformed(e);
}
});
jlcmpname.setText("Company Name");
jlcmpname.setForeground(new Color(0, 0, 120));
jlcmpname.setFont(new Font("TimesRoman", 1, 12));
jtfcmpname.setFont(new Font("TimesRoman", 0, 12));
jtfcmpname.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyTyped(KeyEvent e) {
jtfcmpname_keyTyped(e);
}
});
jladd.setText("Address");
jladd.setForeground(new Color(0, 0, 120));
jladd.setFont(new Font("TimesRoman", 1, 12));
jldblink.setText("DB Link");
jldblink.setForeground(new Color(0, 0, 120));
jldblink.setFont(new Font("TimesRoman", 1, 12));
jtfdblink.setFont(new Font("TimesRoman", 0, 12));
jtfdblink.addKeyListener(new
java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e4) {
jtfdblink_keyTyped(e4);
}
});
jlcmpcode.setText("Company Code");
jlcmpcode.setForeground(new Color(0, 0, 120));
jlcmpcode.setFont(new Font("TimesRoman", 1, 12));
jblkup.setText("?");
jblkup.setForeground(new Color(0, 0, 120));
jblkup.setFont(new Font("TimesRoman", 1, 12));
jbSave.setAlignment(borland.jbcl.util.Alignment.RIGHT | borland.jbcl.util.Alignment.MIDDLE);
jbSave.setLabel("Save");
jbSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbSave_actionPerformed(e);
}
});
jbCancel.setLabel("Cancel");
jbCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbCancel_actionPerformed(e);
}
});
jbExit.setLabel("Exit");
jbExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbExit_actionPerformed(e);
}
});
jblkup.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jblkup_mouseClicked(e);
}
});
jblkup.setEnabled(true);
jblkup.setVisible(true);
jtaadd.setBackground(Color.white);
this.getContentPane().setLayout(gridBagLayout1);
this.setSize(400,300);
this.getContentPane().add(jtfcmpcode, new GridBagConstraints2(1, 0, 1, 1, 0.0, 1.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 138, 0));
this.getContentPane().add(jlcmpname, new GridBagConstraints2(0, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(-1, 4, 0, 5), 5, 0));
this.getContentPane().add(jtfcmpname, new GridBagConstraints2(1, 1, 4, 2, 0.0, 1.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(-1, 0, 0, 0), 196, 0));
this.getContentPane().add(jladd, new GridBagConstraints2(0, 2, 1, 3, 1.0, 0.0
,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(39, 17, 35, 29), 2, 5));
this.getContentPane().add(jldblink, new GridBagConstraints2(0, 5, 1, 2, 1.0, 1.0
,GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 1), 0, -1));
this.getContentPane().add(jtfdblink, new GridBagConstraints2(1, 5, 3, 2, 1.0, 1.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(-1, 0, 0, 1), 185, 0));
this.getContentPane().add(jlcmpcode, new GridBagConstraints2(0, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 36, 0, 12), 64, 1));
this.getContentPane().add(jtaadd, new GridBagConstraints2(1, 3, 2, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 0, 11, 116), -280, -107));
this.getContentPane().add(jblkup, new GridBagConstraints2(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 1, 0, 97), 0, -5));
this.getContentPane().add(jbSave, new GridBagConstraints2(0, 7, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
this.getContentPane().add(jbCancel, new GridBagConstraints2(1, 7, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
this.getContentPane().add(jbExit, new GridBagConstraints2(2, 7, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
}
//Start the applet
public void start() {
dlgMode =new DialogDemo(JOptionPane.getFrameForComponent(this), this , true);
this.setEnabled(true);
jtfcmpcode.grabFocus();
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
CompanyMasterMaintenance applet = new CompanyMasterMaintenance();
applet.isStandalone = true;
JFrame frame = new JFrame();
frame.setTitle("CompanyMasterMaintenance");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(380,200);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
void jtfcmpcode_actionPerformed(ActionEvent e) {
}
void jtfcmpname_keyTyped(KeyEvent e2)
{
String cmpna=jtfcmpname.getText();
if(cmpna.length()>49)
{
cmpna=cmpna.substring(0,cmpna.length()-1);
jtfcmpname.setText(cmpna);

}
}
void jtfdblink_keyTyped(KeyEvent e4)
{
String link=jtfdblink.getText();
if(link.length()>24)
{
link=link.substring(0,link.length()-1);
jtfdblink.setText(link);
}
}
void jblkup_mouseClicked(MouseEvent me)
{
//this.requestFocus();
System.out.println(dlgMode.mode);


}
void jtfcmpcode_keyTyped(KeyEvent e) {
String coc=jtfcmpcode.getText();
if(coc.length()>9)
{
coc=coc.substring(0,coc.length()-1);
jtfcmpcode.setText(coc);

}
}
void jbSave_actionPerformed(ActionEvent e) {

String cmpcode="",cmpname="",cmpadd="",cmpdblink="",Servlet="",param="";
//y
// {

cmpcode=jtfcmpcode.getText();

cmpname=jtfcmpname.getText();
cmpadd=jtaadd.getText();
cmpdblink=jtfdblink.getText();
param=cmpcode+","+cmpname+","+cmpadd+"'"+cmpdblink;
System.out.println(cmpcode+cmpname+cmpadd+cmpdblink);
//}
//catch(NullPointerException ne){System.out.println(ne.getMessage());}
URL conn=null;
URLConnection urc;
try
{
// conn= new URL(protocol,host,int port,String file)
conn= new URL("http://localhost:8080/servlet/CompanyMasterMaintenance");
urc = conn.openConnection();
System.out.println("URL Connectin Ok");
urc.setUseCaches(false);
urc.setDoOutput(true);
PrintStream out = new PrintStream(urc.getOutputStream());
//DataOutputStream out=new DataOutputStream(urc.getOutputStream());
System.out.println("inside action "+param);
out.println(param);

//out.writeUTF(param);
out.flush();
out.close();
}
catch(IOException e1)
{
System.out.println(e1);
}
}
void jbCancel_actionPerformed(ActionEvent e) {
System.out.println("Cancel selected");
}
void jbExit_actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand().toString());
System.out.println("exit selected");
}
================================================================
MY SERVLET
================================================================
public class CMMServlet extends HttpServlet {
//Initialize global variables
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
System.out.println("1");
PrintWriter out = new PrintWriter (response.getOutputStream());
System.out.println("2");
out.println("<html>");
out.println("<head><title>CMMServlet</title></head>");
out.println("<body>");
out.println("</body></html>");
out.close();
try{
/* System.out.println("inside servlet");
DataInputStream din =new DataInputStream(request.getInputStream());
String param =din.readUTF();
System.out.println(param);
din.close(); */
StringBuffer msgBuf = new StringBuffer();
BufferedReader fromApplet = request.getReader();
String line;
while ((line=fromApplet.readLine())!=null) {
if (msgBuf.length()>0) msgBuf.append('\n');
msgBuf.append(line);
}
fromApplet.close();
// Write the message back to the applet
response.setContentType("text/plain");
PrintWriter toApplet = response.getWriter();
toApplet.println("d sent this text in response");
toApplet.close();
}
catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
//Get Servlet information
public String getServletInfo() {
return "CMMServlet Information";
}
}
but when i load the applet in the browser through html. my console says
"com.ms.security.SecurityExceptionEx[Host]: java.io.IOException: bad path: C:\WINDOWS\Java\lib\swing.properties
at com/ms/security/permissions/FileIOPermission.check
at com/ms/security/PolicyEngine.deepCheck
at com/ms/security/PolicyEngine.checkPermission
at com/ms/security/StandardSecurityManager.chk
at com/ms/security/StandardSecurityManager.checkRead
at java/io/FileInputStream.<init>
at java/io/FileInputStream.<init>
at com/sun/java/swing/UIManager.loadSwingProperties
at com/sun/java/swing/UIManager.initialize
at com/sun/java/swing/UIManager.maybeInitialize
at com/sun/java/swing/UIManager.getDefaults
at com/sun/java/swing/UIManager.getColor
at com/sun/java/swing/JPanel.<init>
at com/sun/java/swing/JPanel.<init>
at com/sun/java/swing/JRootPane.createGlassPane
at com/sun/java/swing/JRootPane.<init>
at com/sun/java/swing/JApplet.createRootPane
at com/sun/java/swing/JApplet.<init>
at CompanyMasterMaintenance.<init>
at com/ms/applet/BrowserAppletFrame.newInstance
at com/ms/applet/AppletPanel.processSentEvent
at com/ms/applet/AppletPanel.processSentEvent
at com/ms/applet/AppletPanel.run
at java/lang/Thread.run
com.ms.security.SecurityExceptionEx[Host]: Event queue access denied.
at com/ms/security/permissions/UIPermission.check
at com/ms/security/PolicyEngine.deepCheck
at com/ms/security/PolicyEngine.checkPermission
at com/ms/security/StandardSecurityManager.chk
at com/ms/security/StandardSecurityManager.checkAwtEventQueueAccess
at java/awt/Toolkit.getSystemEventQueue
at com/sun/java/swing/SwingUtilities.getEventQueue
at com/sun/java/swing/SwingUtilities.invokeLater
at com/sun/java/swing/JComponent.revalidate
at com/sun/java/swing/JTextField.setFont
at com/sun/java/swing/text/DefaultTextUI.installDefaults
at com/sun/java/swing/text/DefaultTextUI.installUI
at com/sun/java/swing/JComponent.setUI
at com/sun/java/swing/text/JTextComponent.setUI
at com/sun/java/swing/text/JTextComponent.updateUI
at com/sun/java/swing/text/JTextComponent.<init>
at com/sun/java/swing/JTextField.<init>
at com/sun/java/swing/JTextField.<init>
at CompanyMasterMaintenance.<init>
at com/ms/applet/BrowserAppletFrame.newInstance
at com/ms/applet/AppletPanel.processSentEvent
at com/ms/applet/AppletPanel.processSentEvent
at com/ms/applet/AppletPanel.run
at java/lang/Thread.run
"
please any one kindly solve this
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"sridhar",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
conn= new URL("http://localhost:8080/servlet/CompanyMasterMaintenance");
This line could be the problem.
An applet is allowed to open sockets only on the machine from where it was loaded. Thus, if the DNS name/IP address differs, the browser JVM would throw a Security Exception.
Change the above line to
String codebase = this.getCodebase();
String url = codebase + ":8080/servlet/CompanyMasterMaintenance";
conn = new URL(url);
If this does not work, try commenting out the Toolkit line. I am not sure, but, applets may not be allowed to get the Default toolkit.
Hope this helps.
Ashwin.
reply
    Bookmark Topic Watch Topic
  • New Topic