aspose file tools*
The moose likes Other Open Source Projects and the fly likes Calling a URL via HttpClient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Calling a URL via HttpClient" Watch "Calling a URL via HttpClient" New topic
Author

Calling a URL via HttpClient

Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
Hi - I am new to java.

I have a JSP page that needs to make a URL Get Post and get the response back.
I was thinking of putting the code in a Java Bean that is accessible via the JSP Page.

I can call the MultiThreadedHttpConnectionManager and then
client = new HttpClient();
client.executeMethod(method)
method.getResponseBody

This code is to execute the Get URL and retrieve it's return value.

The question that I have is can I put this code in a Java Bean or do I need to put it in a Servlet ?

Also can I write to a log file from within a Java Bean set property method?

Can I have a method in a Java Bean and how do I call the method from JSP?

Thanks very much.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Leo Cono wrote:I have a JSP page that needs to make a URL Get Post and get the response back.

A GET or a POST? It can't be both.

I can call the MultiThreadedHttpConnectionManager and then
client = new HttpClient();

Why? Is the resource to be accessed in another web application?

Can I have a method in a Java Bean and how do I call the method from JSP?

That greatly depends upon what you mean by "call". The term is quite ambiguous in this context.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
I want to make a URL Get call to an external web application and get a response back.

I want to make the URL Get call within a method in a java bean that is callled from a jsp page.
Is this possible or do I need a servlet?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12779
    
    5
Any Java class could make the URL call - unless there is some SecurityManager preventing it.

Be sure you handle all the possible error conditions.

A big advantage of using a separate class is that you can test it outside the servlet environment - why not just put in a main() method that gets the url from the command line.

Bill
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
I am unfamiliar with creating a method inside a bean and callling it from JSP.

Do I create a set property and treat it like a method? or is there a way to create methods and call them from jsp?

Thanks very much.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

If you are writing modern JSPs using SJTL and EL there is no way to call general methods, as that is something expected to be handled by page controller servlets. What is it about this that forces you to do it from a JSP?
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
The jsp page is collecting user registration information and I want to send this registration information data to another web site via url get execution.
May you please advise the best way to do this?

Thanks very much.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

So you want to gather the info after the user has entered info?

If so, you don't want to call anything from the JSP which gets executed before the page is ever even sent to the user.

This is something you'd do in the servlet controller to which the form is submitted after the user has filled out the info.
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
The jsp page does a postback to itself.
1.) user enter registration data into a jsp page
2.) jsp <% %> section captures the data entered into the form.
3.) jsp calls a bean method to send captured data via URL Get to an external web site
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Leo Cono wrote:The jsp page does a postback to itself.
1.) user enter registration data into a jsp page
2.) jsp <% %> section captures the data entered into the form.
3.) jsp calls a bean method to send captured data via URL Get to an external web site


That's a really out-dated and poor practice. You might want to consider bringing your application up to modern standards and practices.

You'll also find that it prevents you from taking best advantage of modern techniques.

Maybe you'll find this article helpful.
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
Ok but this is not my code and I can not rewrite the java 1.1 architecture.

May you please show me an example of jsp executing a "url get" to an external web site?

Thanks very much.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

What does this have to do with Java 1.1?

If you are working with older code, it'd best to follow the precedent of that code unless you plan a major refactor.

Are there no examples within the code of making calls to bean methods?

And...
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
Yes I have to work with the code I have.

I decided to not put the code in a bean.

From the jsp scriplet, I want to make a URL Get call to an external web site.

Do you know how to do this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

That makes no sense. Even in an old scriptlet environment it's still easier to call a method (one line of Java) than to put a bunch of code in the JSP. Why do you think that's hard?

Just call the code from the JSP scriptlet code. What's the issue?

Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
So appearantly you do not know how to do this.

Does anybody else know how to make a URL Get call to an external web site from a jsp scriplet?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Leo Cono wrote:So appearantly you do not know how to do this.

Apparently you learned nothing from the parable I linked to.

So what is "this"? The use of HttpClient, or how to call a method? You've been very unclear on exactly what you are having trouble with.

I asked for clarification and all I get is lip service? Antagonizing people who are trying to help you is not a winning strategy -- I suggest you re-evaluate your attitude and approach.
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
If you are really trying to help then please give me an answer on how to make a URL get call to an external web site from a jsp page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

You have to cooperate in order to get help; not exude attitude issues.

Again I ask, what aspect are you having problems with? HttpClient? Calling methods? What?

Also, if you are just trying to import text (you haven't really said) why don't you just use <c:import> and be done with it?
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
HttpClient
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

OK then. This has nothing much to do with JSP them. If you are using scriptlets, the the Java code would be the same as you would write anywhere else.

This post has been moved to a forum more appropriate for dealing with HttpClient questions.

I've also adjusted the title a bit to help people find your question.
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
Thanks Very Much
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Wait; now *I'm* confused.

Have you looked at the HttpClient example code? What's going wrong when you're making the request?
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
Where can I find HttpClient example code?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

On its website? I mean, that's where I'd look first, anyway. Seems logical.

http://hc.apache.org/httpclient-3.x/tutorial.html
Leo Cono
Greenhorn

Joined: Sep 06, 2009
Posts: 13
thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a URL via HttpClient