So the solution is keep it in your WEB-INF folder so that browser is not able to access it.Simple and steady.
That is one part of the story.Beacuse everything cant be kept in the WEB-INF.
Other part is –
We can also do it by the servlet mapping.
Lets take an example and learn it step by step.
For example we have made a project in the Netbeans with the name as WebApplication1.
And keep a image file in the WebPages -> images – > DoNotDownloadItImage.jpg.
Do the mapping fot the url in your web.xml.
Hello
NewClass
Hello
/images/*
Explanation for servlet mapping – What it will do it when ever it will find “images” written in the URL, it will instantiate the NewClass.java servlet.
And NewClass.java which is in your default package.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/* import javax.servlet.annotation.WebServlet;
* @WebServlet(name = “NewServlet”, urlPatterns = {“/images/*”}) .This thing is supported in glassFish server.
* But in tomcat it is not supported.
* Again easy to use.But universal law is to do the mapping in web.xml.
*/
public class NewClass extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/index.jsp");
dispatcher.forward(request,response);
}
}
//End of NewClass.
So now when you will run the url as ()
It will show you the index page instead of your image DoNotDownloadItImage.jpg.
And its done.
Same can be done for the jar file