Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoking a servlet from an application??

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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
mport java.net.URL;
import java.net.URLConnection;
import java.io.*;
class HTTPS1
{
public static void main(String args[])
{

HTTPS1 url = new HTTPS1();
url.connectToUrl();



}
public void connectToUrl()
{

try
{

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.setDoOutput(true);
con.setUseCaches(false);
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");
oos.writeObject("hello");

oos.flush();
oos.close();
System.out.println("closed");




}
catch(Exception e)
{
e.printStackTrace();
}
}

}

Any clues on this?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the task might become easier by using Jakarta HttpClient: http://jakarta.apache.org/commons/httpclient/

Moving to Sockets and Internet Protocols...
 
akalanka de silva
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey iam not a servlet expert and i want to learn it.by 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
Bartender
Posts: 9279
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic