• 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

Is this the way to invoke a servlet from an applet?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am trying to invoke a servlet from an applet. The applet contains a JTree structure and on selecting a node of the tree an event for the invocation of servlet is occured. I run this code and no exception comes in my browser java console. But the servlet doent even invoke and show its output.

Here is my applet code:

its init method.

public void init()
{

root = new DefaultMutableTreeNode("root", true);
node1 = new DefaultMutableTreeNode("node 1", true);
node2 = new DefaultMutableTreeNode("node 2" , true);
node3 = new DefaultMutableTreeNode("node 3", true);
root.add(node1);
node1.add(node2);
root.add(node3);
setLayout(new BorderLayout());
tree = new MyJTree(root);
add(new JScrollPane((JTree)tree),"Center");
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//Listen for when the selection changes.
tree.addTreeSelectionListener(this);

}
public void valueChanged(TreeSelectionEvent e)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();

if (node == null) return;
Object nodeInfo = node.getUserObject();

if (node.isLeaf())
{
System.out.println("LEaf");
try
{
URL url = new URL("http://localhost:3265/AppletServlet/treeserv");
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
}
catch(MalformedURLException e5)
{System.out.println(e5.toString());}
catch(IOException e3)
{System.out.println(e3.toString());}
}
else
{
System.out.println("Root");
}
}

My servlet is perfect as it runs normally when i give its own address
in the browser.So i am not presenting its code here. Please send me sample code if possible

Thanks in advance,
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you creating the URLConnection in your init() method? HTTP is a request-response protocol. It doesn't have a persistent connection like a plain socket so you need to create a new instance with each request.
Do you ever use URLConnection to make a request? The code you have here just sets up some variables. Are you actually sending anything? Receiving anything?
 
reply
    Bookmark Topic Watch Topic
  • New Topic