• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Invoking a servlet from an application??

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic