cancel
Showing results for 
Search instead for 
Did you mean: 

Java calling a servlet...

david_fryda2
Participant
0 Kudos

Hi,

Is there a way to call a servlet from a Java standalone class (of course I want to pass to the servlet parameters and to retrieve results from the servlet) ?

Thanks a lot.

Regards.

Accepted Solutions (1)

Accepted Solutions (1)

guru_subramanianb
Active Contributor
0 Kudos

Hi,

From your requirement I understand that you wanted to send objects at runtime from java class to servlet.In Java technology this can be acheieved by a concept called "Serialization".

Please refer to the java docs on

java.io.serialization

You will get better idea.

david_fryda2
Participant
0 Kudos

Thanks Guru for your help.

Regards.

Answers (1)

Answers (1)

guru_subramanianb
Active Contributor
0 Kudos

Hi David,

I think you can do this.

Jus try some thing like this inside your stand alne java class :-

URL test = new URL(servletURL);

where servletURL should contain the complete URL of your servlet

test.openStream();

I suggest you check more on URL class in Java docs.

Hope it helps you a bit.

Regards,

Guru

david_fryda2
Participant
0 Kudos

Hi Guru,

Thanks for the reply.

In my serlvet I wrote:

response.getWriter().write("in the doGet method");

In my Java class, I wrote the following :

URL url = new URL("http://server:port//Servlet1/theservlet");

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)

System.out.println(inputLine);

in.close();

I do get the String "in the doGet method".

But what if I wanted to send Objects ?

Is there a way ?

I cannot user session of course....

Any idea will be welcome.

Thanks in advance.