Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doDelete Method?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

How can I implement the doDelete method as I want to delete a file called welcome.html when this method is called. How can I do this??

Please help guys!
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the sample code where you can see how we can implement doDelete() method in servlet class.....

------------

public class DeleteDemoServlet extends HttpServlet{

public void init() throws ServletException{
//initialization code..
}

public void doGet(HttpServletRequest req, HttpServletResponse rs) throws IOException, ServletException{

}

public void doGet(HttpServletRequest req, HttpServletResponse rs) throws IOException, ServletException{
}

public void doDelete(HttpServletRequest req, HttpServletResponse
rs) throws IOException, ServletException{

boolean success = false;
// get the file to delete
File file=searchFile(request);


if (!file.exists()){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}else{
//delete the file if it is exist.
success=file.delete(); // actual delete operation
}

if (success){
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}

public void destroy(){
// cleanup code....
}
}




Thanks,
Mahesh

---------
SCJP 1.4, SCWCD 1.4, SCBCD 1.3, (SCEA Part I preparing......)
 
Dhananjay Inamdar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mahesh,

From where will I get the "searchFile()" method's implementation. I checked it is not part of the HttpServlet. Please let me know.

Also you have mentioned doGet() method 2 times, I guess you wanted to say doGet() and doPost(). Please verify.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It only makes sense to implement doDelete if you have a client that can send it. A class like java.net.HttpUrlConnection, the Jakarta HttpClient library, a WebDAV client or an JavaScript XmlHttpRequest can do this, but otherwise (i.e., during normal browser activity) this can't happen. How were you planning to activate the method?
[ April 10, 2007: Message edited by: Ulf Dittmer ]
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dhananjay,

searchFile() method would contain the logic to locate the file which you want to delete. It's not part of in any API's. We need to write that method.


private String searchFile(HttpServletRequest req) throws Exception{
// get the file to view
String fileName = req.getPathInfo();

// strip off the leading slash
fileName = fileName.substring(1);

return fileName;
}


Also there was a mistake in my last post, I repeated doGet() method twice in servlet code, it shouldn't be.

Thanks,
Mahesh
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh,
I have a doubt regarding the doDelete(). It seems that we can do the same thing using doPost(). So why will we use doDelete() if we don't get any inbuit file deletion facility?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the help!
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why will we use doDelete()

Indeed, doDelete is rarely used. It was introduced into HTTP along with doPut when the notion of the read/write web was still very much alive (instead of the largely read-only web that actually emerged). It was envisioned that browsers would allow the user to write and alter pages as well as read them. That never happened, and as a consequence, those methods are rarely used. One place where they are used is the WebDAV HTTP extension, which facilitates file operations over HTTP.
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dittmer!
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can also you methods from java.io.File for such operations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic