File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Writing an alert into a HttpServletResponse object

Ratish Vichithran
Greenhorn

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 Download.do?status=X 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.

if(details.isEmpty()){
response.setContentType("text/html");
PrintWriter pw = new PrintWriter(response.getOutputStream());
pw.write("<script type=\"text/javascript\" >" +
"alert('There are no records to be downloaded')" +
"</script>");
pw.close();
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: 2902
    
    1

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
Greenhorn

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

But my problem is various jsps call the same action Download.do. 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. Download.do?status=E). 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: 2902
    
    1

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing an alert into a HttpServletResponse object
 
Similar Threads
Strange Problem with servlet output
Image preview with upload
Upload a File Using jQuery
Null values being returned from servlet
showing in Excel Sheet