Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling of servlet from sub menu's actionPerformed??

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all friends,
I am facing with one problem I want to call a servlet from a swing interface for that I make a menu and from submenu's actionPerformed i want to call a servlet.can any one plz give me the code for that.I tried it like below but it is not working:-
-------------------------------------------------
menuItem = new JMenuItem("Chat", KeyEvent.VK_C);
menuItem.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ev)
{
try
{
URL url=new URL("http://127.0.0.1:8080/examples/servlet/ChatServlet?config_file");
}catch(Exception ex){ex.printStackTrace();}
}
});
FirstMenu.add(menuItem);
-------------------------------------------------
Regards
Bikash
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so you've created a URL object with the URL of your servlet, now you need to actually get the data that the servlet provides. To do this you'll need to use either the openConnection() or openStream() methods of the URL class. openStream() just opens an InputStream containing the response of your servlet. openConnection() creates a URLConnection object holding the response. You'll probably want to use URLConnection because it does some processing on the response for you. Either way, you'll have to get the content of the response and parse out the data that you need. Remember, if the servlet is returning HTML, because you'll have to parse out the tags to get the <i>real</i> data (unless you're displaying it in a JEditorPane and you want HTML...), you can set the content type of your servlet to return as plain text if you don't want to deal with this.

Hope this helps,
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate,
First of all thanks for ur reply.As per Shigan,Jeriba I think my question is not clear now Iam trying to put my question more elaborately:-
I have one swing interface and I want to call a servlet (which is a chat servlet and It is a pure HTML implementation, so this chat is free from any firewall's restrictions.)from one sub menu's actionPerformed().My chatServlet working like when i call my servlet from html file(<A HREF="http://127.0.0.1:8080/examples/servlet/ChatServlet?config_file">Chat</A> it first opens login page which is also a html file.I write this html file inside servlet file.I want to open this login page from my swing interface on browser(IE).Can any one plz guide me how I can do that.I tried it with below given code but couldn't slove this problem:-
menuItem = new JMenuItem("Chat", KeyEvent.VK_C);
menuItem.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ev)
{
try {
URL url = new URL("http://127.0.0.1:8080/examples/servlet/ChatServlet?config_file");
URLConnection uc =url.openConnection();
//what will be the code
}
catch(Exception ex) { ex.printStackTrace();}
}
});
FirstMenu.add(menuItem);
My servlet accept some parameter from this configuration file(config_file.txt).
Regards
Bikash
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code that shows how to get the HTML data from a URL and how it comes back... just replace "http://www.javaranch.com" with your URL...

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate,
Thanks for ur help.But In ur given code it is giving me error during compilation on below line:-
StringBuffer buffer = new StringBuffer();
.............
.............
.............
buffer.append(Character.toString((char)c));
Error is:-method "toString(char)"
Can we put "char" in toString() method ?
Regards
Bikash
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate,
I modified ur given code like below:
........
......
......
BufferedReader br = new BufferedReader( reader );
Thread.yield();
String str = br.readLine();
Thread.yield();
while ( str != null )
{
buffer.append(str);
str = br.readLine();
Thread.yield();
}
.........
........
..........
But it is giving me a below html file so for this file I have to open Internet Explorer in action of sub menu .Can u plz guide me how I can do that.
<html>
<head><title>kdk chat</title>
</head>
<frameset border=1 rows="75%,25%">
<frame name="up" src="http://127.0.0.1:8080/examples/servlet/ChatServlet?conf=config_file&actn=get&id=1019909272274626162">
<frame name="down" src="http://127.0.0.1:8080/examples/servlet/ChatServlet?conf=config_file&actn=init2">
</frameset>
</html>
Regards
Bikash
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... the correct question in this case was "How do I launch Internet Explorer from my Swing application?"

This code shows the answer to that...
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate,
Lot of thanks for ur help.U r right boss my question was not correct.Actually this is the solution that I wanted.
Thanks
Bikash
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic