2

I want to delete files after a click on an HTML link in a jsp page.

The following is my jsp code:

<%
File f=new File("c:\\Folder\\1.jpg");
f.delete();
%>

What href should I use in the HTML code?

<a href......>Delete me </a>
jxh
  • 69,070
  • 8
  • 110
  • 193
user2651739
  • 53
  • 1
  • 1
  • 5
  • 1
    For important security reasons you cannot delete a file on somebody's computer when they click a buton on a web page. – jahroy Aug 23 '13 at 05:30
  • Is your goal to delete a file that exists on the server? If so, see the answer by @shreyanshjogi. You can do what he suggests or make a request to a servlet (which would be better). To make a request to a Servlet, you need to configure your container to map URL patterns to your servlet. Here's [a question about servlet mapping](http://stackoverflow.com/q/8198312/778118). You'll have to google about how to set up servlet mapping on whatever container you're using. – jahroy Aug 23 '13 at 05:46

4 Answers4

5

Html plays on client side and Java(Jsp) plays on server side.You need to make a server request for that.

And one more point

File f=new File("c:\\Folder\\1.jpg");

After you made the request the above line tries to remove the file from the server not from the user machine(who clicked the link).

You might misunderstand that jsp and html existed on same document. Yes but JSP part compiles on server side itself and JSP output resolves as html and is sent to the client.

Note:Html and Javascript cannot have access to files on the machine due to security reason.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
4

For this you can use j query to delete without refreshing Here is the code lets have a try

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function(e) {
   $( "#deletefilesAnchor" ).click(function(e) {
        e.preventDefault();
        if (confirm('Are you sure you want to Delete Files?')) {
        // Save it!
             $.ajax({
                type: "POST",
                url: "action.jsp",
                success: function(msg){
                    alert(msg)
                },
             });
        } else {
        // Do nothing!
        }
   }); 
});

</script>
</head>
<body>
<a id="deletefilesAnchor" href="#">Delete files</a>
</body>
</html>

action.jsp

<%
File f=new File("c:\\Folder\\1.jpg");
if(f.delete())
out.println("Sucessfully deleted file");
else
out.println("Error in deleting file");
%>
1
if(request.getParameter("btnSubmit")!=null) //btnSubmit is the name of your button, not id of that button.
{
File f=new File("c:\\Folder\\1.jpg");
f.delete();
}

<input type="submit" id="btnSubmit" name="btnSubmit" value="delete"/>

This you you can achieve

shreyansh jogi
  • 2,082
  • 12
  • 20
  • Note that this will only delete files that exist on the server (i.e. the computer that hosts the website). It will have no effect on a person who is using a web browser on their personal computer. – jahroy Aug 23 '13 at 05:31
  • they can check on the file whether file exist or not? – shreyansh jogi Aug 23 '13 at 05:32
  • I don't understand what you're asking. Could you please clarify? Code can be written that does just about anything to files on the server. **But**, you cannot manipulate files on somebody's computer using code that runs in a browser (unless you're using ActiveX or a Java applet or some kind of plugin). – jahroy Aug 23 '13 at 05:34
  • that is java code so obeviously you are manipulation from the hosted once and if i run from my machine code is available at hosted location not in my pc and even file is also at hosted location. so will it be create any problem to delete? – shreyansh jogi Aug 23 '13 at 05:36
  • There is no reason you can't delete a file on the server. Maybe that's what the OP actually wants. If so, this answer is correct. Hopefully the OP will clarify what he wants. – jahroy Aug 23 '13 at 05:38
0

You can't do that like this way.

servlet/jsp is run on the server side, but the html link is run on the client side(the browser). If you see the source code of the page(click the right button of the mouse over the browser page), then you can see the jsp code is not exist.

If you want to do that, your should link to another page(like b.jsp), then in the jsp, use the the code above to delete the file.

mccxj
  • 17
  • 5