This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Struts and the fly likes Writing an alert into a HttpServletResponse object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Writing an alert into a HttpServletResponse object" Watch "Writing an alert into a HttpServletResponse object" New topic

Writing an alert into a HttpServletResponse object

Ratish Vichithran

Joined: Feb 27, 2009
Posts: 9
In my application there are several buttons in various pages which does the same functionality of downloading records into an excel file.
All requests for download go to where X varies. Now based on X the data present in the HttpSession object is filtered and is written into an excel.

If in case there is no data existing in the HttpSession object after filtering is applied then I was thinking about showing an alert box which will inform that there are no records to be downloaded.

I have written the below code to achieve that.

PrintWriter pw = new PrintWriter(response.getOutputStream());
pw.write("<script type=\"text/javascript\" >" +
"alert('There are no records to be downloaded')" +
return null;

But the problem with this is that, the screen which was previously available goes blank and only the alert pop-up is shown. I want the previous screen to be as it is and only the alert box to appear. As in case of an excel which comes as an attachment on setting the ContentDisposition
header attribute in HttpServletResponse

can anyone please help me on how I will have to achieve this?

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

I consider you're using Struts 1, (As there is not HttpServlet class in S2),

Its simple thing if you return to the same JSP page from where request is coming, then in Action class you can use like this:

Now, alerting part,
Do you seriously requires that alert or can you print the ActionMesssage using Action#saveErrors() method on JSP page.

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ratish Vichithran

Joined: Feb 27, 2009
Posts: 9
Hi Sagar,
Thanks for the reply.

But my problem is various jsps call the same action Each jsp needs a different set of data that has to be downloaded so I make use of a 'status' request parameter added to the query string (for eg. The value of status varies.

So based on the status value when I don't get any records, I wante to return back an alert message which has to appear before all jsps and the screen state at the background shdn't change.

I hope I am clear this time around.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

It's hard and not so worthy to embed the SCRIPT tag in the response out put using the input JSP page, I suspect that it will mess up the whole JSP page from where the request is coming , also JavaScript is for user experience, its client side thing and can't be altered or handled from server.
Still, I put my approach for the same if you want to do that,
what I thought of doing this is:
1. Get the incoming JSP page
2. Try to embed the "alert" JS tag into it (Its hard to do, have some Googling) OR simply append the JS
3. response output this new modified JSP

Isn't that hard ?

The best and standard way is always been the ActionMeassges in Struts.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Writing an alert into a HttpServletResponse object
It's not a secret anymore!