aspose file tools*
The moose likes Other Open Source Projects and the fly likes Using JSP to post data to external url without losing control focus 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 "Using JSP to post data to external url without losing control focus" Watch "Using JSP to post data to external url without losing control focus" New topic
Author

Using JSP to post data to external url without losing control focus

David Cowan
Greenhorn

Joined: May 01, 2009
Posts: 17
Hi Everyone,

I have a little problem I am hoping someone can help me with please. I need to use a JSP to "POST" a block of text to an external web page, this must not be done using params, it needs to be a plain text payload. The target url will provide no response and hence I need my jsp to post the data to the external url but then keep processing within the jsp. This would work using a <c:import> with params but I cannot use the params, it must be a post of a block of text. If I could have used a POST Method on <c:import> I would not have any problem. Any suggestions anyone please?

Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

A JSP is a phenomenally and spectacularly poor choice of where to try to do something like this. Why not from a servlet where the classes in java.net (or 3rd party tools like HttpClient) would make this relatively straightforward?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Cowan
Greenhorn

Joined: May 01, 2009
Posts: 17
Cheers Bear, for that optimistic prognosis, I know it should be done in a servlet, but does that mean it is impossible to do from a JSP?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67



Only if you party like it's 1999 and put Java code in the form of scriptlets in your JSP.

Is there any reason that you feel the need to do this in a JSP rather than its controller?
David Cowan
Greenhorn

Joined: May 01, 2009
Posts: 17
Well Bear, where do I start .... my limited Java capacity (I'm an old mainframe C programmer who is still retraining ;) ), my lack of a setup Java compile environment having just switched to a new Apple Mac and worst of all I just need a temporary fix while I re-design this properly.
David Cowan
Greenhorn

Joined: May 01, 2009
Posts: 17
you can laugh now if you want
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

Is the situation that you've inherited some legacy web app code with JSPs already written the "old way", with Java code embedded? Since you mentioned the JSTL (<c:import>), I assumed modern standards were being used.

If legacy code, then yeah, a refactor just to add some new functionality isn't called for. But in new code (or legacy code that's so bad that not refactoring prevents any forward progress), JSPs and web app should be written using modern constructs and standards.

But, by all that's good, if your app is already using modern standards, you should not introduce antiquated practices like scriptlets into the JSPs.

In any case, there are no JSP mechanisms to generate a POST request that's not form-generated. You can use the aforementioned java.net package, or a tool such as HttpClient.
David Cowan
Greenhorn

Joined: May 01, 2009
Posts: 17
Thanks Bear, I'll take a look at the stuff you suggest.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

I find HttpClient 3 easier to get started with than HttpClient 4.... just saying.
David Cowan
Greenhorn

Joined: May 01, 2009
Posts: 17
Bear - Quick Follow-up

I have moved to using httpclient as you suggested. I went straight to v4 with it being on GA, to save moving again later. However, in my "newness" to this I cannot see how you POST just a plain old text string?!? I can see how to send name/value pairs (which is not what I want) and I can see how to send a file stream ( which is not what I want) but no plain old post this string as body text. Can you steer me in the right direction please?

Thanks in advance for the help
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

To be honest, I have no information for you because I'm still using HttpClient 3. I was going to update a month or so ago, and found the new layout just too confusing to bother upgrading just for the sake of it.

As this has specifically become about HttpClient, I've moved it along to the open source forum where hopefully someone savvy about HttpClient 4 can help you out.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Maybe check out creating your own HttpRequestWriter, but I'm not convinced twisting HttpCore to do what you're looking for is the best answer--I'd probably explore other libraries first to see if there's an easier way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using JSP to post data to external url without losing control focus