aspose file tools*
The moose likes Servlets and the fly likes servlet & jsp problem/enters doPost twice?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet & jsp problem/enters doPost twice??" Watch "servlet & jsp problem/enters doPost twice??" New topic
Author

servlet & jsp problem/enters doPost twice??

Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
I'm having a problem with my servlet. when a command is called, it is executed twice. In the first call, it does not exit the servlet but on the second round/execution, it exits the servlet twice(!)? Can someone help and tell me what's wrong?
Here's my code:
public void doPost(HttpServletRequest req, HttpServletResponse resp)
{
try
{
this.req = req;
this.resp = resp;
sess = req.getSession();

String action = (String)req.getParameter("axn");

if(action == null)
{
this.showDirectory(null);
}
else if(action.equalsIgnoreCase("create"))
{
out("action = create");
String dirName = this.create();
out("showing directory");
this.showDirectory(dirName);
}
else if(action.equalsIgnoreCase("copy"))
{
this.copy();
}
else if(action.equalsIgnoreCase("move"))
{
String dirName = this.move();
this.showDirectory(dirName);
}
else if(action.equalsIgnoreCase("delete"))
{
String dirName = this.delete();
this.showDirectory(dirName);
}

}
catch(Exception e)
{
out("Error in doPost -- ");
e.printStackTrace();
}
out("exiting servlet"); --> ON THE SECOND PASS, THIS LINE IS CALLED TWICE?

}
private void showDirectory(String dir)
{
try
{
if(dir == null)
dir = "C:";

dir = dir.endsWith(this.fileSep) ? dir : dir + fileSep;

File file = new File(dir);
sess.setAttribute("file", file);
getServletConfig().getServletContext().getRequestDispatcher("/jsp/showDir.jsp").forward(req, resp);
}
catch(ServletException se)
{
out("ERROR(servlet) -- showing directory");
se.printStackTrace();
}
catch(IOException ioe)
{
out("ERROR(IO) -- showing directory");
ioe.printStackTrace();
}
}
private String create()
{
String path = URLDecoder.decode((String)req.getParameter("path"));
String dirName = (String)req.getParameter("dirName");

File file = new File(path.trim(), dirName.trim());
file.mkdir();

out("returning from create");
return path.trim();
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
I don't see where you flush and/or close the response output stream. That could be a cause of never seeing a response to the first request.
What exactly does the out() method do?
Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Hi Olivia,
Three things:
1) If you put CODE tags around the code in your posts, it will make them a lot easier for us all to read as the formatting will be preserved.
2) Although this is probably unrelated to your double-post problem, I notice that you are storing the request and response instances in instance variables. This is a recipe for disaster as servlets need to be thread-safe. Please search through this forum for lots of information on making your servlets thread-safe.
3) Your double post problem may not be a problem with your servlet, but with the manner in which your servlet is being invoked. Could you provide the details of how you are invoking it?
For example, I've seen problems similar to this when a form is submitted and the form.submit() Javascript method is called from an onsubmit handler.
hth,
bear
[ July 18, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
Hi Bill & Bear,
Thank you so much for replying. Regarding flushing or closing the response output stream, I used reset to clear data in the buffer but the results are the same. Is this the proper way of closing/flushing a response output stream? Also, my out is just a method for echoing msgs to the System.
Yes I do invoke the submit via Javascript like this:

How does this affect the call to doPost?
Lastly, I wasn't aware that storing the request and the response to instance variables could be a recipe for disaster. I've been doing this and I haven't encountered any problems until this one. I'll be reading up on making servlets thread-safe. thanks for the tip
You guys are so nice and helpful! This is such a cool place
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
I forgot to add, from the from I have buttons for create, move, copy and delete and the javascript is called via the onClick method
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Are the buttons submit buttons?
bear
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
yup, they're submit buttons. I altered the code such that submit isn't called via JavaScript anymore ...

It's working fine now but I just want to know what was happening before, when submit was called via JavaScript?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

A button of type 'submit' is hard-wired to submit the form it is enclosed within. So when you clicked your button, the form got submitted. It also kicked off your onclick handler which also submitted the form via Javascript. Result: 2 posts.
If you are going to use onclick on a button, it is best not to make it a submit button.
hth,
bear
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
Thanks so much!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
Regarding flushing or closing the response output stream, I used reset to clear data in the buffer but the results are the same. Is this the proper way of closing/flushing a response output stream?

You should close the output stream to ensure that all of your output is sent. Reset is not what you want - that deletes any data and is typically only used when there has been an error and you need to discard existing output.
Bill
Olivia Mayo
Greenhorn

Joined: May 11, 2003
Posts: 29
oh ok, i get what you mean but i didn't use the servlet's output stream
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet & jsp problem/enters doPost twice??