The moose likes Sockets and Internet Protocols and the fly likes Invoking a servlet from an application?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Invoking a servlet from an application??" Watch "Invoking a servlet from an application??" New topic

Invoking a servlet from an application??

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200

I am invoking a servlet from an application. But it doesnt seem to work. It doesnt give an exception either. Here is teh code i used
class HTTPS1
public static void main(String args[])

HTTPS1 url = new HTTPS1();

public void connectToUrl()


URL url = new URL("http://mildh0679:7070/gem/servlet/gem.BatchReportServlet?ACTION=BATCH");
System.out.println("Trying to connect to URL - " + url);
URLConnection con = url.openConnection();
System.out.println("con - " + con);
con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
OutputStream os = con.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
ObjectOutputStream oos = new ObjectOutputStream(bos);
System.out.println("writing hello");


catch(Exception e)


Any clues on this?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
How does it not work? Is the servlet executed in the container? Does it receive the string? Can you try reading the output from the servlet? What exactly is the output of the servlet?

More details will make it easier for us.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
I suspect the task might become easier by using Jakarta HttpClient:

Moving to Sockets and Internet Protocols...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
akalanka de silva

Joined: Sep 14, 2004
Posts: 18
hey iam not a servlet expert and i want to learn the way i make a chat programme by applet.and i also want to invoke the servlet.but i dont know how do it.but i understand u r program and i want to know what method is invoke in the servlet
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Since our friend invokes con.setDoOutput(true), the request will be an HTTP POST and it will invoke doPost() on our servlet. Please do not follow his example, as it will not work. He is creating a request but never sending it to the remote server. One must either invoke connect() or invoke a method which depends on connect(), like getInputStream(), or the request is never made. For the correct use of URLConnection, look at the Java Documentation and thisJava Almanac example

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Invoking a servlet from an application??
It's not a secret anymore!