• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
michael byrd
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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,
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic