Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing an alert into a HttpServletResponse object

 
Ratish Vichithran
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ratish Vichithran
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic