aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doDelete Method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doDelete Method?" Watch "doDelete Method?" New topic
Author

doDelete Method?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9384
    
    2

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!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Mahesh Desai
Ranch Hand

Joined: Apr 04, 2007
Posts: 76
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

Joined: Jan 27, 2003
Posts: 130
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
Marshal

Joined: Mar 22, 2005
Posts: 41880
    
  63
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 ]

Ping & DNS - my free Android networking tools app
Mahesh Desai
Ranch Hand

Joined: Apr 04, 2007
Posts: 76
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

Joined: Apr 28, 2004
Posts: 128
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?


Thanks, Tridib
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9384
    
    2

Thanks guys for the help!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41880
    
  63
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

Joined: Apr 28, 2004
Posts: 128
Thanks Dittmer!
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
We can also you methods from java.io.File for such operations.


Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doDelete Method?