aspose file tools*
The moose likes Struts and the fly likes struts Action for asynchronous HTTP processing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts Action for asynchronous HTTP processing" Watch "struts Action for asynchronous HTTP processing" New topic
Author

struts Action for asynchronous HTTP processing

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hello,
I'm new to struts2 and want to implement the following scenario:

1-user clicks a button
2- user is redirected to an external URL : user will fill forms and submit them at this external URL
3-after user is done submitting forms in external website this later will post HTTP message to a given' status_url' . i need to be able to collect these HTTP params as they come asynchronously to 'status_url' and when HTTP transfer is complete i need to confirm.

Now i thought to build two struts2 actions :
2-first action for redirect to external URL with some hidden html fields constructed dynamically
2-second struts action will be kind of a listener which given a status_url this action will keep reading HTTP params untill it receives HTTP OK.

how can i implement these 2 actions? is there a better way to accomplish this scenario above with some technique in struts 2 framework?

thanks.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
HTTP is a request -> response medium. When you send your HTTP request to the server, it provides an HTTP response. This response has to come in pretty quickly, or your client (the browser, usually) will get tired of waiting and just give up.

However, you can design an HTML page such that while it sits there, showing something to the user, it also runs off every once in a while and sends another request to a server, receiving a response in return. This can be done without any action by the user (for example, you can have a timer set that fires every few seconds). You can trigger this additional request/response cycle with a button the user presses (a 'check status' button, for example) or through other, more automated ways. Ajax can be a help here, as it would make it possible for this request/response to appear to be happen behind the scenes (you won't see the page refresh with each cycle).

With this, you can have your initial request result in a "job" getting started, and then have your result page fire off requests (Ajax or otherwise) to check on the status, updating the display page as appropriate.

However, keep in mind that nothing can send a response to your client for which it didn't send a request. There is no such thing as a Request -> Response -> Later Response model.

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Ok Thank you Mark for the reply.
i think it's better i proceede point by point. so let me explain the first problem i need to solve:

in my struts web app when user clicks a button he should be redirected to a given URL and there should be a form with hidden fields posted to this URL.
what i did is write a method that generates HTML code :

I'm not sure if there is a better struts technique to use for generating this html form and redirecting user to gatwayURL? could we write a struts action to perform this task?
after we solve this point i will describe the following scenario that happens after user is redirected to external URL with html form hidden data.

So for now i'm trying to design a struts technique to implement the form generation and user redirection logic . any suggestion how i can implement this?

thanks
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
You really don't want to generate the HTML markup in your server component (I assume post() is called from your Action object). This isn't just a Struts 2 issue, but an MVC issue (Model View Controller). Instead, you should have a JSP page that generates the desired HTML response page. You can use JSTL in the JSP page to create any dynamic content needed on the page. Then, just have the Struts 2 action object forward to the view page when it's done with its processing.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Mark E Hansen wrote:You really don't want to generate the HTML markup in your server component (I assume post() is called from your Action object). This isn't just a Struts 2 issue, but an MVC issue (Model View Controller). Instead, you should have a JSP page that generates the desired HTML response page. You can use JSTL in the JSP page to create any dynamic content needed on the page. Then, just have the Struts 2 action object forward to the view page when it's done with its processing.


Yes very good suggestion. I need to think about the JSTL code to generate the html form fields like i did in post() method.
is it possible you help me write this JSTL code to convert logic in post() method into JSTL code inside a jsp page? I didn't done JSTL programming much and would like to save time if someone can help converting the post() html code generation into jsp logic through JSTL. if not i'll have to do this task myself (will need to refresh my JSTL memories).

any other suggestion beside using JSTL in a jsp page for this scenario? just want to use the best solution for this problem

thanks.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I can't write the JSTL for you. However, you're making it sounds like the JSTL is going to generate the HTML page. I just want to make sure you realize that the JSP page is for all practical purposes, just an HTML page with a little bit of additional markup in it. Just create the HTML page the way you want it to appear and rename it to .jsp and you have a JSP page. Then, in the page where you need dynamic content, look at technology like JSTL to help with that.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Ok it seems i didn't yet found the right solution. i'm not sure what JSTL could do ? could it generate a html form with hidden fields? and then tell jsp to redirect user to url?

I'm not yet able to think of a good solution to the problem i described. i wonder how a java programmer from a jsp page or struts action could redirect user to a url with some POST hidden fields dynamically embedded to this redirect request?


Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
Yahya Elyasse wrote:Ok it seems i didn't yet found the right solution. i'm not sure what JSTL could do ? could it generate a html form with hidden fields? and then tell jsp to redirect user to url?

I'm not yet able to think of a good solution to the problem i described. i wonder how a java programmer from a jsp page or struts action could redirect user to a url with some POST hidden fields dynamically embedded to this redirect request?


I think you're over thinking this. Do you know how to create an HTML page which includes a hidden field? It's just a <input type="hidden" ...> element in the HTML markup. Create the HTML page that you want to see, and you have the JSP page - because except for some additional markup, that's all a JSP page is.

Once you have the JSP page, then look for areas where you want dynamic content and use JSTL (or something else) to generate the content.

I think you'll have a much easier time of this if you took a look through some Servlets and JSP tutorials.
Also, have a look at the book Core Servlets and JavaServer Pages. The book's web site provides the chapters for free as PDF downloads here: http://pdf.coreservlets.com

You'll then want to do the same thing for JSTL. I don't know of any free books, but the book I used is called JSTL in Action and it was excellent. I'm sure you'll be able to find some free tutorials though, if you Google a little.


Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Mark E Hansen wrote:
Yahya Elyasse wrote:Ok it seems i didn't yet found the right solution. i'm not sure what JSTL could do ? could it generate a html form with hidden fields? and then tell jsp to redirect user to url?

I'm not yet able to think of a good solution to the problem i described. i wonder how a java programmer from a jsp page or struts action could redirect user to a url with some POST hidden fields dynamically embedded to this redirect request?


I think you're over thinking this. Do you know how to create an HTML page which includes a hidden field? It's just a <input type="hidden" ...> element in the HTML markup. Create the HTML page that you want to see, and you have the JSP page - because except for some additional markup, that's all a JSP page is.

Once you have the JSP page, then look for areas where you want dynamic content and use JSTL (or something else) to generate the content.

I think you'll have a much easier time of this if you took a look through some Servlets and JSP tutorials.
Also, have a look at the book Core Servlets and JavaServer Pages. The book's web site provides the chapters for free as PDF downloads here: http://pdf.coreservlets.com

You'll then want to do the same thing for JSTL. I don't know of any free books, but the book I used is called JSTL in Action and it was excellent. I'm sure you'll be able to find some free tutorials though, if you Google a little.



the <input type=hiden> parts are dynamic. these hidden fields are read from a Map then they need to be sent as hidden input fields to the URL.
I have some JSTL basic knowlege. but my jsp page need to know about the Map from where to get the hidden fields.and also need to know about the URL which is dynamic also. I thought to use struts for this but you are pushing me to use JSTL which i believe is not very different from the post() method i wrote that generates the html form.
i did read the books core servlets , jsp etc..I'm trying to use struts for this that's why i post this question in the struts section. i could write a servlet to do this but i prefer using struts for all my logic. Hope you understand what i mean.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
So you want to create a list of items which you want your JSP page to iterate over and create a set of <input type="hidden"...> elements? How about using the Struts <s:iterator ...> tag? If you place this "map" as a member variable on your action (using standard JavaBeans accessors/mutators) then it will be available to the <s:iterator> tag in the JSP.

I was not intending to push you to use JSTL, only that JSTL is one easy way to do dynamic content in a JSP page. In your original example, you were creating the HTML markup in your servlet, which is a no-no.

As for the URL, have a look at the <s:url> struts tag.

A JSP page which makes use of tags (whether JSTL, Struts, etc.) is greatly different from a Java class that uses print statements to create an HTML page. This is where I think you're missing the whole point of MVC. Just create what you want as the final HTML page as a mock-up. Then look at the parts that are static, and the parts that are dynamic. The dynamic parts can be generated with tags, while the static part can just exist in the JSP page, just as it does in your mock-up HTML page.

Even consider that in the <input type="hidden"...> tag, the part showing here is not dynamic, it is going to be the same no matter what value you provide for the tag.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts Action for asynchronous HTTP processing