File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes execute servlet without returning a page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "execute servlet without returning a page" Watch "execute servlet without returning a page" New topic
Author

execute servlet without returning a page

Hank Smith
Greenhorn

Joined: Jan 23, 2007
Posts: 7
My question is: What is the best way to process form data submitted to a servlet but not return an html page to the user?

Background:

I have inherited a servlet-based web application; it is intended for intranet use within a company. I am not a servlet guru.

On a typical page, the user will click a "modify" link to edit a database record; this link executes a servlet that returns an html page that contains a form. This new page is shown in a popup window. The user edits the data as needed and the form submission is processed by another servlet.

The customer of this application doesn't want a response page upon form submission, where normally you would show the user something that indicates success or failure. They want the form to simply be submitted, and no html is returned unless there was a major error of some kind in processing the form data.

The original developer wrote the application such that the servlet that processes the submitted form returns html that contains javascript that closes the window in the page's onLoad event.

This has worked OK until IE7 came along. Now, IE7 tells the user that the window is trying to close itself and prompts for approval. This, of course, is not desirable.

What is the best way to submit a form but not return a page? Or is it even possible with servlets? Should I be looking at some other technology?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Don't return anything at all in the response, just set the status to 204 (No Response). The browser will continue to display the same page that made the request.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

What does the customer want returned?

Just leaving the orginal form exposed is unlikely to lead to happy happy joy joy as users will have no way of knowing that anything has occurred and are likey to keep clicking the OK (or whatever) button and hammer the server with repeated requests.
[ January 23, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

I just did a bit of testing with IE7 and I was able to close a window opened via window.open() without IE7 nagging me with the "Are you sure?" dialog.

Before weilding the sledge-hammer, perhaps you just need to fine-tune how you are opening and closing the window.
Hank Smith
Greenhorn

Joined: Jan 23, 2007
Posts: 7
That was a quick response!

I'm afraid I goofed up describing the problem; sorry. I'll take a whack at describing the problem better and summarize things I've tried to date:

A typical page shows database records in an html table. The problem occurs with adding new records, not editing existing ones. At the top of the page is a button that adds new records to the underlying database table. The button's onClick event runs this logic:



The AddRecord servlet does some miscellaneous housekeeping, adds a new record to the table, and returns some bare-bones html that includes this javascript in the body's onLoad event:



In IE6, this causes the original page to refresh, showing the newly added record, and closes the bare-bones popup, normally happening so quickly that, if you see it at all, it's just a flicker. In IE7, *sometimes* this happens, but other times, you get this message from the browser (while the popup is displayed):

The webpage you are viewing is trying to close the window. Do you want to close this window?

Google searches show that other people have the same problem. A few people have proposed some javascript hacks that they claim work, but the ones I've tried haven't worked for me. This guy found a workaround that worked: The problem with this one is that it results in a prominent screen flicker, and I wonder how long it'll be before Microsoft tweaks IE7 to stop it from working anymore.

Paul suggested returning the http code SC_NO_CONTENT; I tried putting that at the end of the doGet() method in the servlet, in place of the bare-bones html, but that simply causes a blank popup window to appear. The user has to manually close the window.

As far as I can tell, there's no browser setting in IE7 that can be configured to allow popup windows to close themselves.

I've considered embedding a Java applet in the main page, and have the button's javascript logic call a method in the applet to do the record insert. The applet could either do it directly (this is done elsewhere in the application, for different reasons) or open a socket back to the server to have it do it. I'm not excited about this option, but it would work.

So there seems to be two possible ways to do this--the way described by the url above or the applet, but neither one is pretty. The ideal solution would be to have the existing servlet run its logic, but not have to return any html to the user.

Thanks for any ideas.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

In IE6, this causes the original page to refresh, showing the newly added record, and closes the bare-bones popup,


OK, then. Why are you submitting the servlet request to a popup at all?

Am I missing something? It seems as if you want the popup to appear then disappear before it can even show anything? What's the point of the popup in the first place?

I must be missing something becasue this makes no sense to me at all.
Hank Smith
Greenhorn

Joined: Jan 23, 2007
Posts: 7
Thanks for responding.

No, I don't think you're missing anything, but I understand why you're confused.

No popup is desired. What is desired is that when the html button is pressed, some logic on the server executes (a new record is inserted into a table, among other miscellaneous stuff) and the current page is refreshed, showing the new record.

What I described is how the app was originally implemented. The guy used servlets to execute the logic in the manner I described. Time went by and now IE7 is on the scene with its new popup behavior. Users don't like it and I'm nominated to figure out how to do this without a popup. Not being a web developer, I'm not sure what's the best way to do this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by Hank Smith:
No popup is desired. What is desired is that when the html button is pressed, some logic on the server executes (a new record is inserted into a table, among other miscellaneous stuff) and the current page is refreshed, showing the new record.


Good lord.

Please hunt down the original developer and hit him over the head with a Nerf bat. Using a popup to make such a request is completely ludicrous.

Ok, here's what you do. One of:

1) The easiest road would be to change from submitting to a popup to submitting to an invisible iframe embedded in the parent page.

2) Step into the modern era and learn how to use Ajax to make "back-door" requests.

Lose the popup. As fast as you can.
Hank Smith
Greenhorn

Joined: Jan 23, 2007
Posts: 7
Well, the original developer (a consultant hired by the company I work for) was fired a couple years ago, so he's out of Nerf bat range by now.

I've heard of Ajax before, but don't really know how it works. Time to learn!

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Going the iframe route will probably involve less code changes, but Ajax is probably the best way to accomplish your task.

Ajax and iframe questions can be posted to the HTML/Javascript forum where Eric and I hang out.

Is there any chance that your company can demand to be reimbursed by the clueless consultant? Sheesh! :roll:

P.S. You get credit points for coming here to find out better ways to do things.
Hank Smith
Greenhorn

Joined: Jan 23, 2007
Posts: 7
Ajax is *really* cool.

I spent some time learning about Ajax today. In my experimenting for this application, some javascript and the ActiveXObject("Microsoft.XMLHTTP") worked admirably.

Thanks again for the tip.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Bear in mind that the ActiveX version only works in IE.

If you have broader browser support in mind, you'll need to branch the code so that Firefox, Safari and other browsers will use the native XMLHttpRequest JavaScript object (which IE6 does not support).
Hank Smith
Greenhorn

Joined: Jan 23, 2007
Posts: 7
Yeah, I noticed that.

This particular application has numerous IE-only implementation details, and this fix will follow that lead. The app is scheduled for a complete next-generation rewrite in the near future.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by Hank Smith:
The app is scheduled for a complete next-generation rewrite in the near future.


That sounds like a really good idea!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: execute servlet without returning a page
 
Similar Threads
File upload
can't get my child window to close and reload the parent.
How to Return to Previous page that was request by Post Form Submitting after Login?
servlets and html forms.....
html file with multiple form elements