Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling a java servlet from an asp.net page

Status
Not open for further replies.

dubBeat

Programmer
Apr 2, 2007
21
IE
Hi,

I was wondering if somebody could tell me how to call a java servlet from an asp.net page and process the value returned from the servlet.

Im working with a servlet that does exactly what I need. Basically I enter one value and it returns another.

I've tried using httpResonse, httpRequest as follows>

*****************************************************

string getVars = "1234567";

HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("
WebReq.Method = "GET";

HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();



Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);

******************************************************

If I were to run the servlet by its self I enter a number into a text box and press submit. The submit button action is called "demo1_res".

I need a way to call the servlet passing the parameter and execute the method?



Im very new to all of this and im not really too sure how to approach the problem.

Thanks

Dub
 
Have a look at the source of the page for the servlet. The form tag should tell you the method (which I guess will be a POST). You then just need to make sure you supply the relevant form fields.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Hi, thanks for the reply.

It is a post and the action is name "demo1_res".

Have you got experience in programming asp.net?

Im new to it and im a bit lost as how to use the httpWebRequest functions to send a value to the servlet and to then execute and return the results of "demo1_res"

 
If you don't understand a class or how to use it, have a look at their relevant help files:



____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Yeah of course, it was one of the first things I looked at.

Unfortunately for me I didnt find the information presented very digestible or useful to me (novice).

Ive been on the lookout for more of a working example or tutorial but no joy so far.

Thanks anyway,
DUB
 
I'm not sure what you are having trouble with. The example given by microsoft is a working example for a console application so you can literally copy and paste it to see it working.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Maybe ive just been looking at the same problem for too long & lost my objectivity.

Could you help with my understanding a little?

I added the following line to the example

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("
In the example there is a line near the end which says

newStream.Write(byte1, 0, byte1.Length);

does this line actually send the stream to the address i specified above?

The only thing I got from the example was how to get the size of what you are sending
 
does this line actually send the stream to the address i specified above?
If you are using a POST method then yes it does post the data to the page.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Hi, things seem a bit clearer now.

Just one last question.

On the servlet page source I can see

1) the text entry box named"msisdn0"
2) the form action named "demo1_res"

The equivilent PHP code for what I want to do is

**************************************************
$number='1234567';
$form=array('msisdn0'=>$number);

$status=$http_client->post('/demo1_res',$form,'http:localhost:8080/demo1_res');

if($status==HTTP_STATUS_OK){
print($http_client->get_response_body());

**************************************************

above you can see I pass the method i want, the form text box and input value, and the address as parameters to a http client request.

How might I send these details using c#

Basically I dont know how to tell the httpWebRequest the method/action it should execute and pass a parameter to it.

is this achieved via the stream?

I have looked through the documentation but Im really not entirely sure what to searching for or investigating

Thanks
Dub

 
It depends on which form method (GET or POST) the page you are posting to uses. If it is a POST, then you'll need to use a StreamWriter to actually post the data. Otherwise, you can just append the querystring values and use the StreamReader to read the response. I've written up an example here.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 

I really appreciate the time you've taken to help me

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top